Skip to content
Related Articles
Open in App
Not now

Related Articles

How to perform element-wise division on tensors in PyTorch?

Improve Article
Save Article
  • Last Updated : 02 Mar, 2022
Improve Article
Save Article

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 result. We can use the below syntax to compute the element-wise division-

Syntax: torch.div(input, other, rounding_mode=None)

Parameters:

  • input: the first input tensor (dividend).
  • other: the second input tensor (divisor).
  • rounding_mode: The type of rounding applied to the result.

Return: it returns a new tensor with element-wise division of the tensor input by the tensor other.

Example:

Inputs:
tensor([10., 25., 30.])
tensor([2., -5., 10.])

Output:
tensor([5., -5., 3.])

Inputs:
tensor([3., 0., 23.])
tensor([2., -5., 0.])

Output:
tensor([1.5, -0., inf])

Let’s understand how the element-wise division of tensors works with the help of some Python examples.

Example 1:

In the example below, we perform the element-wise division of two 1-D tensors using the PyTorch method torch.div().

Python3




# Python program to divide two 1D tensors
# element-wise using torch.div() method
# importing torch
import torch
 
# creating first tensor
A = torch.tensor([0.03120.34010.1645, -1.0781])
print("Tensor A:\n", A)
 
# creating second tensor
B = torch.tensor([-1.85840.5706, -0.89942.2492])
print("\nTensor B:\n", B)
 
# divide A by B
result = torch.div(A, B)
print("\nElement-wise Division Output:\n", result)


Output:

Tensor A:
 tensor([ 0.0312,  0.3401,  0.1645, -1.0781])

Tensor B:
 tensor([-1.8584,  0.5706, -0.8994,  2.2492])

Element-wise Division Output:
 tensor([-0.0168,  0.5960, -0.1829, -0.4793])

Example 2:

In the example below, we perform the element-wise division of a 2D tensor by a 2D tensor using the PyTorch method torch.div(). We also apply different rounding modes.

Python3




# Python program to divide two
# 2D tensors element-wise
# importing torch
import torch
 
# defining first 2D tensor
a = torch. tensor([[-1.86650.63410.8920],
                   [-0.17120.39491.9414],
                   [-1.2088, -1.0375, -1.3087],
                   [0.9161, -0.29721.5289]])
print("Tensor a:\n", a)
 
# defining second 2D tensor
# b = torch.randn(4,3)
b = torch. tensor([[-0.21870.5252, -0.5840],
                   [1.5293, -0.45141.8490],
                   [-0.7269, -0.1561, -0.0629],
                   [-0.5379, -0.97510.6541]])
print("\nTensor b:\n", b)
 
# computing element-wise division
print("\nElement-wise Division:")
result1 = torch.div(a, b)
print("\nResult:\n", result1)
result2 = torch.div(a, b, rounding_mode='trunc')
print("\nResult with rounding_mode='trunc':\n", result2)
result3 = torch.div(a, b, rounding_mode='floor')
print("\nResult with rounding_mode='floor':\n", result3)


Output:

Tensor a:
 tensor([[-1.8665,  0.6341,  0.8920],
        [-0.1712,  0.3949,  1.9414],
        [-1.2088, -1.0375, -1.3087],
        [ 0.9161, -0.2972,  1.5289]])

Tensor b:
 tensor([[-0.2187,  0.5252, -0.5840],
        [ 1.5293, -0.4514,  1.8490],
        [-0.7269, -0.1561, -0.0629],
        [-0.5379, -0.9751,  0.6541]])

Element-wise Division:

Result:
 tensor([[ 8.5345,  1.2073, -1.5274],
        [-0.1119, -0.8748,  1.0500],
        [ 1.6630,  6.6464, 20.8060],
        [-1.7031,  0.3048,  2.3374]])

Result with rounding_mode='trunc':
 tensor([[ 8.,  1., -1.],
        [-0., -0.,  1.],
        [ 1.,  6., 20.],
        [-1.,  0.,  2.]])

Result with rounding_mode='floor':
 tensor([[ 8.,  1., -2.],
        [-1., -1.,  1.],
        [ 1.,  6., 20.],
        [-2.,  0.,  2.]])

Example 3:

In the below example, we perform the element-wise division of a 3-D tensor by a 1-D tensor using the PyTorch method torch.div().

Python3




# Python program to divide a 3D tensor by
# a 1D tensor element-wise
# Importing torch
import torch
 
# defining tensors
a = torch.randn(3, 2, 2)
b = torch.randn(2)
 
# printing the matrices
print("Tensor a :\n", a)
print("\nTensor b :\n", b)
 
# divide tensor a by b
result = torch.div(a, b)
print("\nElementwise Division Output :\n", result)


Output:

Tensor a :
 tensor([[[-0.7549,  1.8301],
         [-0.5545,  1.3180]],

        [[ 0.1159,  0.8394],
         [ 0.0452, -1.2860]],

        [[ 0.3850, -0.9654],
         [-1.5530, -0.8627]]])

Tensor b :
 tensor([-1.2378,  0.2153])

Elementwise Division Output :
 tensor([[[ 0.6098,  8.5011],
         [ 0.4480,  6.1226]],

        [[-0.0937,  3.8991],
         [-0.0365, -5.9739]],

        [[-0.3110, -4.4844],
         [ 1.2546, -4.0074]]])

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!