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 的模數運算符。此運算符使用向下捨入結果的除法來定義。範例
>>> 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])