pytorch

How to Perform Subtraction Operation on Tensors in PyTorch?

PyTorch tensors are multidimensional arrays that are utilized to store data of different types and shapes. They have numerous attributes and methods that permit users to perform various operations on them, such as reshaping, indexing, slicing, etc. Moreover, users can also perform the subtraction operation on tensors to subtract their values.

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

How to Perform Subtraction Operation on Tensors in PyTorch?

To perform the subtraction 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 subtraction 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([4, 1, 7])

tens2 = torch.tensor([3, 9, 5])

print(tens1)

print(tens2)

This has created two tensors as seen below:

Now, we will use different methods to perform subtraction on these tensors.

Method 1: Subtract Tensors Using β€œ-” Operator

To subtract two tensors, simply use the β€œβ€ operator and store the differences in a variable. Then, print that variable. Here, we are subtracting β€œtens1” and β€œtens2” and storing the result in the β€œsub” variable:

sub = tens1 + tens2

print(sub)

Upon doing so, the subtraction of the two tensors has been calculated:

Alternatively, users can directly subtract tensors and print their differences without using the third variable:

print(tens1 - tens2)

The below output shows the subtraction of two tensors:

Method 2: Subtract Tensors Using β€œtorch.subtract()” Method

PyTorch provides the β€œtorch.subtract()” method to subtract desired tensors. Here, we are subtracting β€œtens1” and β€œtens2” tensors using the β€œtorch.subtract()” method and storing their differences in the β€œsub” variable:

sub = torch.subtract(tens1, tens2)

print(sub)

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

Alternatively, directly subtract tensors and print their differences in one step:

print(torch.subtract(tens1, tens2))

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

Bonus Tip: How to Find Cumulative Sum of Differences?

Users can also calculate the sum of the values of the resultant tensor:

print(torch.subtract(tens1, tens2).sum())

The below output displays the sum of β€œ1”, β€œ-8” and β€œ2” which is β€œ-5”:

We have efficiently explained the subtraction operation methods on tensors in PyTorch.

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

Conclusion

To perform the subtraction operation on tensors in PyTorch, first, import the β€œtorch” library and create two desired tensors. After that, use the β€œβ€ operator or β€œtorch.subtract()” method to subtract the tensors and display the results. This blog has illustrated different methods to perform the subtraction 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.