TUTORIALAI 与数据科学FETCHED
PyTorch torch.abs 函数
PyTorch torch.abs 函数 Pytorch torch 参考手册 torch.abs 是 PyTorch 中用于计算张量绝对值的函数。 函数定义 torch.abs(input, out=None) 使用示例 实例 [mycode4 type='python'] import torch # 创建包含负数的张量 x = torch.tensor([-1, -2, 3, -4]) ..
更新于 2026/7/16
教程详情页默认 noindex。
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)
# 创建包含负数的张量
x = torch.tensor([-1, -2, 3, -4])
# 计算绝对值
y = torch.abs(x)
print(y)
输出结果为:
tensor([1, 2, 3, 4])
Pytorch torch 参考手册