Skip to content

Commit 25b6483

Browse files
authored
docs: Add PyTorch Tensor Operations entry for .rad2deg() (#7852) (#7923)
* docs: Add PyTorch tensor method .rad2deg() entry (#7852) * Update rad2deg.md * minor content fixes ---------
1 parent d9883e5 commit 25b6483

File tree

1 file changed

+68
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/rad2deg

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
Title: '.rad2deg()'
3+
Description: 'Converts the elements of a tensor from radians to degrees.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Functions'
9+
- 'PyTorch'
10+
- 'Tensor'
11+
- 'Trigonometry'
12+
CatalogContent:
13+
- 'intro-to-py-torch-and-neural-networks'
14+
- 'paths/data-science'
15+
---
16+
17+
The **`.rad2deg()`** function in PyTorch converts the elements of a tensor from radians to degrees. This is useful for trigonometric and rotational operations when working with angular measurements. The operation is performed element-wise, meaning it applies the conversion formula ($$\text{degrees} = \text{radians} \times \frac{180}{\pi}$$) to every value in the tensor, returning a new tensor with the results.
18+
19+
This function is available for tensors with floating-point data types such as `torch.float16`, `torch.float32`, or `torch.float64`.
20+
21+
## Syntax
22+
23+
```pseudo
24+
torch.rad2deg(input)
25+
```
26+
27+
**Parameters:**
28+
29+
- `input` (Tensor): A tensor containing angular values in radians.
30+
31+
**Return value:**
32+
33+
Returns a tensor containing the degree equivalents of the original tensor's elements. The returned tensor has the same size and data type as the original input tensor.
34+
35+
## Example
36+
37+
This example demonstrates how to use `.rad2deg()` to convert common angular values ($\pi$ radians and $\frac{\pi}{2}$ radians) into degrees:
38+
39+
```py
40+
import torch
41+
import math
42+
43+
# Create a tensor with values in radians
44+
radians_tensor = torch.tensor([
45+
math.pi / 2,
46+
math.pi,
47+
0.0
48+
])
49+
50+
print("Original Tensor (Radians):")
51+
print(radians_tensor)
52+
53+
# Convert radians to degrees using torch.rad2deg()
54+
degrees_tensor = torch.rad2deg(radians_tensor)
55+
56+
print("\nConverted Tensor (Degrees):")
57+
print(degrees_tensor)
58+
```
59+
60+
The output of this code is:
61+
62+
```shell
63+
Original Tensor (Radians):
64+
tensor([1.5708, 3.1416, 0.0000])
65+
66+
Converted Tensor (Degrees):
67+
tensor([ 90.0000, 180.0000, 0.0000])
68+
```

0 commit comments

Comments
 (0)