评价此页

torch.remainder#

torch.remainder(input, other, *, out=None) Tensor#

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

它也可以通过除法来定义,其中除法向零舍入:

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

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

注意

不支持复数输入。在某些情况下,从数学上讲不可能满足复数模运算的定义。有关除以零的处理方式,请参阅torch.fmod()

另请参阅

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

参数
  • input (TensorScalar) – 被除数

  • other (TensorScalar) – 除数

关键字参数

out (Tensor, optional) – 输出张量。

示例

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