PyTorch torch.view_as_complex Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.view_as_complexIt is a function in PyTorch used to convert a real tensor view into a complex tensor. It converts the last dimension from two real values (real part and imaginary part) into a single complex value.

Function Definition

torch.view_as_complex(input)

Usage Example

Example

import torch

# Create a real tensor; the last dimension must have 2 elements
x = torch.randn(3, 2)

# Convert to complex view
y = torch.view_as_complex(x)

print("Original real tensor shape:", x.shape)
print("Complex tensor shape:", y.shape)
print("Complex tensor:", y)

The output result is:

原始实数张量形状: torch.Size([3, 2])
复数张量形状: torch.Size([3])
复数张量: tensor([ 1.2345+0.5678j, -0.3456+1.2345j,  2.3456-0.9876j])

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions