torch.round¶
- torch.round(input, *, decimals=0, out=None) Tensor¶
將
input中的元素舍入到最接近的整數。對於整數輸入,遵循 array-api 約定,返回輸入 tensor 的副本。輸出的返回型別與輸入的 dtype 相同。
注意
此函式實現“舍半取偶”規則來處理當一個數與兩個整數等距時的情況(例如 round(2.5) 為 2)。
當指定了 :attr:`decimals` 引數時,使用的演算法類似於 NumPy 的 around。此演算法速度快但不精確,對於低精度 dtypes 容易溢位。例如,round(tensor([10000], dtype=torch.float16), decimals=3) 的結果是 inf。
另請參閱
torch.ceil(),向上舍入。torch.floor(),向下舍入。torch.trunc(),向零舍入。- 引數
- 關鍵字引數
out (Tensor, 可選) – 輸出 tensor。
示例
>>> 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.])