評價此頁

torch.fmod#

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

逐元素應用 C++ 的 std::fmod。結果的符號與被除數 input 相同,其絕對值小於 other

此函式可以定義為使用 torch.div() 來實現

torch.fmod(a, b) == a - a.div(b, rounding_mode="trunc") * b

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

注意

當除數為零時,對於 CPU 和 GPU 上的浮點資料型別,返回 NaN;對於 CPU 上的整數除零,會引發 RuntimeError;GPU 上的整數除零可能會返回任何值。

注意

不支援複數輸入。在某些情況下,在數學上不可能滿足複數模運算的定義。

另請參閱

torch.remainder() 實現的是 Python 的模運算子。而本函式是透過向下取整結果的除法來定義的。

引數
  • input (Tensor) – 被除數

  • other (TensorScalar) – 除數

關鍵字引數

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

示例

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