torch.remainder
- torch.remainder(input, other, *, out=None) → Tensor
-
计算Python的取模运算的逐元素操作。结果与除数
other
具有相同的符号,并且其绝对值小于other
的绝对值。它也可以使用
torch.div()
来定义torch.remainder(a, b) == a - a.div(b, rounding_mode="floor") * b
注意
不支持复数输入。在某些情况下,复数无法满足取模运算的数学定义。有关除以零的情况,请参见
torch.fmod()
。参见
torch.fmod()
实现了 C++ 中的 std::fmod 函数。这个函数定义基于向零取整的除法运算。示例:
>>> torch.remainder(torch.tensor([-3., -2, -1, 1, 2, 3]), 2) tensor([ 1., 0., 1., 1., 0., 1.]) >>> torch.remainder(torch.tensor([1, 2, 3, 4, 5]), -1.5) tensor([ -0.5000, -1.0000, 0.0000, -0.5000, -1.0000 ])