PyTorch .rad2deg()
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.
This function is available for tensors with floating-point data types such as torch.float16, torch.float32, or torch.float64.
Syntax
torch.rad2deg(input)
Parameters:
input(Tensor): A tensor containing angular values in radians.
Return value:
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.
Example
This example demonstrates how to use .rad2deg() to convert common angular values ($\pi$ radians and $\frac{\pi}{2}$ radians) into degrees:
import torchimport math# Create a tensor with values in radiansradians_tensor = torch.tensor([math.pi / 2,math.pi,0.0])print("Original Tensor (Radians):")print(radians_tensor)# Convert radians to degrees using torch.rad2deg()degrees_tensor = torch.rad2deg(radians_tensor)print("\nConverted Tensor (Degrees):")print(degrees_tensor)
The output of this code is:
Original Tensor (Radians):tensor([1.5708, 3.1416, 0.0000])Converted Tensor (Degrees):tensor([ 90.0000, 180.0000, 0.0000])
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours