Skip to content

Commit cb7227d

Browse files
[Term Entry] PyTorch Tensor Operations: .angle()
1 parent ec410ba commit cb7227d

File tree

1 file changed

+157
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/angle

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
Title: '.angle()'
3+
Description: 'Computes the element-wise angle of complex tensors in PyTorch.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Data Science'
9+
- 'Mathematics'
10+
- 'PyTorch'
11+
- 'Tensor'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/data-science'
15+
---
16+
17+
The **`.angle()`** method in PyTorch is a tensor operation that computes the element-wise angle (in radians) of a given complex tensor. This function returns the phase angle of each complex value in the tensor, representing the argument of each complex number. The `.angle()` method is particularly useful in signal processing, Fourier transforms, and other applications that involve complex number operations.
18+
19+
When working with complex tensors in PyTorch, the `.angle()` method allows you to extract the phase information from your data. This is essential for analyzing wave phenomena, processing image data in the frequency domain, and understanding the directional components of complex-valued neural networks.
20+
21+
## Syntax
22+
23+
```pseudo
24+
torch.angle(input, *, out=None)
25+
```
26+
27+
**Parameters:**
28+
29+
- `input`: The input complex tensor for which to compute the element-wise angle.
30+
- `out` (Optional): Output tensor to store the result. If provided, this tensor will be used for the output, otherwise a new tensor is created.
31+
32+
**Return value:**
33+
34+
A new tensor containing the element-wise angle (in radians) of the input tensor.
35+
36+
> **Note:** Starting in PyTorch 1.8, `.angle()` returns π (pi) for negative real numbers, zero for non-negative real numbers, and propagates NaNs. In previous versions, it would return zero for all real numbers and not propagate floating-point NaNs.
37+
38+
## Example 1: Basic usage of `.angle()`
39+
40+
This example demonstrates how to calculate the element-wise angle of a complex tensor and understand the output in radians:
41+
42+
```py
43+
# Import the required libraries
44+
import torch
45+
46+
# Create a complex tensor
47+
input = torch.tensor([1 - 2j, 2 + 1j, 3 - 2j, -4 + 3j, -5 - 2j])
48+
49+
# Print the input tensor
50+
print("\nInput Tensor: ", input)
51+
52+
# Compute the element-wise angle in radians
53+
ang = torch.angle(input)
54+
55+
# Print the computed element-wise angle in radians
56+
print("\nElement-wise angles in radian: ", ang)
57+
```
58+
59+
This example produces the following output:
60+
61+
```shell
62+
Input Tensor: tensor([ 1.-2.j, 2.+1.j, 3.-2.j, -4.+3.j, -5.-2.j])
63+
64+
Element-wise angles in radian: tensor([-1.1071, 0.4636, -0.5880, 2.4981, -2.7611])
65+
```
66+
67+
The output shows the angle of each complex number in radians. For example, the angle of `1-2j` is approximately `-1.1071` radians.
68+
69+
## Example 2: Converting angles from radians to degrees
70+
71+
This example shows how to convert the angle from radians to degrees, which is often more intuitive for visualization and interpretation:
72+
73+
```py
74+
# Import the required libraries
75+
import torch
76+
from numpy import pi
77+
78+
# Create a complex tensor
79+
input = torch.tensor([1 - 2j, 2 + 1j, 3 - 2j, -4 + 3j, -5 - 2j])
80+
81+
# Print the input tensor
82+
print("\nInput Tensor: ", input)
83+
84+
# Compute the element-wise angle in radians
85+
ang = torch.angle(input)
86+
87+
# Convert the angle to degrees
88+
deg = ang * 180 / pi
89+
90+
# Print the computed element-wise angle in degrees
91+
print("\nElement-wise angles in degree: ", deg)
92+
```
93+
94+
The example produces this output:
95+
96+
```shell
97+
Input Tensor: tensor([ 1.-2.j, 2.+1.j, 3.-2.j, -4.+3.j, -5.-2.j])
98+
99+
Element-wise angles in degree: tensor([-63.4349, 26.5651, -33.6901, 143.1301, -158.1986])
100+
```
101+
102+
Converting to degrees makes the angles easier to interpret in many applications, particularly when visualizing phases in a polar coordinate system.
103+
104+
## Example 3: Working with multi-dimensional complex tensors
105+
106+
This example demonstrates how the `.angle()` method handles multi-dimensional complex tensors, which is common in applications like image processing or multi-channel signal analysis:
107+
108+
```py
109+
# Import the required libraries
110+
import torch
111+
from numpy import pi
112+
113+
# Define a complex 2D tensor
114+
input = torch.tensor([
115+
[1 - 2j, 2 + 3j, 3 - 3j],
116+
[4 + 3j, 5 - 4j, -6 + 2j],
117+
[-7 - 2j, 8 + 2j, 9 - 4j]
118+
])
119+
120+
# Print the input tensor
121+
print("\nInput Tensor:\n", input)
122+
123+
# Compute the element-wise angle in radians
124+
radians = torch.angle(input)
125+
126+
# Print the computed element-wise angle in radians
127+
print("\nElement-wise angles in radians:\n ", radians)
128+
129+
# Convert the angle to degrees
130+
degree = radians * 180 / pi
131+
132+
# Print the computed element-wise angle in degrees
133+
print("\nElement-wise angles in degrees:\n ", degree)
134+
```
135+
136+
This produces the following output:
137+
138+
```shell
139+
Input Tensor:
140+
tensor([[ 1.-2.j, 2.+3.j, 3.-3.j],
141+
[ 4.+3.j, 5.-4.j, -6.+2.j],
142+
[-7.-2.j, 8.+2.j, 9.-4.j]])
143+
144+
Element-wise angles in radians:
145+
tensor([[-1.1071, 0.9828, -0.7854],
146+
[ 0.6435, -0.6747, 2.8198],
147+
[-2.8633, 0.2450, -0.4182]])
148+
149+
Element-wise angles in degrees:
150+
tensor([[-63.4349, 56.3099, -45.0000],
151+
[ 36.8699, -38.6598, 161.5650],
152+
[-164.0546, 14.0362, -23.9625]])
153+
```
154+
155+
This example shows how `.angle()` preserves the dimensionality of the input tensor, calculating the phase angle for each complex value while maintaining the original structure.
156+
157+
PyTorch provides a wide range of tensor operations for various computations. Explore functions in the [PyTorch Tensor Operations](https://www.codecademy.com/resources/docs/pytorch/tensor-operations) documentation to effectively manipulate tensors.

0 commit comments

Comments
 (0)