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 上整数除以零可能返回任何值。注意
复杂输入不受支持。在某些情况下,使用复数无法满足模运算的定义。
参见
实现 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])