Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 with same or different dimensions. The dimension of the final tensor will be the same as the dimension of the higher-dimensional tensor due to PyTorch's broadcasting rules.
Syntax
torch.sub(input, other, *, alpha=1, out=None)
Parameters:
- input ? The tensor to be subtracted from
- other ? The tensor or scalar to subtract
- alpha ? The multiplier for other (default: 1)
- out ? The output tensor (optional)
Subtracting a Scalar from a Tensor
Here are three different ways to subtract a scalar from a tensor ?
import torch
# Create a tensor
t = torch.Tensor([1.5, 2.03, 3.8, 2.9])
print("Original Tensor t:\n", t)
# Method 1: Subtract scalar directly
v = torch.sub(t, 5.60)
print("Element-wise subtraction result:\n", v)
# Method 2: Using a 1-element tensor
t1 = torch.Tensor([5.60])
w = torch.sub(t, t1)
print("Element-wise subtraction result:\n", w)
# Method 3: Using tensor with same shape
t2 = torch.Tensor([5.60, 5.60, 5.60, 5.60])
x = torch.sub(t, t2)
print("Element-wise subtraction result:\n", x)
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])
Subtracting 1D Tensor from 2D Tensor
The 1D tensor is broadcast across each row of the 2D tensor ?
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)
T1:
tensor([[8., 7.],
[4., 5.]])
T2:
tensor([10., 5.])
Element-wise subtraction result:
tensor([[-2., 2.],
[-6., 0.]])
Subtracting 2D Tensor from 1D Tensor
The 1D tensor is broadcast to match the 2D tensor dimensions ?
import torch
# Create tensors
T1 = torch.Tensor([[1, 2], [4, 5]])
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)
T1:
tensor([[1., 2.],
[4., 5.]])
T2:
tensor([10., 5.])
Element-wise subtraction result:
tensor([[9., 3.],
[6., 0.]])
Subtracting 2D Tensors
Element-wise subtraction between tensors of the same dimensions ?
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)
T1:
tensor([[8., 7.],
[3., 4.]])
T2:
tensor([[0., 3.],
[4., 9.]])
Element-wise subtraction result:
tensor([[ 8., 4.],
[-1., -5.]])
Using Alternative Syntax
PyTorch also supports the minus operator (-) for tensor subtraction ?
import torch
a = torch.tensor([5.0, 3.0, 2.0])
b = torch.tensor([1.0, 2.0, 4.0])
# Using torch.sub()
result1 = torch.sub(a, b)
print("Using torch.sub():\n", result1)
# Using minus operator
result2 = a - b
print("Using minus operator:\n", result2)
Using torch.sub(): tensor([4., 1., -2.]) Using minus operator: tensor([4., 1., -2.])
Conclusion
PyTorch provides flexible element-wise subtraction through torch.sub() or the minus operator. Broadcasting allows subtraction between tensors of different dimensions, making operations intuitive and efficient.
