Open In App

How to compute the element-wise angle of given input tensor in PyTorch?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch.

torch.angle() method

Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angle() method to compute the element-wise angle, the input is passed in the form of a tensor in radians. To compute the angle in the degree we multiply the angle in radian by 180/NumPy.pi, before moving further let’s see the syntax of the given method:

Syntax: torch.angle(tens, *, out=None)

Parameters:

  • tens – This is our input tensor.
  • out – This is our output tensor (optional). 

Returns: Returns the elements-wise angle of given tensor.

Example 1:

In this example, we will compute element-wise angles in radians of a given 1-D tensor.

Python3




# Import the required libraries
import torch
  
# define a complex tensor
input = torch.tensor([1 - 2j, 2 + 1j, 3 - 2j, -4 + 3j, -5 - 2j])
  
# print the above define tensor
print("\n Input Tensor: ", input)
  
# compute the elements-wise angle in radians
ang = torch.angle(input)
  
# print the computed elements-wise angle in radians
print("\n Elements-wise angles in radian: ", ang)


Output:

How to compute the element-wise angle of given input tensor in PyTorch?

 

Example 2:

In this example, we will compute element-wise angles to convert the angle into a degree of the given 1-D tensor.

Python3




# Import the required libraries
import torch
from numpy import pi
  
# define a complex tensor
input = torch.tensor([1 - 2j, 2 + 1j, 3 - 2j, -4 + 3j, -5 - 2j])
  
# print the above define tensor
print("\n\nInput Tensor: ", input)
  
# compute the elements-wise angle in radians
ang = torch.angle(input)
  
# convert the angle in degree
deg = ang*180/pi
  
# print the computed elements-wise angle in degree
print("\nElements-wise angles in degree: ", deg)


Output:

How to compute the element-wise angle of given input tensor in PyTorch?

 

Example 3:

In this example, we will compute element-wise angles to convert the angle into a degree of the given 2-D tensor.

Python3




# Import the required libraries
import torch
from numpy import pi
  
# define a complex tensor
input = torch.tensor([[1 - 2j, 2 + 3j, 3 - 3j],
                      [4 + 3j, 5 - 4j, -6 + 2j],
                      [-7 - 2j, 8 + 2j, 9 - 4j]])
  
# print the above define tensor
print("\n Input Tensor:\n ", input)
  
# compute the elements-wise angle in radians
radians = torch.angle(input)
  
# print the computed elements-wise angle in radians
print("\n Elements-wise angles in radians:\n ", radians)
  
# convert the angle in degree
degree = radians * 180/pi
  
# print the computed elements-wise angle in degree
print("\n Elements-wise angles in degree:\n ", degree)


Output:

How to compute the element-wise angle of given input tensor in PyTorch?

 



Last Updated : 22 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads