Bird
Raised Fist0
PyTorchml~20 mins

First PyTorch computation - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PyTorch Computation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PyTorch tensor operation?
Consider the following PyTorch code that creates two tensors and adds them. What is the output tensor?
PyTorch
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
z = x + y
print(z)
Atensor([5, 7, 9])
Btensor([4, 7, 9])
Ctensor([1, 2, 3, 4, 5, 6])
Dtensor([3, 3, 3])
Attempts:
2 left
💡 Hint
Remember that adding two tensors of the same shape adds their elements one by one.
❓ Model Choice
intermediate
2:00remaining
Choosing the right PyTorch tensor for GPU computation
You want to perform fast matrix multiplication on a GPU using PyTorch. Which tensor creation method ensures the tensor is on the GPU?
Atorch.tensor([[1, 2], [3, 4]], dtype=torch.float64)
Btorch.tensor([[1, 2], [3, 4]])
Ctorch.tensor([[1, 2], [3, 4]], device='cuda')
Dtorch.tensor([[1, 2], [3, 4]], requires_grad=True)
Attempts:
2 left
💡 Hint
To use GPU, the tensor must be created or moved to the CUDA device.
❓ Metrics
advanced
2:00remaining
Interpreting training loss and accuracy in PyTorch
During training a classification model in PyTorch, you observe the following after one epoch: training loss = 0.8, training accuracy = 0.65. What does this mean?
AThe model is 80% accurate and 65% loss means it is underfitting.
BThe model is making correct predictions 65% of the time, and the loss value indicates how far predictions are from true labels.
CLoss and accuracy are unrelated; only accuracy matters for model quality.
DA loss of 0.8 means the model predictions are perfect, and accuracy of 0.65 is low.
Attempts:
2 left
💡 Hint
Loss measures prediction error; accuracy measures correct predictions percentage.
🔧 Debug
advanced
2:00remaining
Why does this PyTorch code raise an error?
What error does this PyTorch code raise and why? import torch x = torch.tensor([1, 2, 3]) y = torch.tensor([4, 5]) z = x + y
PyTorch
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5])
z = x + y
ARuntimeError: The tensors have different shapes and cannot be broadcast for addition.
BSyntaxError: Missing parentheses in print statement.
CTypeError: Unsupported operand types for +: 'Tensor' and 'Tensor'.
DNo error; output is tensor([5, 7, 3]).
Attempts:
2 left
💡 Hint
Check if the two tensors have the same shape or compatible shapes for addition.
🧠 Conceptual
expert
3:00remaining
Understanding autograd in PyTorch
Which statement best describes how PyTorch's autograd system works during backpropagation?
AAutograd requires the user to write explicit derivative functions for each operation.
BAutograd manually updates weights during training without needing gradients.
CAutograd only works with CPU tensors and ignores GPU tensors.
DAutograd records operations on tensors with requires_grad=True to build a computation graph for automatic differentiation.
Attempts:
2 left
💡 Hint
Think about how PyTorch tracks operations to compute gradients automatically.

Practice

(1/5)
1. What is a PyTorch tensor mainly used for?
easy
A. Holding numbers for math operations
B. Storing text data only
C. Creating web pages
D. Managing database connections

Solution

  1. Step 1: Understand the role of tensors in PyTorch

    PyTorch tensors are like containers that hold numbers for math operations, similar to arrays.
  2. Step 2: Compare options with tensor purpose

    Storing text data only, creating web pages, and managing database connections do not relate to math operations or number storage, so they are incorrect.
  3. Final Answer:

    Holding numbers for math operations -> Option A
  4. Quick Check:

    PyTorch tensor = numbers container [OK]
Hint: Tensors hold numbers for math, not text or web [OK]
Common Mistakes:
  • Thinking tensors store only text
  • Confusing tensors with web tools
  • Assuming tensors manage databases
