Python

PyTorch – Rad2deg()

We will discuss how to convert the radians to degrees using the rad2deg() method in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

Torch.rad2deg()

Rad2deg() in PyTorch converts the given radians in tensor to degrees. It takes one parameter.

Syntax:

torch.rad2deg(tensor_object)

Parameter:

tensor_object is a tensor

Return:

It returns a new tensor with degrees.

Example 1:

In this example, we will create a tensor with one dimension that has 5 elements and convert it into degrees.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34,5.67,8.90,4.56,7.43])

 

#display

print("Actual radians present in the Tensor: ")

print(data1)

 

print("Degrees")

print(torch.rad2deg(data1))

Output:

Actual radians present in the Tensor:

tensor([1.3400, 5.6700, 8.9000, 4.5600, 7.4300])

Degrees

tensor([ 76.7763, 324.8671, 509.9324, 261.2688, 425.7076])

Here:

1. The 1.3400 radians is equal to 76.7763 degrees.

2. The 5.6700 radians is equal to 324.8671 degrees.

3. The8.9000 radians is equal to 509.9324 degrees.

4. The 4.5600 radians is equal to 261.2688 degrees.

5. The 7.4300 radians is equal to 425.7076 degrees.

Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements in each row and convert them into radians.

#first import the torch module

import torch

 

#create a 2D tensor

data1 = torch.tensor([[1.34,5.67,8.90,4.56,7.43],[1,2,3,4,5]])

 

#display

print("Actual radians present in the Tensor: ")

print(data1)

 

print("Degrees")

print(torch.rad2deg(data1))

Output:

Actual radians present in the Tensor:

tensor([[1.3400, 5.6700, 8.9000, 4.5600, 7.4300],

[1.0000, 2.0000, 3.0000, 4.0000, 5.0000]])

Degrees

tensor([[ 76.7763, 324.8671, 509.9324, 261.2688, 425.7076],

[ 57.2958, 114.5916, 171.8873, 229.1831, 286.4789]])

Here:

1. The 1.3400 radians is equal to 76.7763 degrees, 1 radians is equal to 57.2958 degrees.

2. The 5.6700 radians is equal to 324.8671 degrees, 2 radians is equal to 114.5916 degrees.

3. The 8.9000 radians is equal to 509.9324 degrees, 3 radians is equal to 171.8873 degrees.

4. The 4.5600 radians is equal to 261.2688 degrees, 4 radians is equal to 229.1831 degrees.

5. The 7.4300 radians is equal to 425.7076 degrees, 5 radians is equal to 286.4789 degrees.

Work with CPU

If you want to run a rad2deg() function on the CPU, we have to create a tensor with a cpu() function. This will run on a CPU machine.

When we create a tensor, this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example 1:

In this example, we will create a tensor with one dimension that has 5 elements on the cpu and convert it into degrees.

#first import the torch module

import torch

 

#create a 1D tensor

data1 = torch.tensor([1.34,5.67,8.90,4.56,7.43]).cpu()

 

#display

print("Actual radians present in the Tensor: ")

print(data1)

 

print("Degrees")

print(torch.rad2deg(data1))

Output:

Actual radians present in the Tensor:

tensor([1.3400, 5.6700, 8.9000, 4.5600, 7.4300])

Degrees

tensor([ 76.7763, 324.8671, 509.9324, 261.2688, 425.7076])

Here:

1. The 1.3400 radians is equal to 76.7763 degrees.

2. The 5.6700 radians is equal to 324.8671 degrees.

3. The 8.9000 radians is equal to 509.9324 degrees.

4. The 4.5600 radians is equal to 261.2688 degrees.

5. The 7.4300 radians is equal to 425.7076 degrees.

Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements on the cpu in each row and convert them into radians.

#first import the torch module

import torch

 

#create a 2D tensor

data1 = torch.tensor([[1.34,5.67,8.90,4.56,7.43],[1,2,3,4,5]]).cpu()

 

#display

print("Actual radians present in the Tensor: ")

print(data1)

 

print("Degrees")

print(torch.rad2deg(data1))

Output:

Actual radians present in the Tensor:

tensor([[1.3400, 5.6700, 8.9000, 4.5600, 7.4300],

[1.0000, 2.0000, 3.0000, 4.0000, 5.0000]])

Degrees

tensor([[ 76.7763, 324.8671, 509.9324, 261.2688, 425.7076],

[ 57.2958, 114.5916, 171.8873, 229.1831, 286.4789]])

Here:

1. The 1.3400 radians is equal to 76.7763 degrees, 1 radians is equal to 57.2958 degrees.

2. The 5.6700 radians is equal to 324.8671 degrees, 2 radians is equal to 114.5916 degrees.

3. The 8.9000 radians is equal to 509.9324 degrees, 3 radians is equal to 171.8873 degrees.

4. The 4.5600 radians is equal to 261.2688 degrees, 4 radians is equal to 229.1831 degrees.

5. The 7.4300 radians is equal to 425.7076 degrees, 5 radians is equal to 286.4789 degrees.

Conclusion

In this PyTorch lesson, we discussed about the rad2deg() function. It converts the given radians in tensor to degrees. We also run the tensor on the cpu by considering the two examples.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain