How to perform element-wise division on tensors in PyTorch?
Last Updated :
02 Mar, 2022
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
import torch
A = torch.tensor([ 0.0312 , 0.3401 , 0.1645 , - 1.0781 ])
print ( "Tensor A:\n" , A)
B = torch.tensor([ - 1.8584 , 0.5706 , - 0.8994 , 2.2492 ])
print ( "\nTensor B:\n" , 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
import torch
a = torch. 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 ]])
print ( "Tensor a:\n" , a)
b = torch. 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 ]])
print ( "\nTensor b:\n" , b)
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
import torch
a = torch.randn( 3 , 2 , 2 )
b = torch.randn( 2 )
print ( "Tensor a :\n" , a)
print ( "\nTensor b :\n" , 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]]])
Similar Reads
How to perform element-wise addition on tensors in PyTorch?
In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function. This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in dim
3 min read
How to perform element-wise subtraction on tensors in PyTorch?
In this article, we are going to understand how to perform element-wise subtraction on tensors in PyTorch in Python. We can perform element-wise subtraction using torch.sub() method. torch.sub() method allows us to perform subtraction on the same or different dimensions of tensors. It takes two tens
3 min read
How to perform element-wise multiplication on tensors in PyTorch?
In this article, we are going to see how to perform element-wise multiplication on tensors in PyTorch in Python. We can perform element-wise addition using torch.mul() method. This function also allows us to perform multiplication on the same or different dimensions of tensors. If tensors are differ
3 min read
How to compute element-wise entropy of an input tensor in PyTorch
In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input a
2 min read
How To Sort The Elements of a Tensor in PyTorch?
In this article, we are going to see how to sort the elements of a PyTorch Tensor in Python. To sort the elements of a PyTorch tensor, we use torch.sort() method. We can sort the elements along with columns or rows when the tensor is 2-dimensional. Syntax: torch.sort(input, dim=- 1, descending=False
3 min read
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() metho
3 min read
How to compute the element-wise angle of given input tensor in PyTorch?
In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch. torch.angle() method Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angl
3 min read
Compute element-wise logical AND, OR and NOT of tensors in PyTorch
In this article, we are going to see how to compute element-wise logical AND, OR, and NOT of given tensors in PyTorch. We can compute this by using the torch.logical_and(), torch.logical_or(), and torch.logical_not() methods. Let's discuss all of them one by one. Compute element-wise with logical AN
4 min read
How to compare two tensors in PyTorch?
In this article, we are going to see how we can compare two tensors in Pytorch. We can compare two tensors by using the torch.eq() method. This method compares the corresponding elements of tensors. It has to return rue at each location where both tensors have equal value else it will return false.
2 min read
How to Get the Value of a Tensor in PyTorch
When working with PyTorch, a powerful and flexible deep learning framework, you often need to access and manipulate the values stored within tensors. Tensors are the core data structures in PyTorch, representing multi-dimensional arrays that can store various types of data, including scalars, vector
5 min read