- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.])
- Related Articles
- How to perform element-wise addition on tensors in PyTorch?
- How to perform element-wise subtraction on tensors in PyTorch?
- How to perform element-wise multiplication on tensors in PyTorch?
- How to perform element-wise division on tensors in PyTorch?
- How to apply rectified linear unit function element-wise in PyTorch?
- PyTorch – How to compute element-wise logical XOR of tensors?
- PyTorch – How to compute element-wise entropy of an input tensor?
- Return the element-wise remainder of division in Numpy
- How to compute the element-wise angle of the given input tensor in PyTorch?
- Return element-wise quotient and remainder simultaneously in Python Numpy
- Return the element-wise remainder of division with modulo operation in Numpy
- Return the element-wise remainder of division with fmod() operation in Numpy
- How to apply functions element-wise in a dataframe in Python?
- Subtract arguments element-wise in Numpy
- How to compute the Heaviside step function for each element in input in PyTorch?
