Introduction
In this tutorial, we will see how to use torch.sub() function to subtract tensors in PyTorch. We will see multiple examples of this function for easy understanding by beginners. So let us begin.
Subtracting Tensors in PyTorch with torch.sub()
In PyTorch, the tensors can be subtracted easily with the torch sub function whose usage is very selfexplanatory. It also enables broadcasting to a common shape which means the tensors of different sizes can be subtracted from each other if their shapes are compatible for broadcasting.
Syntax
The syntax of torch.sub function is as follows â€“
torch.sub(input, other, alpha=1, out=None)
 input:Â This is the input tensor for addition.
 other: This is the other tensor or scalar to be subtracted from the input tensor above.
 alpha:Â An optional multiplier to other tensor before it is added to the input tensor. By default, it is 1.
 out:Â This is the output tensor.
Example of torch.sub() function
Import Torch Library
Before going through examples of the torch add function let us first import the torch library.
In [0]:
import torch;
Example 1: Subtracting a Scalar Value from Tensor with torch.sub()
In this example, we create a 2D tensor and subtract a scalar value of 5 from it by using the torch.sub function. The scalar value is broadcasted and subtracted from the tensor elementwise as evident in the output.
In [1]:
tensor1 = torch.arange(12).reshape(4,3) scalar = 5 subÂ =Â torch.sub(tensor1,scalar) print("First Tensor:") print(tensor1) print("Scalar:"Â +Â str(scalar)) print("Tensor1 minus Scalar:") print(sub)
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Scalar:5 Tensor1 minus Scalar: tensor([[5, 4, 3], [2, 1, 0], [ 1, 2, 3], [ 4, 5, 6]])
Example 2: Subtracting a 1D Tensor from 2D Tensor
In this example, we create a 1D tensor of size 1Ã—3 and subtract it from a 2D tensor of size 4Ã—3. The 1D tensor is subtracted from each row of the 2D tensor elementwise.
In [2]:
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(3) subÂ =Â torch.sub(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Tensor1 minus Tensor2:") print(sub)
Out[2]:
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([0, 1, 2]) Tensor1 minus Tensor2: tensor([[0, 0, 0], [3, 3, 3], [6, 6, 6], [9, 9, 9]])
Example 3: Subtracting a 1D Tensor from 2D Tensor
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(4).reshape(4,1) subÂ =Â torch.sub(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Tensor1 minus Tensor2:") print(sub)
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([[0], [1], [2], [3]]) Tensor1 minus Tensor2: tensor([[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8]])
Example 4: Element Wise Subtraction of two 2D Tensors
In this example, we create two 2D Tensors of the same size 4Ã—3 and then do elementwise subtraction with the torch sub function.
In[4]:
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(start=4, end=16).reshape(4,3) sub = torch.sub(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Tensor1 minus Tensor2:") print(sub)
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([[ 4, 5, 6], [ 7, 8, 9], [10, 11, 12], [13, 14, 15]]) Tensor1 minus Tensor2: tensor([[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]])
Example 5: Subtracting a 1D Tensor from 3D Tensor
In this example, we create a 1D tensor of size 1Ã—3 and subtract it from a 3D tensor of size 2x4x3. In this case, intuitively we can say that the 1D tensor is subtracted elementwise from each row of the 3D tensor.
In [5]:
tensor1 = torch.arange(24).reshape(2,4,3) tensor2 = torch.arange(3) subÂ =Â torch.sub(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Tensor1 minus Tensor2:") print(sub)
Out[10]:
First Tensor: tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) Second Tensor: tensor([0, 1, 2]) Tensor1 minus Tensor2: tensor([[[ 0, 0, 0], [ 3, 3, 3], [ 6, 6, 6], [ 9, 9, 9]], [[12, 12, 12], [15, 15, 15], [18, 18, 18], [21, 21, 21]]])
Example 6: Subtracting a 2D Tensor from 3D Tensor
In [6]:
tensor1 = torch.arange(24).reshape(2,4,3) tensor2 = torch.arange(12).reshape(4,3) subÂ =Â torch.sub(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Tensor1 minus Tensor2:") print(sub)
First Tensor: tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) Second Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Tensor1 minus Tensor2: tensor([[[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[12, 12, 12], [12, 12, 12], [12, 12, 12], [12, 12, 12]]])
Example 7: Subtracting two 3D Tensors Element Wise
In this example, we create two 3D Tensors of the same size 2x4x3 and then do elementwise subtraction with the torch add function.
In [7]:
tensor1 = torch.arange(24).reshape(2,4,3) tensor2 = torch.arange(start=4, end=28).reshape(2,4,3) subÂ =Â torch.sub(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Tensor1 minus Tensor2:") print(sub)
First Tensor: tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) Second Tensor: tensor([[[ 4, 5, 6], [ 7, 8, 9], [10, 11, 12], [13, 14, 15]], [[16, 17, 18], [19, 20, 21], [22, 23, 24], [25, 26, 27]]]) Tensor1 minus Tensor2: tensor([[[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]], [[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]]])
Example 8: Using torch.sub() with Alpha Parameter
In this example, we will see how to use the parameter alpha with torch.sub() function. The second tensor is multiplied by the value of alpha = 5 and its output is subtracted from the first tensor.
In [8]:
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(3) subÂ =Â torch.sub(tensor1,tensor2,Â alpha=5) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Tensor1 minus Tensor2:") print(sub)
Out[8]:
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([0, 1, 2]) Tensor1 minus Tensor2: tensor([[ 0, 4, 8], [ 3, 1, 5], [ 6, 2, 2], [ 9, 5, 1]])
 Also Read â€“ How to use torch.add() to Add Tensors in PyTorch
Reference: PyTorch Documentation

MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.