快捷方式

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 上的浮点 dtype,返回 NaN ;对于 CPU 上的整数除以零,引发 RuntimeError ;GPU 上的整数除以零可能返回任何值。

注意

不支持复数输入。在某些情况下,数学上不可能满足复数模运算的定义。

另请参阅

torch.remainder(),它实现了 Python 的模运算符。这个运算符定义为使用向下取整的除法。

参数
  • input (Tensor) – 被除数

  • other (TensorScalar) – 除数

关键字参数

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

示例

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

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得解答

查看资源