📜  Python PyTorch 余数()方法

📅  最后修改于: 2022-05-13 01:54:40.165000             🧑  作者: Mango

Python PyTorch 余数()方法

PyTorch 的剩余()方法计算除法运算的元素余数(除以除数)。被除数是张量,而除数可以是数字或张量。此方法应用模运算,如果结果的符号与除数不同,则将除数添加到模结果中。此方法仅支持整数和浮点值输入。以下是此方法的语法 -

让我们借助一些Python示例来了解torch.remainder()方法。

示例 1:

在下面的Python示例中,我们计算火炬张量除以数字时的余数。

这里,-13 除以 5,余数是 2。如何? mod(-13, 5) = -3,然后 -3+5 = 2。当模值与除数不同时,将除数加到模上。请注意当除数为 -5 时余数有何不同。

Python3
# Python 3 program to demonstrate the 
# torch.remainder() method
# importing torch
import torch
  
# define the dividend
x = torch.tensor([5, -13, 24, -7, 7])
print("Dividend:", x)
  
# define the divisor
y = 5
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)
z = -5
print("Divisor:",z)
remainder = torch.remainder(x,z)
print("Remainder:",remainder)


Python3
# Python 3 program to demonstrate the 
# torch.remainder() method
# importing torch
import torch
  
# define the dividend
x = torch.tensor([15, -13, 15, -15, 0])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([7, 7, -7, -7, 7])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)


Python3
# Python 3 program to demonstrate the 
# torch.remainder() method for float values
# importing torch
import torch
  
# define the dividend
x = torch.tensor([15., -13., 15., -15., 0])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([7., 7., -7., -7., 7.])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)


Python3
# Python 3 program to demonstrate the 
# torch.remainder() method
# importing torch
import torch
import numpy as np
  
# define the dividend
x = torch.tensor([15., -13., 0., -15., 0])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([0., np.inf, 0., 0., np.inf])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)


Python3
# Python 3 program to demonstrate the 
# torch.remainder() method
# importing torch
import torch
import numpy as np
  
# define the dividend
x = torch.tensor([15])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([0])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)


输出:

Dividend: tensor([  5, -13,  24,  -7,   7])
Divisor: 5
Remainder: tensor([0, 2, 4, 3, 2])
Divisor: -5
Remainder: tensor([ 0, -3, -1, -2, -3])

示例 2:

在下面的Python示例中,当被除数和除数都是火炬张量时,我们计算元素余数。

Python3

# Python 3 program to demonstrate the 
# torch.remainder() method
# importing torch
import torch
  
# define the dividend
x = torch.tensor([15, -13, 15, -15, 0])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([7, 7, -7, -7, 7])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)

输出:

Dividend: tensor([ 15, -13,  15, -15,   0])
Divisor: tensor([ 7,  7, -7, -7,  7])
Remainder: tensor([ 1,  1, -6, -1,  0])

示例 3:

在下面的示例中,我们像示例 2 中一样找到余数,但对于浮点张量。

Python3

# Python 3 program to demonstrate the 
# torch.remainder() method for float values
# importing torch
import torch
  
# define the dividend
x = torch.tensor([15., -13., 15., -15., 0])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([7., 7., -7., -7., 7.])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)

输出:

Dividend: tensor([ 15., -13.,  15., -15.,   0.])
Divisor: tensor([ 7.,  7., -7., -7.,  7.])
Remainder: tensor([ 1.,  1., -6., -1.,  0.])

示例 4:

在下面的示例中,尝试找到除以零或无穷大时的余数。

请注意,当除数为零时,无论被除数如何,余数都是 nan(非数字)。当非零除以无穷大时,余数为无穷大,但当零除以无穷大时,余数为 0。还要注意两个张量都是浮点张量。请参阅下一个整数除以零的示例。

Python3

# Python 3 program to demonstrate the 
# torch.remainder() method
# importing torch
import torch
import numpy as np
  
# define the dividend
x = torch.tensor([15., -13., 0., -15., 0])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([0., np.inf, 0., 0., np.inf])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)

输出:

Dividend: tensor([ 15., -13.,   0., -15.,   0.])
Divisor: tensor([0., inf, 0., 0., inf])
Remainder: tensor([nan, inf, nan, nan, 0.])

示例 5:

在此示例中,我们尝试找到整数除以零时的余数。

请注意,在整数除数的情况下,它会引发运行时错误,而在浮点除数的情况下,它会将余数返回为 nan(如示例 4 中所示)。

Python3

# Python 3 program to demonstrate the 
# torch.remainder() method
# importing torch
import torch
import numpy as np
  
# define the dividend
x = torch.tensor([15])
print("Dividend:", x)
  
# define the divisor
y = torch.tensor([0])
print("Divisor:",y)
  
# compute the remainder
remainder = torch.remainder(x,y)
print("Remainder:",remainder)

输出:

Dividend: tensor([15])
Divisor: tensor([0])


RuntimeError: ZeroDivisionError