complextorch.nn.modules.casting#

Drop-in torch.nn.Module adapters that convert between real-tensor layouts and complex tensors, so they compose inside torch.nn.Sequential.

These layouts come up in real-to-complex pipelines:

  • Interleaved: [..., 2*D] with real and imaginary parts interleaved: (re_0, im_0, re_1, im_1, ...).

  • Concatenated: [..., 2*D] with all real parts followed by all imaginary parts: (re_0, ..., re_{D-1}, im_0, ..., im_{D-1}).

If your data is in (..., 2) final-dim layout (one slot for real, one for imag), use torch.view_as_complex() / torch.view_as_real() directly — no wrapper is needed.

Classes#

ComplexToConcatenated

Complex → Concatenated Real Layout

ComplexToInterleaved

Complex → Interleaved Real Layout

ConcatenatedToComplex

Concatenated Real Layout → Complex

InterleavedToComplex

Interleaved Real Layout → Complex

RealToComplex

Real → Complex (Zero Imaginary)

Module Contents#

class complextorch.nn.modules.casting.ComplexToConcatenated(*args: Any, **kwargs: Any)[source]#

Bases: torch.nn.Module

Complex → Concatenated Real Layout#

Inverse of ConcatenatedToComplex. Maps complex [..., D] to real [..., 2D] with all real parts followed by all imaginary parts.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input: torch.Tensor) torch.Tensor[source]#
class complextorch.nn.modules.casting.ComplexToInterleaved(*args: Any, **kwargs: Any)[source]#

Bases: torch.nn.Module

Complex → Interleaved Real Layout#

Inverse of InterleavedToComplex. Maps complex [..., D] to real [..., 2D] with real/imag interleaved.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input: torch.Tensor) torch.Tensor[source]#
class complextorch.nn.modules.casting.ConcatenatedToComplex(*args: Any, **kwargs: Any)[source]#

Bases: torch.nn.Module

Concatenated Real Layout → Complex#

Maps [..., 2D] with the first D slots being real parts and the last D slots being imaginary parts to a complex tensor of shape [..., D].

Input: (re_0, ..., re_{D-1}, im_0, ..., im_{D-1}).

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input: torch.Tensor) torch.Tensor[source]#
class complextorch.nn.modules.casting.InterleavedToComplex(*args: Any, **kwargs: Any)[source]#

Bases: torch.nn.Module

Interleaved Real Layout → Complex#

Maps [..., 2D] with real/imag interleaved along the last dim to a complex tensor of shape [..., D].

Input: (re_0, im_0, re_1, im_1, ...).

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input: torch.Tensor) torch.Tensor[source]#
class complextorch.nn.modules.casting.RealToComplex(dtype: torch.dtype = torch.cfloat)[source]#

Bases: torch.nn.Module

Real → Complex (Zero Imaginary)#

Lifts a real tensor into a complex tensor by setting the imaginary part to zero. Useful as the first layer of a network whose input is a real signal but whose internal representations are complex.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

extra_repr() str[source]#

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(input: torch.Tensor) torch.Tensor[source]#
dtype = Ellipsis#