espnet2.enh.layers.complex_utils.to_complex
Less than 1 minute
espnet2.enh.layers.complex_utils.to_complex
espnet2.enh.layers.complex_utils.to_complex(c)
Convert input to a native PyTorch complex tensor.
This function converts the input tensor to a native PyTorch complex tensor format. It handles both ComplexTensor and native complex tensors. If the input is neither, it attempts to convert the tensor using torch.view_as_complex.
- Parameters:c (Union *[*torch.Tensor , ComplexTensor ]) – The input tensor to be converted.
- Returns: The converted complex tensor.
- Return type: Union[torch.Tensor, ComplexTensor]
Examples
>>> import torch
>>> from torch_complex.tensor import ComplexTensor
>>> real = torch.tensor([1.0, 2.0])
>>> imag = torch.tensor([3.0, 4.0])
>>> complex_tensor = ComplexTensor(real, imag)
>>> to_complex(complex_tensor)
tensor([1.+3.j, 2.+4.j])
>>> native_complex_tensor = torch.tensor([1.0, 2.0], dtype=torch.complex64)
>>> to_complex(native_complex_tensor)
tensor([1.+0.j, 2.+0.j])
>>> real_tensor = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> to_complex(real_tensor)
tensor([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])