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

PythonPyTorchServer Side ProgrammingProgramming

To perform element-wise division on two tensors in PyTorch, we can use the torch.div() method. It divides each element of the first input tensor by the corresponding element of the second tensor. We can also divide a tensor by a scalar. A tensor can be divided by a tensor with same or different dimension. The dimension of the final tensor will be same as the dimension of the higher-dimensional tensor. If we divide a 1D tensor by a 2D tensor, then the final tensor will a 2D tensor.

Steps

  • Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.

  • Define two or more PyTorch tensors and print them. If you want to divide a tensor by a scalar, define a scalar.

  • Divide a tensor by another tensor or scalar using torch.div() and assign the value to a new variable. Dividing the tensors using this method does not make any change in the original tensors.

  • Print the final tensor.

Example 1

# Python program to perform element-wise division
# import the required library
import torch

# Create a tensor
t = torch.Tensor([2, 3, 5, 9])
print("Original Tensor t:
", t) # Divide a tensor by a scalar 4 v = torch.div(t, 4) print("Element-wise division result:
", v) # Same result can also be obtained as below t1 = torch.Tensor([4]) w = torch.div(t, t1) print("Element-wise division result:
", w) # other way to do above operation t2 = torch.Tensor([4,4,4,4]) x = torch.div(t, t2) print("Element-wise division result:
", x)

Output

Original Tensor t:
   tensor([2., 3., 5., 9.])
Element-wise division result:
   tensor([0.5000, 0.7500, 1.2500, 2.2500])
Element-wise division result:
   tensor([0.5000, 0.7500, 1.2500, 2.2500])
Element-wise division result:
   tensor([0.5000, 0.7500, 1.2500, 2.2500])

Example 2

The following Python program shows how to divide a 2D tensor by a 1D tensor.

# import the required library
import torch

# Create a 2D tensor
T1 = torch.Tensor([[3,2],[7,5]])

# Create a 1-D tensor
T2 = torch.Tensor([10, 8])
print("T1:
", T1) print("T2:
", T2) # Divide 2-D tensor by 1-D tensor v = torch.div(T1, T2) print("Element-wise division result:
", v)

Output

T1:
tensor([[3., 2.],
         [7., 5.]])
T2:
tensor([10., 8.])
Element-wise division result:
tensor([[0.3000, 0.2500],
         [0.7000, 0.6250]])

Example 3

The following Python program shows how to divide a 1D tensor by a 2D tensor.

# Python program to dive a 1D tensor by a 2D tensor
# import the required library
import torch

# Create a 2D tensor
T1 = torch.Tensor([[8,7],[4,5]])

# Create a 1-D tensor
T2 = torch.Tensor([10, 5])
print("T1:
", T1) print("T2:
", T2) # Divide 1-D tensor by 2-D tensor v = torch.div(T2, T1) print("Division 1D tensor by 2D tensor result:
", v)

Output

T1:
tensor([[8., 7.],
         [4., 5.]])
T2:
tensor([10., 5.])
Division 1D tensor by 2D tensor result:
tensor([[1.2500, 0.7143],
         [2.5000, 1.0000]])

You can notice the final tensor is a 2D tensor.

Example 4

The following Python program shows how to divide a 2D tensor by a 2D tensor.

# import necessary library
import torch

# Create two 2-D tensors
T1 = torch.Tensor([[8,7],[3,4]])
T2 = torch.Tensor([[0,3],[4,9]])

# Print the above tensors
print("T1:
", T1) print("T2:
", T2) # Divide T1 by T2 v = torch.div(T1,T2) print("Element-wise division result:
", v)

Output

T1:
tensor([[8., 7.],
         [3., 4.]])
T2:
tensor([[0., 3.],
         [4., 9.]])
Element-wise division result:
tensor([[ inf, 2.3333],
         [0.7500, 0.4444]])
raja
Updated on 06-Nov-2021 09:51:34

Advertisements