How to find element-wise remainder in PyTorch?


Element-wise remainder when a tensor is divided by other tensor is computed using the torch.remainder() method. We can also apply torch.fmod() to find the remainder.

The difference between these two methods is that in torch.remainder(), when the sign of result is different than the sign of divisor, then the divisor is added to the result; whereas in torch.fmod(), it is not added.

Syntax

torch.remainder(input, other)
torch.fmod(input, other)

Parameters

  • Input – It is a PyTorch tensor or scalar, the dividend.

  • Other – It is also a PyTorch tensor or scalar, the divisor.

Output

It returns a tensor of element-wise remainder values.

Steps

  • Import the torch library.

  • Define tensors, the dividend and the divisor.

  • Compute torch.remainder(input, other) or torch.fmod(input, other). It gives a tensor of remainder values.

  • Display the computed tensor of remainders.

Example 1

In the following Python program, we will see how to find the remainder when a tensor is divided by a scalar quantity.

# Python program to find remainder using torch.remainder()
# import the library
import torch

# define a tensor
tensor1 = torch.tensor([10,-22,31,-47])

# print the created tensors
print("Tensor 1:", tensor1)
print("Divisor:", 5)

# compute the element-wise remainder tensor/scalar
rem = torch.remainder(tensor1, 5)

print("Remainder:", rem)

Output

Tensor 1: tensor([ 10, -22, 31, -47])
Divisor: 5
Remainder: tensor([0, 3, 1, 3])

Example 2

# Python program to find remainder using torch.fmod()
# import necessary libraries
import torch

# define a tensor
tensor1 = torch.tensor([10,-22,31,-47])
# print the created tensors
print("Tensor 1:", tensor1)
print("Divisor:", 5)

# compute the element-wise remainder of tensor/scalar
rem = torch.fmod(tensor1, 5)

print("Remainder:", rem)

Output

Tensor 1: tensor([ 10, -22, 31, -47])
Divisor: 5
Remainder: tensor([ 0, -2, 1, -2])

Notice the difference between the output of the above two examples. In both the examples, we have the same inputs, but we use different methods to compute the remainder. In Example 1, we use torch.remainder(), whereas in Example 2, we use torch.fmod().

Example 3

# import necessary libraries
import torch

# define two tensors
tensor1 = torch.tensor([10,22,31,47])
tensor2 = torch.tensor([2,3,4,5])

# print the created tensors
print("Tensor 1:", tensor1)
print("Tensor 2:", tensor2)
# compute the element-wise remainder of tensor1/tensor2
rem = torch.remainder(tensor1, tensor2)
print("Remainder:", rem)

Output

Tensor 1: tensor([10, 22, 31, 47])
Tensor 2: tensor([2, 3, 4, 5])
Remainder: tensor([0, 1, 3, 2])

Example 4

# import necessary libraries
import torch

# define two tensors
tensor1 = torch.tensor([10.,22.,31.,47.])
tensor2 = torch.tensor([0,3,0,5])

# print the created tensors
print("Tensor 1:", tensor1)
print("Tensor 2:", tensor2)

# compute the element-wise remainder of tensor1/tensor2
rem = torch.remainder(tensor1, tensor2)
print("Remainder:", rem)

Output

Tensor 1: tensor([10., 22., 31., 47.])
Tensor 2: tensor([0, 3, 0, 5])
Remainder: tensor([nan, 1., nan, 2.])

Updated on: 06-Dec-2021

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements