Python PyTorch remainder() method
Last Updated :
28 Feb, 2022
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
import torch
x = torch.tensor([ 5 , - 13 , 24 , - 7 , 7 ])
print ( "Dividend:" , x)
y = 5
print ( "Divisor:" ,y)
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
import torch
x = torch.tensor([ 15 , - 13 , 15 , - 15 , 0 ])
print ( "Dividend:" , x)
y = torch.tensor([ 7 , 7 , - 7 , - 7 , 7 ])
print ( "Divisor:" ,y)
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
import torch
x = torch.tensor([ 15. , - 13. , 15. , - 15. , 0 ])
print ( "Dividend:" , x)
y = torch.tensor([ 7. , 7. , - 7. , - 7. , 7. ])
print ( "Divisor:" ,y)
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
import torch
import numpy as np
x = torch.tensor([ 15. , - 13. , 0. , - 15. , 0 ])
print ( "Dividend:" , x)
y = torch.tensor([ 0. , np.inf, 0. , 0. , np.inf])
print ( "Divisor:" ,y)
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
import torch
import numpy as np
x = torch.tensor([ 15 ])
print ( "Dividend:" , x)
y = torch.tensor([ 0 ])
print ( "Divisor:" ,y)
remainder = torch.remainder(x,y)
print ( "Remainder:" ,remainder)
|
Output:
Dividend: tensor([15])
Divisor: tensor([0])
RuntimeError: ZeroDivisionError
Similar Reads
Python - PyTorch div() method
PyTorch torch.div() method divides every element of the input with a constant and returns a new modified tensor. Syntax: torch.div(inp, other, out=None) Arguments inp: This is input tensor. other: This is a number to be divided to each element of input inp. out: The output tensor. Return: It returns
1 min read
Python - PyTorch fmod() method
PyTorch torch.fmod() method gives the element-wise remainder of division by divisor. The divisor may be a number or a Tensor. Syntax: torch.fmod(input, div, out=None) Arguments input: This is input tensor. div: This may be a number or a tensor. out: The output tensor. Return: It returns a Tensor. Le
1 min read
Python - PyTorch frac() method
PyTorch torch.frac() method computes the fractional portion of each element in input. Syntax: torch.frac(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example 1: # Importing the PyTor
1 min read
Python - PyTorch floor() method
PyTorch torch.floor() method returns a new tensor which is floor of the elements of input, the largest integer less than or equal to each element. Syntax: torch.floor(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept w
1 min read
Python | Decimal remainder_near() method
Decimal#remainder_near() : remainder_near() is a Decimal class method which returns x - y * n, where n is the integer nearest the exact value of x / y Syntax: Decimal.remainder_near() Parameter: Decimal values Return: x - y * n, where n is the integer nearest the exact value of x / y Code #1 : Examp
2 min read
Python | sympy.totient() method
With the help of sympy.totient() method, we can find Euler totient function or phi(n) of a given integer. Euler totient function is the number of positive integers less than or equal to a given integer that are relatively prime to it. In other words, it is the number of integers k in the range 1
1 min read
numpy.remainder() in Python
numpy.remainder() is another function for doing mathematical operations in numpy.It returns element-wise remainder of division between two array arr1 and arr2 i.e. arr1 % arr2 .It returns 0 when arr2 is 0 and both arr1 and arr2 are (arrays of) integers. Syntax : numpy.remainder(arr1, arr2, /, out=No
2 min read
Python | sympy.reduced_totient() method
With the help of sympy.reduced_totient() method, we can find the Carmichael reduced totient function or lambda(n) in SymPy. reduced_totient(n) or [Tex]\lambda(n)[/Tex] is the smallest m > 0 such that [Tex]k^m \equiv 1 \mod n[/Tex] for all k relatively prime to n. Syntax: reduced_totient(n) Parame
1 min read
Python | sympy.subfactorial() method
With the help of sympy.subfactorial() method, we can find the Subfactorial of a number in SymPy. Subfactorial of a number is given by - [Tex] !n = \begin{cases} 1 & n = 0 \\ 0 & n = 1 \\ (n-1)(!(n-1) + !(n-2)) & n > 1 \end{cases} [/Tex] Syntax: subfactorial(n) Parameter: n - It denote
1 min read
Python | sympy.div() method
The function div() provides division of polynomials with remainder. That is, for polynomials f and g, it computes q and r, such that f= g*q+r and deg(r) <q. Syntax:sympy.div(f, g, domain='QQ') Return: division of polynomials with remainder Example #1: # import sympy from sympy import * f = 5 * x*
1 min read