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

To compute the elementwise angle of the given input tensor, we apply torch.angle(). It takes an input tensor and returns a tensor with angle in radian computed element wise. To convert the angles into the degree we multiply the angle in radian by 180/np.pi. It supports both real and complex-valued tensors.

### Syntax

torch.angle(input)

### Steps

To compute the elementwise angle, you could follow the steps given below −

• Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
• Define torch tensors and print them.

input = torch.tensor([1 + 1j, -1 -4j, 3-2j])
• Compute torch.angle(input). It is a tensor with angle in radians computed elementwise for the input.

angle = torch.angle(input)
• Print the above computed tensor with angles in radians.

print("Angles in Radian:", angle)

Let's take a couple of examples to demonstrate how to compute elementwise angles in radians and degrees.

## Example 1

In the Python3 program below we compute element wise angle in radians and degrees of the given input tensor.

# Python 3 program to compute the element-wise
# angle (in radians and degree) of the given input tensor
# Import the required libraries
import torch
from numpy import pi

# define a complex input tensor
input = torch.tensor([1 + 1j, -1 -4j, 3-2j])

# print the input tensor
print("Input Tensor:", input)

# compute the angle in radians
angle = torch.angle(input)

# print the computed tensor of angles

# convert the angle s in degree
degree = angle*180/pi

# print the computed tensor of degree
print("Angles in Degree:", degree)

## Output

Input Tensor:
tensor([ 1.+1.j, -1.-4.j, 3.-2.j])
tensor([ 0.7854, -1.8158, -0.5880])
Angles in Degree:
tensor([ 45.0000, -104.0362, -33.6901])

## Example 2

In this program, we compute element wise angle in radians and degrees of the given input tensor.

# Python 3 program to to compute the element-wise
# angle (in radians and degrees) of the given input tensor
# Import the required libraries
import torch
from numpy import pi

# define a complex input tensor
real = torch.randn(3,4)
imag = torch.randn(3,4)
input = torch.complex(real, imag)

# print the input tensor
print("Input Tensor:", input)

# compute the angle in radians
angle = torch.angle(input)

# print the computed tensor of angles

# convert the angle s in degree
degree = angle*180/pi

# print the computed tensor of degree
print("Angles in Degree:", degree)

## Output

Input Tensor:
tensor([[ 0.1967-0.0188j, -0.5311-1.2427j, 0.9937+0.0051j, - 1.8304-0.1321j],
[-0.1787+1.7834j, 0.9925+0.2452j, -0.8813-0.0207j, - 0.4967+0.9938j],
[-0.9051-0.1204j, 1.0013+0.3430j, 0.6131-0.0317j, - 0.3861+0.6365j]])
[-172.4209, 18.9103, -2.9624, 121.2437]])