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

Last updated Jun 24, 2022

PyTorch is a deep learning framework that can also be used to compute numerically. In this article, we'll go through the fundamentals of PyTorch's element-wise subtraction.

The torch.sub() function of PyTorch is used to execute element-wise subtraction on tensors. Tensors are subtracted from their respective elements. A scalar or tensor may be subtracted from yet another scalar or tensor. A tensor may be subtracted from another tensor, whether the dimensions are similar or distinct. The resulting tensor's dimensions are just the same as the greater tensor's dimension.

 

 element-wise subtraction on tensors in PyTorch

Performing element-wise subtraction on tensors in PyTorch

Simply follow the simple steps below to perform element-wise subtraction operation on tensors:

Step 1: Import the necessary torch library and make sure it is already installed.

Step 2: Create at least two tensors using PyTorch and print them out.

Step 3: Define the subtract a scalar quantity as well.

Step 4: Use a torch to subtract one scalar or one tensor from another, then set the result as a new variable.

Step 5: This is the last step in the process, and it requires printing the final tensor.

 

Example

In this section, we will look at a Python code that subtracts a scalar number from a tensor

import torch

T1 = torch.Tensor([[5,6],[3,4]])

T2 = torch.Tensor([7, 8])

print("T1:\n", T1)

print("T2:\n", T2)

v = torch.sub(T1, T2)

print("Element-wise subtraction result:\n", v)

 

 

Output

T1:

tensor([[5., 6.],

         [3., 4.]])

T2:

   tensor([7., 8.])

Element-wise subtraction result:

tensor([[-2., -2.],

         [-4., 14.]])

 

 

Download Source code

 

Python Advcance Tutorial

API Integration in Python Part 2 - Python Advance Tutorial

Python String Tutorial for Beginners

Python Array for Beginners | Array Examples in Python

What is the difference between .py and .pyc file

Python List | Create, append, remove items from list for beginner tutorial

Python Tuples for Beginners

Generate Random Number in Python | Heads and Tails Game

Python Notes application - Python File Operations Read and write files

Python Sqlite Tutorial - How do i use Sqlite in python

Python array Example - Create two different arrays and join them

Python Array Example - Check array elements availability count

Python Array Example - Create array with fixed size elements

Python Array Example - Create array with Random Integer Numbers

Python Array Example - Insert and sort the array items

Python Tkinter Canvas Draw shapes in Python

Python how to Create a Button in TKinter python

How To Read and Write CSV Files in Python

How To Read and write Excel files in Python

Python MySQL Connection Create table CRUD operations

Python Sqlite CRUD Operations

Python Advance - GUI Tutorial

Python How to get an Entry box within a Messagebox in Tkinter?

Python How do i Print a list to a Tkinter Text widget

Python How to attach a vertical scrollbar to a Treeview using Tkinter?

Python How to disable multiselection on Treeview in Tkinter?

Python How to take input in a text widget and display the text in Tkinter?

Python How to clear the text field part of ttk.Combobox in Tkinter?

Python How do I open a website in a Tkinter window?

Python Tkinter – How to position a topLevel() widget relative to the root window?

Python Creating a LabelFrame inside a Tkinter Canvas

Python Adding coloured text to selected text in Tkinter

Python How to set a certain number of rows and columns of a Tkinter grid?

Python Python - Ranking Rows of Pandas DataFrame

Python How do I install Python SciPy?

Python How to compute the sine of elements of a tensor in PyTorch?

How to get the data type of a tensor in PyTorch?

How to compute the mean and standard deviation of a tensor in PyTorch?

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

How to compute the Logarithm of elements of a tensor in PyTorch?

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

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

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

How to sort the elements of a tensor in PyTorch?

How to find mean across the image channels in PyTorch?

How to find the k-th and the top "k" elements of a tensor in PyTorch?

How to squeeze and unsqueeze a tensor in PyTorch?

How to Compare two Tensors in PyTorch?

How to compute the histogram of a tensor in PyTorch?

How to get a new API response in a Tkinter Textbox?

How do i convert Image to PDF Python

How do i convert JPG to PNG and PNG to JPG image python

Python get Image information - get image size

Python create timer

Python language Transulator - Google Tansulator

Image Slider in python

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

732 Views