PyTorch .deg2rad()

Anonymous contributor's avatar
Anonymous contributor
Published Jul 15, 2025
Contribute to Docs

In PyTorch, the .deg2rad() function converts angles from degrees to radians. This is especially useful when using trigonometric functions like torch.sin() or torch.cos(), which require input in radians.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
    • Intermediate.
      3 hours

Syntax

torch.deg2rad(input, *, out=None)

Parameters:

  • input: The input tensor containing angles in degrees.
  • out (optional): The output tensor to store the result. If provided, the result is written to this tensor in-place.

Return value:

A tensor with the same shape as input, where each element is converted from degrees to radians. If out is specified, the returned tensor is the same as out.

Example

This example demonstrates the usage of the .deg2rad() function:

import torch
# Define a tensor with angles in degrees
degrees = torch.tensor([0.0, 90.0, 180.0, 270.0])
# Convert to radians
radians = torch.deg2rad(degrees)
# Print the result
print(radians)

Here is the output:

tensor([0.0000, 1.5708, 3.1416, 4.7124])

All contributors

Contribute to Docs

Learn PyTorch on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
    • Intermediate.
      3 hours