GFG App
Open App
Browser
Continue

# How to compute element-wise remainder of given input tensor in PyTorch?

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.]])```

My Personal Notes arrow_drop_up