torch.view_as_real
Convert a complex tensor back to its real representation.
Reverses the transformation of view_as_complex() by adding back the final dimension
that separates real and imaginary parts. The last dimension of the output will be exactly 2,
with index 0 containing real parts and index 1 containing imaginary parts.
This is the inverse operation of view_as_complex() and enables round-trip conversions
without data loss.
- Inverse operation: Perfect inverse of
view_as_complex() - Shape expansion: Adds back the final dimension of size 2
- Zero-copy: Reinterprets shape without copying data
- Round-trip property: Always satisfies
view_as_real(view_as_complex(x)) == x - Storage format: Exposes internal [..., 2] representation that complex uses
- Input requirement: Input should be output of
view_as_complex()or have [..., 2] shape - Manual [..., 2] tensors: If manually creating, ensure correct [real, imag] layout
- No dtype change: Output dtype is same as input (float32 for 'complex64', etc)
Parameters
inputTensor- Complex tensor logically representing complex numbers. Internally stored as [..., 2] real pairs from
view_as_complex()
Returns
Tensor– valued tensor with shape [..., 2] where: - Element [..., 0] is the real part - Element [..., 1] is the imaginary partExamples
// Round-trip conversion
const original = torch.tensor([[1, 2], [3, 4]]); // [2, 2]
const complex = torch.view_as_complex(original); // [2] complex
const reconstructed = torch.view_as_real(complex); // [2, 2] real
// reconstructed equals original exactly
// Extract real and imaginary parts
const z = torch.view_as_complex(torch.tensor([[1, 2], [3, 4]]));
const real_imag = torch.view_as_real(z); // [[1, 2], [3, 4]]
const real_parts = real_imag.select(-1, 0); // [1, 3] - just real parts
const imag_parts = real_imag.select(-1, 1); // [2, 4] - just imaginary parts
// Signal processing: Extract components from FFT
const fft_output = torch.randn(100, 50, 2); // FFT result
const complex_fft = torch.view_as_complex(fft_output); // [100, 50]
const real_repr = torch.view_as_real(complex_fft); // [100, 50, 2]
// Magnitude and phase
const z = torch.view_as_complex(torch.tensor([[3, 4]])); // 3+4i
const repr = torch.view_as_real(z); // [[3, 4]]
const mag = torch.norm(repr, 2, -1); // sqrt(3^2 + 4^2) = 5See Also
- PyTorch torch.view_as_real()
- view_as_complex - Inverse operation converting real [..., 2] to complex [...]
- complex - Create from separate real and imaginary tensors
- polar - Create from magnitude and phase