評價此頁

torch.remainder#

torch.remainder(input, other, *, out=None) Tensor#

逐元素計算Python 的模運算。結果的符號與除數 other 相同,且其絕對值小於 other

它也可以用 torch.div() 來定義,如下所示:

torch.remainder(a, b) == a - a.div(b, rounding_mode="floor") * b

支援廣播到共同形狀型別提升,以及整數和浮點數輸入。

注意

不支援複數輸入。在某些情況下,用複數滿足模運算的定義在數學上是不可能的。有關除以零的處理方式,請參閱 torch.fmod()

另請參閱

torch.fmod() 實現的是 C++ 的 std::fmod。而 torch.remainder 是基於向零舍入的除法定義的。

引數
  • input (TensorScalar) – 被除數

  • other (TensorScalar) – 除數

關鍵字引數

out (Tensor, optional) – 輸出張量。

示例

>>> torch.remainder(torch.tensor([-3., -2, -1, 1, 2, 3]), 2)
tensor([ 1.,  0.,  1.,  1.,  0.,  1.])
>>> torch.remainder(torch.tensor([1, 2, 3, 4, 5]), -1.5)
tensor([ -0.5000, -1.0000,  0.0000, -0.5000, -1.0000 ])