Open In App

Python PyTorch remainder() method

Improve
Improve
Like Article
Like
Save
Share
Report

PyTorch remainder() method computes the element-wise remainder of the division operation (dividend is divided by divisor). The dividend is a tensor whereas the divisor may be a number or tensor. This method applies the modulus operation and if the sign of the result is different from the divisor then the divisor is added to the modulus result. This method supports only integer and float valued inputs. Following is the syntax for this method-

Syntax: torch.remainder(input, other, out=None)

Parameters:

  • input: the dividend (tensor).
  • other: the divisor (tensor or number).

Return: It returns a tensor with remainder values.

Let’s understand the torch.remainder() method with the help of some Python examples.

Example 1:

In the Python example below we compute the remainder when a torch tensor is divided by a number. 

Here, the division of -13  by 5, the remainder is 2. How? mod(-13, 5) = -3, then -3+5 = 2. When the modulus value is different from the divisor, the divisor is added to the modulus. Notice how the remainders are different when the divisor is -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)


Output:

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

Example 2:

In the Python example below, we compute the element-wise remainder when both dividend and divisor are torch tensors.

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)


Output:

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

Example 3:

In the example below, we find the remainder as we did in Example 2 but for the floating-point tensors.

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)


Output:

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

Example 4:

In the example below, try to find the remainder when divided by zero or infinity.

Notice that when the divisor is zero the remainder is nan (Not a Number) irrespective of the dividend value. When a nonzero is divided by infinity, the remainder is infinity, but when zero is divided by infinity the remainder is 0. Also notice that both the tensors are floating-point tensors. See the next example when an integer is divided by zero.

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)


Output:

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

Example 5:

In this example, we try to find the remainder when an integer is divided by zero.

Notice that in the case of integer dividend it throws a runtime error while in the case of floating-point dividend it returns the remainder as nan (as in Example 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)


Output:

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


RuntimeError: ZeroDivisionError


Last Updated : 28 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads