torch.round(input, *, decimals=0, out=None)
函数作用
将输入元素舍入为最接近的整数。
注意⚠️:
- 当一个数字与两个整数等距时(例如 round(2.5) 是 2),此函数实现“
舍入一半到偶数
”以打破平局。(zero is treated as even)
- 参数 decimals :
可正可负
。 要舍入的小数位数(默认值:0)。如果小数为负数,则指定小数点左侧的位数。
举例
# 默认decimals=0
>>> torch.round(torch.tensor((4.7, -2.3, 9.1, -7.7)))
tensor([ 5., -2., 9., -8.])
>>> # Values equidistant from two integers are rounded towards the
>>> # the nearest even value (zero is treated as even)
>>> torch.round(torch.tensor([-0.5, 0.5, 1.5, 2.5]))
tensor([-0., 0., 2., 2.])
>>> # A positive decimals argument rounds to the to that decimal place
>>> torch.round(torch.tensor([0.1234567]), decimals=3)
tensor([0.1230])
>>> # A negative decimals argument rounds to the left of the decimal
>>> torch.round(torch.tensor([1200.1234567]), decimals=-3)
tensor([1000.])