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 的模运算符。此函数定义为使用向下取整除法。示例
>>> 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])