Tensors are like multi-dimensional arrays. We use operations like add, mul, and matmul to combine or transform data in machine learning.
Tensor operations (add, mul, matmul) in PyTorch
Introduction
Syntax
PyTorch
import torch # Add two tensors result = torch.add(tensor1, tensor2) # Multiply two tensors element-wise result = torch.mul(tensor1, tensor2) # Matrix multiply two tensors result = torch.matmul(tensor1, tensor2)
All tensors must have compatible shapes for these operations.
Matrix multiplication follows linear algebra rules, not element-wise multiplication.
Examples
PyTorch
a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) add_result = torch.add(a, b)
PyTorch
a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) mul_result = torch.mul(a, b)
PyTorch
a = torch.tensor([[1, 2], [3, 4]]) b = torch.tensor([[5, 6], [7, 8]]) matmul_result = torch.matmul(a, b)
Sample Model
This program shows how to add, multiply element-wise, and matrix multiply two 2x2 tensors.
PyTorch
import torch # Define two tensors x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) # Add tensors add = torch.add(x, y) # Multiply tensors element-wise mul = torch.mul(x, y) # Matrix multiply tensors matmul = torch.matmul(x, y) print('Add result:\n', add) print('Mul result:\n', mul) print('Matmul result:\n', matmul)
Important Notes
Use torch.add and torch.mul for element-wise operations.
Use torch.matmul for matrix multiplication, which is different from element-wise multiplication.
Shapes must match or be broadcastable for add and mul; for matmul, inner dimensions must align.
Summary
Tensors hold data in multi-dimensional arrays.
Use add and mul for element-wise operations.
Use matmul for matrix multiplication following linear algebra.
Practice
1. Which PyTorch function is used for element-wise multiplication of two tensors?
easy
Solution
Step 1: Understand element-wise multiplication
Element-wise multiplication multiplies each element of one tensor by the corresponding element of another tensor.Step 2: Identify the correct PyTorch function
torch.mul()performs element-wise multiplication, whiletorch.matmul()is for matrix multiplication.Final Answer:
torch.mul() -> Option DQuick Check:
Element-wise multiply = torch.mul() [OK]
Hint: Element-wise multiply uses torch.mul() not matmul [OK]
Common Mistakes:
- Confusing matmul with element-wise multiply
- Using torch.add() instead of multiply
- Trying torch.dot() for element-wise multiply
2. Which of the following is the correct syntax to add two tensors
a and b in PyTorch?easy
Solution
Step 1: Identify the addition operation
Adding two tensors element-wise can be done withtorch.add(a, b)ora + b.Step 2: Check syntax correctness
torch.add(a, b)is the correct function call.c = a + b + 1adds 1 extra, so it's incorrect.Final Answer:
c = torch.add(a, b) -> Option AQuick Check:
Tensor add syntax = torch.add(a, b) [OK]
Hint: Use torch.add(a, b) or a + b for tensor addition [OK]
Common Mistakes:
- Adding extra numbers accidentally
- Using mul or matmul instead of add
- Incorrect function call syntax
3. What is the output of the following PyTorch code?
import torch x = torch.tensor([[1, 2], [3, 4]]) y = torch.tensor([[5, 6], [7, 8]]) z = torch.matmul(x, y) print(z)
medium
Solution
Step 1: Perform matrix multiplication manually
Multiply rows ofxby columns ofy:
First row: (1*5 + 2*7) = 19, (1*6 + 2*8) = 22
Second row: (3*5 + 4*7) = 43, (3*6 + 4*8) = 50Step 2: Confirm output tensor
The result tensor is[[19, 22], [43, 50]].Final Answer:
tensor([[19, 22], [43, 50]]) -> Option CQuick Check:
Matrix multiply result = [[19, 22], [43, 50]] [OK]
Hint: Multiply rows by columns for matmul output [OK]
Common Mistakes:
- Adding elements instead of multiplying
- Confusing element-wise multiply with matmul
- Mixing row and column indices
4. Identify the error in this PyTorch code snippet:
import torch x = torch.tensor([1, 2, 3]) y = torch.tensor([[4, 5, 6], [7, 8, 9]]) z = torch.matmul(x, y) print(z)
medium
Solution
Step 1: Check tensor shapes
xshape is (3,),yshape is (2, 3). For matmul, inner dimensions must match: (N, M) x (M, P).Step 2: Identify mismatch
Here,xis 1D with length 3,yis 2D with shape (2, 3). The inner dimensions don't align for matmul.Final Answer:
Shape mismatch for matrix multiplication -> Option AQuick Check:
Matmul needs matching inner dims = shape mismatch [OK]
Hint: Check tensor shapes before matmul to avoid mismatch [OK]
Common Mistakes:
- Assuming 1D and 2D tensors always multiply
- Ignoring shape rules for matmul
- Thinking matmul works like element-wise multiply
5. Given two tensors
a = torch.tensor([[1, 2], [3, 4]]) and b = torch.tensor([[2, 0], [1, 2]]), which code correctly computes the element-wise product and then adds the matrix product of a and b?hard
Solution
Step 1: Understand the operations order
First, compute element-wise product:torch.mul(a, b). Then compute matrix product:torch.matmul(a, b).Step 2: Add the two results
Usetorch.add()to sum the element-wise and matrix products.Final Answer:
torch.add(torch.mul(a, b), torch.matmul(a, b)) -> Option BQuick Check:
Element-wise mul + matmul = torch.add(torch.mul(), torch.matmul()) [OK]
Hint: Add element-wise mul and matmul results with torch.add() [OK]
Common Mistakes:
- Mixing order of operations
- Using mul on sums instead of add on products
- Confusing arguments order in add or mul
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
