PyTorch torch.atan2 函数
torch.atan2 是 PyTorch 中用于计算逐元素二维反正切(arctangent2)的函数。它根据两个坐标值 (y, x) 计算角度,返回值的单位为弧度,范围在 [-pi, pi] 之间。
函数定义
torch.atan2(input, other, out=None)
参数说明
input: 分子张量(y坐标)other: 分母张量(x坐标)out: 输出张量(可选)
使用示例
实例
import torch
# 创建张量
y = torch.tensor([0.0, 1.0, -1.0, 1.0])
x = torch.tensor([1.0, 0.0, 0.0, -1.0])
# 计算二维反正切
result = torch.atan2(y, x)
print(result)
# 创建张量
y = torch.tensor([0.0, 1.0, -1.0, 1.0])
x = torch.tensor([1.0, 0.0, 0.0, -1.0])
# 计算二维反正切
result = torch.atan2(y, x)
print(result)
输出结果为:
tensor([ 0.0000, 1.5708, -1.5708, 2.3562])

Pytorch torch 参考手册