PyTorch torch.round 函数

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

PyTorch torch.round 函数


torch.round 是 PyTorch 中用于逐元素四舍五入的函数。

函数定义

torch.round(input, decimals, out)

使用示例

实例

import torch

x = torch.tensor([0.1, 0.5, 0.9, 1.2, 1.5])

print(torch.round(x))

# 指定小数位数
y = torch.tensor([1.234, 5.678])
print(torch.round(y, decimals=2))

输出结果为:

tensor([0., 0., 1., 1., 2.])
tensor([1.2300, 5.6800])