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 函数。这个函数定义基于向零取整的除法运算。

参数
  • 输入 (Tensor标量) – 被除数

  • other (TensorScalar) – 除数

关键字参数

out (Tensor, 可选) – 指定输出张量。

示例:

>>> 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 ])
本页目录