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