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()函数:
Syntax: torch.atan2(input, other, out=None)
Parameters:
- input: the first input tensor (y-coordinates).
- other: the second input tensor (x coordinates).
- out: the output tensor, a keyword argument.
Return: It returns a new tensor with element-wise arctangent of (input/other).
示例 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()
输出:
Tensor y:
tensor([-8, -3, -2, -1, 0, 1, 2, 3, 4], dtype=torch.int32)
Tensor x:
tensor([1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=torch.int32)
atan2:
tensor([-1.4464, -1.2490, -1.1071, -0.7854, 0.0000, 0.7854, 1.1071, 1.2490,
1.3258])
示例 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()
输出:
Tensor y:
tensor([25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73], dtype=torch.int32)
Tensor x:
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49], dtype=torch.int32)
atan2 values:
tensor([1.5308, 1.4940, 1.4601, 1.4289, 1.4001, 1.3734, 1.3487, 1.3258, 1.3045,
1.2847, 1.2663, 1.2490, 1.2329, 1.2178, 1.2036, 1.1903, 1.1777, 1.1659,
1.1547, 1.1442, 1.1342, 1.1247, 1.1157, 1.1071, 1.0990, 1.0913, 1.0839,
1.0769, 1.0701, 1.0637, 1.0575, 1.0517, 1.0460, 1.0406, 1.0354, 1.0304,
1.0256, 1.0209, 1.0165, 1.0122, 1.0081, 1.0041, 1.0002, 0.9965, 0.9929,
0.9894, 0.9861, 0.9828, 0.9796])