| by Arround The Web | No comments

PyTorch – deg2rad()

PyTorch is an open-source framework for the Python programming language.

Tensor is a multidimensional array that is used to store data. So 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 data is a multi-dimensional array.

torch.deg2rad()

deg2rad() in PyTorch converts the given degrees in a tensor to radians.

It takes one parameter.

Syntax:
torch.deg2rad(tensor_object)

Parameter:
tensor_object is a tensor.

Return:
It will return a new tensor with degrees.

Example 1:
In this example, we will create a tensor with one dimension that has five elements and convert it into radians.

#first import the torch module
import torch
 
#create a 1D tensor
data1 = torch.tensor([180,90,270,0,360])
 
#display
print("Actual degrees present in the Tensor: ")
print(data1)
 
print("Radians")
print(torch.deg2rad(data1))

Output:

Actual degrees present in the Tensor:
tensor([180,  90, 270,   0, 360])
Radians
tensor([3.1416, 1.5708, 4.7124, 0.0000, 6.2832])

Here:

  1. 180 degrees is equal to 3.1416 radians.
  2. 90 degrees is equal to 1.5708 radians.
  3. 270 degrees is equal to 4.7124 radians.
  4. 0 degrees is equal to 0.0000 radians.
  5. 360 degrees is equal to 6.2832 radians.

Example 2:
In this example, we will create a tensor with two dimensions that has five elements in each row and convert them into degrees.

#first import the torch module
import torch
 
#create a 2D tensor
data1 = torch.tensor([[180,90,270,0,360],[720,120,240,670,560]])
 
#display
print("Actual degrees present in the Tensor: ")
print(data1)
 
print("Radians")
print(torch.deg2rad(data1))

Output:

Actual degrees present in the Tensor:
tensor([[180,  90, 270,   0, 360],
        [720, 120, 240, 670, 560]])
Radians
tensor([[ 3.1416,  1.5708,  4.7124,  0.0000,  6.2832],
        [12.5664,  2.0944,  4.1888, 11.6937,  9.7738]])

Here:

  1. 180 degrees is equal to 3.1416 radians, and 720 degrees is equal to 12.5664 radians.
  2. 90 degrees is equal to 1.5708 radians, and 120 degrees is equal to2.0944 radians.
  3. 270 degrees is equal to 4.7124 radians, and 240 degrees is equal to 4.1888 radians.
  4. 0 degrees is equal to 0.0000 radians, and 670 degrees is equal to 11.6937 radians.
  5. 360 degrees is equal to 6.2832 radians, and 560 degrees is equal to 9.7738 radians.

Work with CPU

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

At this time, when we are creating a tensor, 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 five elements on the CPU and convert it into radians.

#first import the torch module
import torch
 
#create a 1D tensor
data1 = torch.tensor([180,90,270,0,360]).cpu()
 
#display
print("Actual degrees present in the Tensor: ")
print(data1)
 
print("Radians")
print(torch.deg2rad(data1))

Output:

Actual degrees present in the Tensor:
tensor([180,  90, 270,   0, 360])
Radians
tensor([3.1416, 1.5708, 4.7124, 0.0000, 6.2832])

Here:

  1. 180 degrees is equal to 3.1416 radians.
  2. 90 degrees is equal to 1.5708 radians.
  3. 270 degrees is equal to 4.7124 radians.
  4. 0 degrees is equal to 0.0000 radians.
  5. 360 degrees is equal to 6.2832 radians.

Example 2:
In this example, we will create a tensor with two dimensions that has five elements on the CPU in each row and convert them into degrees.

#first import the torch module
import torch
 
#create a 2D tensor
data1 = torch.tensor([[180,90,270,0,360],[720,120,240,670,560]]).cpu()
 
#display
print("Actual degrees present in the Tensor: ")
print(data1)
 
print("Radians")
print(torch.deg2rad(data1))

Output:

Actual degrees present in the Tensor:
tensor([[180,  90, 270,   0, 360],
        [720, 120, 240, 670, 560]])
Radians
tensor([[ 3.1416,  1.5708,  4.7124,  0.0000,  6.2832],
        [12.5664,  2.0944,  4.1888, 11.6937,  9.7738]])

Here:

  1. 180 degrees is equal to 3.1416 radians, and 720 degrees is equal to 12.5664 radians.
  2. 90 degrees is equal to 1.5708 radians, and 120 degrees is equal to2.0944 radians.
  3. 270 degrees is equal to 4.7124 radians, and 240 degrees is equal to 4.1888 radians.
  4. 0 degrees is equal to 0.0000 radians, and 670 degrees is equal to 11.6937 radians.
  5. 360 degrees is equal to 6.2832 radians, and 560 degrees is equal to 9.7738 radians.

Conclusion

In this PyTorch lesson, we discussed deg2rad(). It converts the given degrees of tensor to radians. We also ran the tensor on the CPU by considering two examples.

Share Button

Source: linuxhint.com

Leave a Reply