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