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



To perform element-wise subtraction on tensors, we can use the torch.sub() method of PyTorch. The corresponding elements of the tensors are subtracted. We can subtract a scalar or tensor from another tensor. We can subtract a tensor from a tensor with same or different dimension. The dimension of the final tensor will be same as the dimension of the higher-dimensional 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 subtract a scalar quantity, define it.

  • Subtract a scalar or a tensor from another tensor using torch.sub() and assign the value to a new variable. You can also subtract a scalar quantity from the tensor. Subtracting the tensors using this method does not make any change in the original tensors.

  • Print the final tensor.

Example 1

Here, we will have a Python 3 program to subtract a scalar quantity from a tensor. We will see three different ways to perform the same task.

# Python program to perform element-wise subtraction # import the required library import torch # Create a tensor t = torch.Tensor([1.5, 2.03, 3.8, 2.9]) print("Original Tensor t:\n", t) # Subtract a scalar value to a tensor v = torch.sub(t, 5.60) print("Element-wise subtraction result:\n", v) # Same result can also be obtained as below t1 = torch.Tensor([5.60]) w = torch.sub(t, t1) print("Element-wise subtraction result:\n", w) # Other way to do above operation t2 = torch.Tensor([5.60,5.60,5.60,5.60]) x = torch.sub(t, t2) print("Element-wise subtraction result:\n", x)

Output

Original Tensor t:    tensor([1.5000, 2.0300, 3.8000, 2.9000]) Element-wise subtraction result:    tensor([-4.1000, -3.5700, -1.8000, -2.7000]) Element-wise subtraction result:    tensor([-4.1000, -3.5700, -1.8000, -2.7000]) Element-wise subtraction result:    tensor([-4.1000, -3.5700, -1.8000, -2.7000])

Example 2

The following program shows how to subtract a 1-D tensor from a 2-D tensor.

# Import necessary library 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) # Subtract 1-D tensor from 2-D tensor v = torch.sub(T1, T2) print("Element-wise subtraction result:\n", v)

Output

T1: tensor([[8., 7.],          [4., 5.]]) T2:    tensor([10., 5.]) Element-wise subtraction result: tensor([[-2., 2.],          [-6., 0.]])

Example 3

The following program shows how to subtract a 2D tensor from a 1D tensor.

# Python program to subtract 2D tensor from 1D tensor # Import the library import torch # Create a 2D tensor T1 = torch.Tensor([[1,2],[4,5]]) # Create a 1-D tensor T2 = torch.Tensor([10, 5]) print("T1:\n", T1) print("T2:\n", T2) # Subtract 2-D tensor from 1-D tensor v = torch.sub(T2, T1) print("Element-wise subtraction result:\n", v)

Output

T1: tensor([[1., 2.],          [4., 5.]]) T2:    tensor([10., 5.]) Element-wise subtraction result: tensor([[9., 3.],          [6., 0.]])

You can notice that the final tensor is a 2D tensor.

Example 4

The following program shows how to subtract a 2D tensor from a 2D tensor.

# import the library import torch # Create two 2-D tensors T1 = torch.Tensor([[8,7],[3,4]]) T2 = torch.Tensor([[0,3],[4,9]]) print("T1:\n", T1) print("T2:\n", T2) # Subtract above two 2-D tensors v = torch.sub(T1,T2) print("Element-wise subtraction result:\n", v)

Output

T1: tensor([[8., 7.],          [3., 4.]]) T2: tensor([[0., 3.],          [4., 9.]]) Element-wise subtraction result: tensor([[ 8., 4.],          [-1., -5.]])
Updated on: 2021-11-06T09:47:54+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements