How to compute element-wise remainder of given input tensor in PyTorch?
Last Updated :
09 Oct, 2022
In this article, we are going to see how to compute the element-wise remainder in PyTorch. we have two methods to compute element-wise reminders one is torch.remainder() and the other one is torch.fmod() let's go discuss both of them one by one.
torch.remainder() method
The 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 scalar quantity or tensor. The values must be an integer and float only. before moving further let's see the syntax of the given method.
Syntax: torch.remainder(input, other, out=None)
Parameters:
- input (Tensor or Scalar) : the dividend element.
- other (Tensor or Scalar) : the divisor element.
Return: This method returns a new tensor with remainder values.
Example 1:
The following program is to compute the element-wise remainder of two single-dimension tensors.
Python3
# importing torch
import torch
# define the dividend
tens_1 = torch.tensor([5., -12., 25., -10., 30])
print("Dividend: ", tens_1)
# define the divisor
tens_2 = torch.tensor([5., -5., -6., 5., 8.])
print("Divisor: ", tens_2)
# compute the remainder
remainder = torch.remainder(tens_1, tens_2)
# display result
print("Remainder: ", remainder)
Output:
Dividend: tensor([ 5., -12., 25., -10., 30.])
Divisor: tensor([ 5., -5., -6., 5., 8.])
Remainder: tensor([ 0., -2., -5., -0., 6.])
Example 2:
The following program is to compute the element-wise remainder of two 2D tensors.
Python3
# importing torch
import torch
# define the dividend
tens_1 = torch.tensor([[5., -12.],
[-10., 30.], ])
print("\n Dividend: \n", tens_1)
# define the divisor
tens_2 = torch.tensor([[5., -5.],
[5., 8.], ])
print("\n Divisor: \n", tens_2)
# compute the remainder
remainder = torch.remainder(tens_1, tens_2)
# display result
print("\n Remainder: \n", remainder)
Output:
Dividend:
tensor([[ 5., -12.],
[-10., 30.]])
Divisor:
tensor([[ 5., -5.],
[ 5., 8.]])
Remainder:
tensor([[ 0., -2.],
[-0., 6.]])
torch.fmod() method
This method gives also helps us to compute the element-wise remainder of division by the divisor. The divisor may be a number or a Tensor. When the divisor is zero it will return NaN. before moving further let's see the syntax of the given method.
Syntax: torch.fmod(input, other)
Parameters:
- input (Tensor) : the dividend.
- other (Tensor or Scalar) : the divisor.
Return: This method returns a new tensor with remainder values.
Example 1:
The following program is to compute the element-wise remainder of two single-dimension tensors.
Python3
# importing torch
import torch
# define the dividend
tens_1 = torch.tensor([5., -10., -17., 19., 20.])
print("\n\n Dividend: ", tens_1)
# define the divisor
tens_2 = torch.tensor([2., 5., 17., 7., 10.])
print("\n Divisor: ", tens_2)
# compute the remainder using fmod()
remainder = torch.fmod(tens_1, tens_2)
# display result
print("\n Remainder: ", remainder)
Output:
Dividend: tensor([ 5., -10., -17., 19., 20.])
Divisor: tensor([ 2., 5., 17., 7., 10.])
Remainder: tensor([1., -0., -0., 5., 0.])
Example 2:
The following program is to compute the element-wise remainder of two 2D tensors.
Python3
# importing torch
import torch
# define the dividend
tens_1 = torch.tensor([[16., -12.],
[-10., 30.], ])
print("\n\n Dividend: \n", tens_1)
# define the divisor
tens_2 = torch.tensor([[5., -6.],
[5., 8.], ])
print("\n Divisor: \n", tens_2)
# compute the remainder using fmod()
remainder = torch.fmod(tens_1, tens_2)
# display result
print("\n Remainder:\n", remainder)
Output:
Dividend:
tensor([[ 16., -12.],
[-10., 30.]])
Divisor:
tensor([[ 5., -6.],
[ 5., 8.]])
Remainder:
tensor([[1., -0.],
[-0., 6.]])
Similar Reads
How to compute the element-wise angle of given input tensor in PyTorch? In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch. torch.angle() method Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angl
3 min read
How to compute element-wise entropy of an input tensor in PyTorch In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input a
2 min read
How to Compute the Logistic Sigmoid Function of Tensor Elements in PyTorch In this article, we will see how to compute the logistic sigmoid function of Tensor Elements in PyTorch. The torch.special.expit() & torch.sigmoid() methods are logistic functions in a tensor. torch.sigmoid() is an alias of torch.special.expit() method. Â So, these methods will take the torch ten
2 min read
How to perform element-wise subtraction on tensors in PyTorch? In this article, we are going to understand how to perform element-wise subtraction on tensors in PyTorch in Python. We can perform element-wise subtraction using torch.sub() method. torch.sub() method allows us to perform subtraction on the same or different dimensions of tensors. It takes two tens
3 min read
How to perform element-wise division on tensors in PyTorch? In this article, we will understand how to perform element-wise division of two tensors in PyTorch. To perform the element-wise division of tensors, we can apply the torch.div() method. It takes two tensors (dividend and divisor) as the inputs and returns a new tensor with the element-wise division
3 min read