快捷方式

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 上的浮點數 dtype,會回傳 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])

文件

獲取 PyTorch 的全面開發人員文件

查看文件

教程

獲取初學者和高級開發人員的深入教程

查看教程

資源

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

查看資源