Shortcuts

torch.remainder

torch.remainder(input, other, *, out=None) 张量

计算 Python的模运算 逐元素进行。结果的符号与除数other相同,其绝对值 小于other的绝对值。

它也可以根据torch.div()定义为

torch.remainder(a, b) == a - a.div(b, rounding_mode="floor") * b

支持广播到通用形状类型提升以及整数和浮点输入。

注意

不支持复数输入。在某些情况下,使用复数无法满足取模运算的数学定义。有关如何处理除以零的情况,请参见torch.fmod()

另请参阅

torch.fmod() 实现了 C++ 的 std::fmod。 这个函数是基于向零舍入的除法定义的。

Parameters
  • 输入 (张量标量) – 被除数

  • 其他 (Tensor标量) – 除数

Keyword Arguments

输出 (张量, 可选) – 输出张量。

示例:

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