TUTORIALAI 与数据科学FETCHED
PyTorch torch.round 函数
PyTorch torch.round 函数 Pytorch torch 参考手册 torch.round 是 PyTorch 中用于逐元素四舍五入的函数。 函数定义 torch.round(input, decimals, out) 使用示例 实例 [mycode4 type='python'] import torch x = torch.tensor([0.1, 0.5, 0.9, 1.2..
更新于 2026/7/16
教程详情页默认 noindex。
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))
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])
Pytorch torch 参考手册