pytorch

How to Perform Division Operation on Tensors in PyTorch?

In PyTorch, tensors are multidimensional arrays that can store different types of data, such as matrices, scalars, vectors, etc. PyTorch provides various methods and functions to perform different operations on tensors, such as arithmetic operations like addition, subtraction, multiplication, and division.

This blog will illustrate different methods to perform the division operation on tensors in PyTorch.

How to Perform Division Operation on Tensors in PyTorch?

To perform the division operation on tensors in PyTorch, different methods are used:

However, users need to follow the provided steps first to use these methods:

Step 1: Import PyTorch Library

First, import the “torch” library to perform the division operation:

import torch

 

Step 2: Create Tensors

Then, create two desired tensors and print their elements. Here, we are creating two simple tensors “tens1” and “tens2” from the list using the “torch.tensor()” function:

tens1 = torch.tensor([6, 1, 9])
tens2 = torch.tensor([3, 9, 2])

print(tens1)
print(tens2)

 

This has created two tensors as seen below:

Now, we will use different methods to divide these tensors’ values.

Method 1: Divide Tensors Using “/” Operator

To divide two tensors, simply use the “/” operator and store the result in a variable. Then, print that variable. Here, we are dividing “tens1” with “tens2” and storing their result in the “div” variable:

div = tens1 / tens2

print(div)

 

The below output shows the division of two tensors:

Alternatively, users can directly divide and print the division without using the third variable:

print(tens1 / tens2)

 

Subsequently, the division of two tensors has been calculated:

Method 2: Divide Tensors Using “torch.div()” Method

PyTorch provides the “torch.div()” method to divide desired tensors. Here, we are dividing “tens1” with “tens2” tensors using the “torch.div()” method and storing the result in the “div” variable:

div = torch.div(tens1, tens2)

print(div)

 

In the below output, the division of two tensors can be seen:

Alternatively, divide tensors and print the result at once:

print(torch.div(tens1, tens2))

 

It can be observed that the division operation has been performed on two tensors:

Method 3: Divide Tensors Using “torch.true_divide()” Method

Users can also use the “torch.true_divide()” method to divide the values of two tensors:

div = torch.true_divide(tens1, tens2)

print(div)

 

We have efficiently explained the methods to perform the division operation on tensors in PyTorch.

Note: You can access our Google Colab Notebook at this link.

Conclusion

To perform the division operation on tensors in PyTorch, first, import the “torch” library and create two desired tensors. After that, use the “/” operator or “torch.div()” or “torch.true_divide()” method to divide two tensors and display the results. This blog has illustrated different methods to perform the division operation on tensors in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.