PyTorch torch.abs 函数

约 235 字 更新于 2026-11-18 5iyo.com

PyTorch torch.abs 函数

torch.abs 是 PyTorch 中用于计算张量绝对值的函数。

函数定义


torch.abs(input, out=None)


使用示例

实例

import torch

# 创建包含负数的张量
x = torch.tensor([-1, -2, 3, -4])

# 计算绝对值
y = torch.abs(x)
print(y)

输出结果为:


tensor([1, 2, 3, 4])