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