Challenge - 5 Problems
Tensor Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of element-wise addition and multiplication
What is the output of the following PyTorch code?
PyTorch
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) add_result = a + b mul_result = a * b print(add_result.tolist(), mul_result.tolist())
Attempts:
2 left
💡 Hint
Remember that + and * between tensors perform element-wise operations.
✗ Incorrect
The + operator adds corresponding elements: 1+4=5, 2+5=7, 3+6=9. The * operator multiplies element-wise: 1*4=4, 2*5=10, 3*6=18.
❓ Model Choice
intermediate1:30remaining
Choosing the correct operation for matrix multiplication
Which PyTorch operation correctly performs matrix multiplication between two 2D tensors?
Attempts:
2 left
💡 Hint
Matrix multiplication is not element-wise addition or multiplication.
✗ Incorrect
torch.matmul performs matrix multiplication. torch.add and + do element-wise addition. torch.mul does element-wise multiplication.
🔧 Debug
advanced2:00remaining
Identify the error in tensor multiplication
What error will the following code raise?
PyTorch
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([[4, 5, 6], [7, 8, 9]]) result = a * b print(result)
Attempts:
2 left
💡 Hint
Check if the shapes of the tensors are compatible for element-wise multiplication.
✗ Incorrect
No error. Tensor a has shape (3,) and tensor b has shape (2,3). The shapes are broadcastable: PyTorch pads a on the left to (1,3), and the leading dimension 1 broadcasts to 2, resulting in element-wise multiplication with output shape (2,3): tensor([[4,10,18],[14,16,27]]).
❓ Metrics
advanced1:30remaining
Calculate output shape after matrix multiplication
Given two tensors a and b with shapes (4, 3) and (3, 5), what is the shape of torch.matmul(a, b)?
Attempts:
2 left
💡 Hint
Matrix multiplication shape rule: (m, n) x (n, p) = (m, p).
✗ Incorrect
The inner dimensions 3 match. The output shape is (4, 5).
🧠 Conceptual
expert2:30remaining
Difference between torch.mul and torch.matmul
Which statement correctly describes the difference between torch.mul and torch.matmul?
Attempts:
2 left
💡 Hint
Think about how each function treats tensor dimensions and multiplication rules.
✗ Incorrect
torch.mul multiplies each element of one tensor by the corresponding element of another tensor (element-wise). torch.matmul performs matrix multiplication, combining rows and columns as in linear algebra.