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 self-explanatory. 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 1-D Tensor from 2-D Tensor
In this example, we create a 1-D tensor of size 1×3 and subtract it from a 2-D tensor of size 4×3. The 1-D 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 1-D Tensor from 2-D 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 2-D Tensors
In this example, we create two 2D Tensors of the same size 4×3 and then do element-wise 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 1-D Tensor from 3-D Tensor
In this example, we create a 1-D tensor of size 1×3 and subtract it from a 3-D tensor of size 2x4x3. In this case, intuitively we can say that the 1-D tensor is subtracted element-wise from each row of the 3-D 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 2-D Tensor from 3-D 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 3-D Tensors Element Wise
In this example, we create two 3D Tensors of the same size 2x4x3 and then do element-wise 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