2. Which of the following is the correct way to create a tensor with values [1, 2, 3] in PyTorch?
easy
A. torch.tensor([1, 2, 3])
B. torch.array([1, 2, 3])
C. torch.list([1, 2, 3])
D. torch.vector([1, 2, 3])

Solution

  1. Step 1: Recall PyTorch tensor creation syntax

    PyTorch uses torch.tensor() to create tensors from lists or arrays.
  2. Step 2: Check each option's validity

    torch.vector([1, 2, 3]), torch.array([1, 2, 3]), and torch.list([1, 2, 3]) use incorrect functions that do not exist in PyTorch.
  3. Final Answer:

    torch.tensor([1, 2, 3]) -> Option A
  4. Quick Check:

    Use torch.tensor() to create tensors [OK]
Hint: Use torch.tensor() to create tensors from lists [OK]
Common Mistakes:
  • Using torch.array which is not a PyTorch function
  • Trying torch.list or torch.vector which don't exist
  • Confusing PyTorch with NumPy syntax
3. What will be the output of this PyTorch code?
import torch
x = torch.tensor([2, 4])
y = torch.tensor([3, 1])
z = x + y
print(z)
medium
A. tensor([6, 4])
B. tensor([5, 5])
C. tensor([2, 4, 3, 1])
D. Error: cannot add tensors

Solution

  1. Step 1: Add tensors element-wise

    Adding tensors x and y adds each element: 2+3=5 and 4+1=5.
  2. Step 2: Confirm output format

    tensor([6, 4]) and tensor([2, 4, 3, 1]) show wrong sums or concatenation, and 'Error: cannot add tensors' is incorrect as addition is valid and produces tensor([5, 5]).
  3. Final Answer:

    tensor([5, 5]) -> Option B
  4. Quick Check:

    2+3=5 and 4+1=5 [OK]
Hint: Tensor addition is element-wise [OK]
Common Mistakes:
  • Thinking addition concatenates tensors
  • Mixing element-wise sum with multiplication
  • Expecting an error for valid addition
4. Identify the error in this PyTorch code snippet:
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5])
z = x + y
print(z)
medium
A. No error, code runs fine
B. Missing import statement
C. Incorrect tensor creation syntax
D. Tensors have different sizes, cannot add

Solution

  1. Step 1: Check tensor sizes

    x has 3 elements, y has 2 elements; PyTorch cannot add tensors of different sizes element-wise.
  2. Step 2: Identify error cause

    There is no missing import statement and tensor creation syntax is correct. 'No error, code runs fine' is wrong as code raises size mismatch error.
  3. Final Answer:

    Tensors have different sizes, cannot add -> Option D
  4. Quick Check:

    Tensor sizes must match for addition [OK]
Hint: Check tensor shapes before adding [OK]
Common Mistakes:
  • Ignoring size mismatch error
  • Assuming PyTorch auto-adjusts sizes
  • Thinking import or syntax is wrong
5. You want to multiply two tensors element-wise and then find the sum of all elements in the result. Which PyTorch code correctly does this?
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
# Choose the correct next line
hard
A. result = torch.dot(x, y).sum()
B. result = x.matmul(y)
C. result = (x * y).sum()
D. result = x + y.sum()

Solution

  1. Step 1: Understand element-wise multiplication and sum

    x * y multiplies each element: [1*4, 2*5, 3*6] = [4, 10, 18]. Then sum() adds all: 4+10+18=32.
  2. Step 2: Evaluate options

    torch.dot(x, y).sum() applies dot product then sum again (dot product is not element-wise multiplication, and summing a scalar is redundant). x.matmul(y) does matrix multiplication, not element-wise. x + y.sum() adds x to sum of y, not multiplication.
  3. Final Answer:

    result = (x * y).sum() -> Option C
  4. Quick Check:

    Element-wise multiply then sum = 32 [OK]
Hint: Use (x * y).sum() for element-wise multiply then sum [OK]
Common Mistakes:
  • Using matmul instead of element-wise multiply
  • Applying sum twice incorrectly
  • Adding tensors instead of multiplying
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard