快捷方式

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(),向零舍入。

引數
  • input (Tensor) – 輸入 tensor。

  • decimals (int) – 舍入到的小數位數(預設值:0)。如果 decimals 是負數,則表示小數點左側的位數。

關鍵字引數

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.])

文件

查閱 PyTorch 全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源