torch.round#
- torch.round(input, *, decimals=0, out=None) Tensor#
將
input的元素四捨五入到最近的整數。對於整數輸入,遵循 array-api 的約定,返回輸入張量的副本。輸出的返回型別與輸入的 dtype 相同。
注意
此函式實現“四捨五入到偶數”的規則,以打破數字距離兩個整數相等時的平局(例如,round(2.5) 是 2)。
當指定 :attr:`decimals` 引數時,所使用的演算法類似於 NumPy 的 around。該演算法速度快但精度不高,並且對於低精度 dtype 很容易溢位。例如,round(tensor([10000], dtype=torch.float16), decimals=3) 是 inf。
另請參閱
torch.ceil(),向上取整。torch.floor(),向下取整。torch.trunc(),向零取整。- 引數
- 關鍵字引數
out (Tensor, optional) – 輸出張量。
示例
>>> 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.])