📜  Python – PyTorch atan2() 方法

📅  最后修改于: 2022-05-13 01:54:51.687000             🧑  作者: Mango

Python – PyTorch atan2() 方法

PyTorch atan2() 方法计算(y/x) 的逐元素反正切,其中 y, x 分别是点的 y 坐标和 x 坐标的张量。 (y/x) 的反正切是正 x 轴与从 (0,0) 到 (x,y) 的直线之间的角度。所以atan2()方法计算这些角度。这些角度以弧度为单位,在[-pi, pi]范围内。这些角度是使用点的象限计算的。可以使用输入张量元素的符号返回象限。在数学上,它也可以称为 2 参数反正切 (atan2)。

torch.atan2()函数:

示例 1:

在这个例子中,我们为两个输入张量计算元素方式的 2 参数反正切值,这里的 2 参数反正切值是按元素计算的。

Python3
# Python 3 program to demonstrate torch.atan2() method
# importing torch
import torch
  
# defining first tensor (y-coordinates)
y = torch.tensor([0., 40., -137., -30.])
  
# defining second tensor (x-coordinates)
x = torch.tensor([120., -4., -70., 23.])
  
# printing the two tensors
print('Tensor y:', y)
print('Tensor x:', x)
  
# computing the 2-argument arc tangent
result = torch.atan2(y, x)
print('atan2(y,x):', result)


Python3
# Python3 program to demonstrate torch.atan2() method
%matplotlib qt 
  
# importing required libraries
import torch
import numpy as np
import matplotlib.pyplot as plt
  
# defining first tensor (y-coordinates)
b = np.array([-8,-3,-2,-1,0,1,2,3,4])
  
# defining second tensor (x-coordinates)
a = np.array([1,1,1,1,1,1,1,1,1])
y = torch.tensor(b)
x = torch.tensor(a)
print('Tensor y:\n', y)
print('Tensor x:\n', x)
  
# computing the 2-argument arc tangent
result = torch.atan2(y,x)
print('atan2:\n', result)
  
# tensor to numpy array
result = result.numpy()
  
# plot the result using matplotlib
plt.plot(b/a, result, color='r', label='atan2')
plt.xlabel("y/x")
plt.ylabel("atan2")
plt.title("2D atan2 plot GFG")
plt.show()


Python3
# Python3 program to demonstrate torch.atan2() method
%matplotlib qt 
  
# importing required libraries
import torch
import numpy as np
import matplotlib.pyplot as plt
  
# defining first tensor (y-coordinates)
b = np.arange(25,74, 1)
y = torch.tensor(b)
  
# defining second tensor (x-coordinates)
a = np.arange(1,50, 1)
x = torch.tensor(a)
print('Tensor y:\n', y)
print('Tensor x:\n', x)
  
# computing the 2-argument arc tangent
result = torch.atan2(y,x)
print('atan2 values:\n', result)
  
# converting the result tensor to numpy array
result = result.numpy()
  
# plot the result as 3D using matplotlib
fig = plt.figure()
  
# syntax for 3-D projection
ax = plt.axes(projection ='3d')
ax.plot3D(a, b, result, 'green')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('atan2')
ax.set_title('3D atan2() plot GFG')
plt.show()


输出:

Tensor y: tensor([   0.,   40., -137.,  -30.])
Tensor x: tensor([120.,  -4., -70.,  23.])
atan2(y,x): tensor([ 0.0000,  1.6705, -2.0432, -0.9167])

示例 2:

在下面的示例中,我们计算元素方式的 2 参数反正切,并使用 Matplotlib 将结果可视化为 2D 图。在这里,我们在“y/x”值和“atan2()”之间绘制了一个二维图。 “y/x”值绘制在 x 轴上,“atan2()”值绘制在 y 轴上。请注意,增加 atan2() 的值会增加。

Python3

# Python3 program to demonstrate torch.atan2() method
%matplotlib qt 
  
# importing required libraries
import torch
import numpy as np
import matplotlib.pyplot as plt
  
# defining first tensor (y-coordinates)
b = np.array([-8,-3,-2,-1,0,1,2,3,4])
  
# defining second tensor (x-coordinates)
a = np.array([1,1,1,1,1,1,1,1,1])
y = torch.tensor(b)
x = torch.tensor(a)
print('Tensor y:\n', y)
print('Tensor x:\n', x)
  
# computing the 2-argument arc tangent
result = torch.atan2(y,x)
print('atan2:\n', result)
  
# tensor to numpy array
result = result.numpy()
  
# plot the result using matplotlib
plt.plot(b/a, result, color='r', label='atan2')
plt.xlabel("y/x")
plt.ylabel("atan2")
plt.title("2D atan2 plot GFG")
plt.show()

输出:

示例 3:

在下面的示例中,我们计算元素方式的 2 参数反正切,并使用 Matplotlib 将结果可视化为 3D 图。在这里,我们在“x”、“y”和“atan2()”之间绘制了一个 3D 图。 “x”值绘制在 x 轴上,“y”值绘制在 y 轴上,“atan2()”值绘制在 z 轴上。注意 atan2() 与 x 和 y 的关系。

Python3

# Python3 program to demonstrate torch.atan2() method
%matplotlib qt 
  
# importing required libraries
import torch
import numpy as np
import matplotlib.pyplot as plt
  
# defining first tensor (y-coordinates)
b = np.arange(25,74, 1)
y = torch.tensor(b)
  
# defining second tensor (x-coordinates)
a = np.arange(1,50, 1)
x = torch.tensor(a)
print('Tensor y:\n', y)
print('Tensor x:\n', x)
  
# computing the 2-argument arc tangent
result = torch.atan2(y,x)
print('atan2 values:\n', result)
  
# converting the result tensor to numpy array
result = result.numpy()
  
# plot the result as 3D using matplotlib
fig = plt.figure()
  
# syntax for 3-D projection
ax = plt.axes(projection ='3d')
ax.plot3D(a, b, result, 'green')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('atan2')
ax.set_title('3D atan2() plot GFG')
plt.show()

输出: