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
import torch
tens_1 = torch.tensor([ 5. , - 12. , 25. , - 10. , 30 ])
print ( "Dividend: " , tens_1)
tens_2 = torch.tensor([ 5. , - 5. , - 6. , 5. , 8. ])
print ( "Divisor: " , tens_2)
remainder = torch.remainder(tens_1, tens_2)
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
import torch
tens_1 = torch.tensor([[ 5. , - 12. ],
[ - 10. , 30. ], ])
print ( "\n Dividend: \n" , tens_1)
tens_2 = torch.tensor([[ 5. , - 5. ],
[ 5. , 8. ], ])
print ( "\n Divisor: \n" , tens_2)
remainder = torch.remainder(tens_1, tens_2)
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
import torch
tens_1 = torch.tensor([ 5. , - 10. , - 17. , 19. , 20. ])
print ( "\n\n Dividend: " , tens_1)
tens_2 = torch.tensor([ 2. , 5. , 17. , 7. , 10. ])
print ( "\n Divisor: " , tens_2)
remainder = torch.fmod(tens_1, tens_2)
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
import torch
tens_1 = torch.tensor([[ 16. , - 12. ],
[ - 10. , 30. ], ])
print ( "\n\n Dividend: \n" , tens_1)
tens_2 = torch.tensor([[ 5. , - 6. ],
[ 5. , 8. ], ])
print ( "\n Divisor: \n" , tens_2)
remainder = torch.fmod(tens_1, tens_2)
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 tens
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
Compute element-wise logical AND, OR and NOT of tensors in PyTorch
In this article, we are going to see how to compute element-wise logical AND, OR, and NOT of given tensors in PyTorch. We can compute this by using the torch.logical_and(), torch.logical_or(), and torch.logical_not() methods. Let's discuss all of them one by one. Compute element-wise with logical AN
4 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
How to perform element-wise addition on tensors in PyTorch?
In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function. This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in dim
3 min read
How to perform element-wise multiplication on tensors in PyTorch?
In this article, we are going to see how to perform element-wise multiplication on tensors in PyTorch in Python. We can perform element-wise addition using torch.mul() method. This function also allows us to perform multiplication on the same or different dimensions of tensors. If tensors are differ
3 min read
How to Apply Rectified Linear Unit Function Element-Wise in PyTorch?
In this article, we are going to see How to Apply Rectified Linear Unit Function Element-Wise in PyTorch in Python. We can Rectify Linear Unit Function Element-Wise by using torch.nn.ReLU() method. torch.nn.ReLU() method In PyTorch, torch.nn.ReLu() method replaces all the negative values with 0 and
2 min read
How to Construct a Complex Tensor With the Given Real and Imaginary Parts in PyTorch?
In this article, we will see how to construct a complex tensor with the given Real and Imaginary Parts in PyTorch. Now we will see how to construct complex number in pytorch using torch.complex() method with the given input as real and imaginary numbers. Syntax: torch.complex(real, imag) Parameter:
3 min read