Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to perform element-wise division on tensors in PyTorch?
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.
Syntax
torch.div(input, other, *, rounding_mode=None, out=None)
Parameters:
- input ? The dividend tensor
- other ? The divisor tensor or scalar
- rounding_mode ? Optional rounding mode ('trunc' or 'floor')
- out ? Optional output 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: Dividing a Tensor by a Scalar
# Python program to perform element-wise division
import torch
# Create a tensor
t = torch.Tensor([2, 3, 5, 9])
print("Original Tensor t:\n", t)
# Divide a tensor by a scalar 4
v = torch.div(t, 4)
print("Element-wise division result:\n", v)
# Same result can also be obtained as below
t1 = torch.Tensor([4])
w = torch.div(t, t1)
print("Element-wise division result:\n", w)
# other way to do above operation
t2 = torch.Tensor([4,4,4,4])
x = torch.div(t, t2)
print("Element-wise division result:\n", x)
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: Dividing a 2D Tensor by a 1D Tensor
The following example shows how to divide a 2D tensor by a 1D tensor using broadcasting ?
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:\n", T1)
print("T2:\n", T2)
# Divide 2-D tensor by 1-D tensor
v = torch.div(T1, T2)
print("Element-wise division result:\n", v)
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: Dividing a 1D Tensor by a 2D Tensor
The following example shows how to divide a 1D tensor by a 2D tensor ?
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:\n", T1)
print("T2:\n", T2)
# Divide 1-D tensor by 2-D tensor
v = torch.div(T2, T1)
print("Division 1D tensor by 2D tensor result:\n", v)
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: Dividing a 2D Tensor by a 2D Tensor
The following example shows how to divide two 2D tensors element-wise ?
import torch
# Create two 2-D tensors
T1 = torch.Tensor([[8,7],[3,4]])
T2 = torch.Tensor([[2,3],[4,9]])
# Print the above tensors
print("T1:\n", T1)
print("T2:\n", T2)
# Divide T1 by T2
v = torch.div(T1,T2)
print("Element-wise division result:\n", v)
T1:
tensor([[8., 7.],
[3., 4.]])
T2:
tensor([[2., 3.],
[4., 9.]])
Element-wise division result:
tensor([[4.0000, 2.3333],
[0.7500, 0.4444]])
Alternative Methods
PyTorch also provides the division operator / for element-wise division ?
import torch
# Create tensors
a = torch.tensor([10.0, 20.0, 30.0])
b = torch.tensor([2.0, 4.0, 5.0])
# Using / operator
result1 = a / b
print("Using / operator:", result1)
# Using torch.div()
result2 = torch.div(a, b)
print("Using torch.div():", result2)
Using / operator: tensor([5., 5., 6.]) Using torch.div(): tensor([5., 5., 6.])
Conclusion
Element-wise division in PyTorch can be performed using torch.div() or the / operator. Both methods support broadcasting, allowing division between tensors of different dimensions. The resulting tensor inherits the shape of the higher-dimensional tensor.
