PyTorch — Arithmetic Operations And Matrix Multiplication
In PyTorch, arithmetic operations refer to basic mathematical operations such as addition, subtraction, multiplication, and division performed on tensors. Tensors are fundamental data structures in PyTorch, similar to arrays in NumPy or matrices in linear algebra.
Here’s how you can perform arithmetic operations in PyTorch:
Basic code which create two tensors
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
Addition:
result = x + y
print(result) # Output: tensor([5, 7, 9])
tensor = torch.tensor([1,3,2])
print(tensor + 10) # Output: tensor([11, 13, 12])
Subtraction:
result = x - y
print(result) # Output: tensor([-3, -3, -3])
tensor = torch.tensor([11,13,12])
print(tensor -10) # Output: tensor([1, 3, 2])
Multiplication:
result = x * y
print(result) # Output: tensor([ 4, 10, 18])
tensor = torch.tensor([1,3,2])
print(tensor * 10) # Output: tensor([10, 30, 20])
Division:
result = y / x
print(result) # Output: tensor([4, 2.5, 2])
tensor = torch.tensor([4,30,20])
print(tensor/2) # Output: tensor([ 2., 15., 10.])
PyTorch also supports in-place operations, where the result is stored in the original tensor:
x.add_(y) # Equivalent to x = x + y
print(x) # Output: tensor([5, 7, 9])
Differeence between tensor.add and tensor.add_
tensor = torch.tensor([1,3,2])
tensor.add(10)
print(tensor) # Output : tensor([1, 3, 2])
tensor = torch.tensor([1,3,2])
tensor.add_(10)
print(tensor) #Output : tensor([11, 13, 12])
Same is difference between
tensor.sub and tensor.sub_
tensor.mul and tensor.mul_
tensor.mul and tensor.mul_
tensor.div and tensor.div_
Matrix multiplication
This is a little different then other methods, in this we do a dot product of two matrix
A = torch.tensor([1, 2,3])
B = torch.tensor([1,2, 3])
# Matrix multiplication using @
D = A @ B
print(D)
A = torch.tensor([1, 2,3])
B = torch.tensor([1,2, 3])
# Matrix multiplication usingtorch.matmul()
D = torch.matmul(A,B)
print(D)
Rule if Matrix Mul
- The inner dimesions should be same
- The result matrix will be of size outer dimensions
A common problem we encounter is when we have to multiply two matrix, they don’t follow the rule 1 ( are not in correct shape) to solve that we use Tranpose of a matrix