PyTorch torch.absolute 函数
torch.absolute 是 PyTorch 中用于计算逐元素绝对值的函数,与 torch.abs 功能相同。
函数定义
torch.absolute(input, out=None)
使用示例
实例
import torch
# 创建包含负数的张量
x = torch.tensor([-1.0, -2.0, 3.0, -4.0])
# 计算绝对值
result = torch.absolute(x)
print(result)
# 创建包含负数的张量
x = torch.tensor([-1.0, -2.0, 3.0, -4.0])
# 计算绝对值
result = torch.absolute(x)
print(result)
输出结果为:
tensor([1., 2., 3., 4.])

Pytorch torch 参考手册