PyTorch torch.conj() Method

PyTorch torch.conj() method “returns a view of input with a flipped conjugate bit.” If the input has a non-complex dtype, this function just returns input.

Syntax

torch.conj(input)

Parameters

input (Tensor): The input tensor.

Example 1: Basic usage with a 1D complex tensor

import torch

tensor_1 = torch.tensor([1 + 2j, 3 - 4j, -1 + 5j])
conjugate_1 = torch.conj(tensor_1)

print("Original Tensor:")
print(tensor_1)
print("\nConjugate Tensor:")
print(conjugate_1)

Output

Original Tensor:
tensor([ 1.+2.j, 3.-4.j, -1.+5.j])

Conjugate Tensor:
tensor([ 1.-2.j, 3.+4.j, -1.-5.j])

Example 2: Using the method with a 2D complex tensor

import torch

tensor_2 = torch.tensor([
  [1 + 1j, 2 - 2j],
  [3 + 3j, 4 - 4j]
])
conjugate_2 = torch.conj(tensor_2)

print("Original Tensor:")
print(tensor_2)
print("\nConjugate Tensor:")
print(conjugate_2)

Output

Using the method with a 2D complex tensor

Example 3: Using the method with real numbers

import torch

tensor_3 = torch.tensor([1, 2, 3, 4])
conjugate_3 = torch.conj(tensor_3)

print("Original Tensor:")
print(tensor_3)
print("\nConjugate Tensor (same as original for real numbers):")
print(conjugate_3)

Output

Original Tensor:
tensor([1, 2, 3, 4])

Conjugate Tensor (same as original for real numbers):
tensor([1, 2, 3, 4])

That’s it!

Related posts

torch.argwhere()

torch.adjoint()

torch.polar()

Leave a Comment