torch.fmod#
- torch.fmod(input, other, *, out=None) Tensor #
逐元素应用 C++ 的 std::fmod。结果的符号与被除数
input
相同,且其绝对值小于other
。此函数可以根据以下公式定义:
torch.fmod(a, b) == a - a.div(b, rounding_mode="trunc") * b
注意
当除数为零时,浮点数 dtype 在 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])