快捷方式

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 (Tensor or Scalar) – 除數

關鍵字引數

out (Tensor, optional) – 輸出 Tensor。

示例

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

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

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

檢視資源