PyTorch torch.view_as_complex function
PyTorch torch reference manual
torch.view_as_complex A function in PyTorch for converting a view of a real-valued tensor into a complex tensor. It transforms the last dimension from two real values (real and imaginary parts) into a single complex value.
Function definition
torch.view_as_complex(input)
Usage examples
Example
import
torch
Create a real-valued tensor; the last dimension must have 2 elements
x
=
torch.
randn
(
3
,
2
)
Convert to a complex view
y
=
torch.
view_as_complex
(
x
)
(
"Original real tensor shape:"
,
x.
shape
)
(
"Complex tensor shape:"
,
y.
shape
)
(
"Complex tensor:"
,
y
)
The output is:
原始实数张量形状: torch.Size([3, 2])
复数张量形状: torch.Size([3])
复数张量: tensor([ 1.2345+0.5678j, -0.3456+1.2345j, 2.3456-0.9876j])