What is the purpose of the forward pass in a neural network?
The forward pass calculates the output of the network by passing input data through its layers. It shows how the input transforms into predictions.
Click to reveal answer
beginner
What does the loss function measure in training a model?
The loss function measures how far the model's predictions are from the true answers. It gives a number showing the error to minimize.
Click to reveal answer
intermediate
What happens during the backward pass (backpropagation)?
During the backward pass, the model calculates gradients that show how to change each weight to reduce the loss. It moves backward through the network.
Click to reveal answer
beginner
What does the optimizer's step() function do?
The step() function updates the model's weights using the gradients calculated in the backward pass. It moves weights to reduce the loss.
Click to reveal answer
intermediate
Why do we zero the gradients before the backward pass?
We zero the gradients to clear old gradient values. Otherwise, gradients would accumulate and give wrong updates.
Click to reveal answer
What is the first step in training a neural network?
APerforming the forward pass
BCalling optimizer.step()
CCalculating gradients
DZeroing the gradients
✗ Incorrect
Training starts by passing input data through the network to get predictions, called the forward pass.
Which function calculates how wrong the model's predictions are?
Aoptimizer.step()
Bloss function
Cbackward()
Dzero_grad()
✗ Incorrect
The loss function measures the difference between predictions and true values.
What does backward() do in PyTorch?
ACalculates gradients
BClears gradients
CUpdates weights
DComputes loss
✗ Incorrect
backward() computes gradients of the loss with respect to model parameters.
Why do we call optimizer.zero_grad() before backward()?
ATo update weights
BTo reset weights
CTo calculate loss
DTo clear old gradients
✗ Incorrect
zero_grad() clears previous gradients to avoid accumulation.
What does optimizer.step() do?
ACalculates loss
BComputes gradients
CUpdates model weights
DClears gradients
✗ Incorrect
optimizer.step() updates the model's weights using the gradients.
Explain the sequence of steps in one training iteration of a PyTorch model including forward pass, loss, backward, and step.
Think about what happens from input to weight update.
You got /5 concepts.
Why is it important to zero gradients before calling backward in PyTorch training?
Consider what happens if you don't clear gradients.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the forward pass in a PyTorch model?
easy
A. To compute gradients for backpropagation
B. To update the model weights after training
C. To run input data through the model and get predictions
D. To calculate the loss between predictions and targets
Solution
Step 1: Understand the forward pass role
The forward pass takes input data and processes it through the model layers to produce predictions.
Step 2: Differentiate from other steps
Updating weights is done by the optimizer step, loss calculation measures error, and backward computes gradients.
Final Answer:
To run input data through the model and get predictions -> Option C
Quick Check:
Forward pass = predictions [OK]
Hint: Forward pass means getting predictions from inputs [OK]
Common Mistakes:
Confusing forward pass with weight update
Mixing loss calculation with forward pass
Thinking backward computes predictions
2. Which of the following is the correct PyTorch code to perform a backward pass on a loss tensor named loss?
easy
A. loss.step()
B. loss.backward()
C. loss.forward()
D. loss.update()
Solution
Step 1: Identify the backward method
In PyTorch, calling loss.backward() computes gradients for all tensors with requires_grad=True.
Step 2: Check other options
step() is for optimizers, forward() is for models, and update() is not a PyTorch method.
Final Answer:
loss.backward() -> Option B
Quick Check:
Backward pass uses loss.backward() [OK]
Hint: Use loss.backward() to compute gradients [OK]
Common Mistakes:
Using step() on loss instead of optimizer
Calling forward() on loss tensor
Trying to update loss directly
3. Given the following PyTorch code snippet, what will be printed?
import torch
import torch.nn as nn
model = nn.Linear(2, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
inputs = torch.tensor([[1.0, 2.0]])
targets = torch.tensor([[1.0]])
outputs = model(inputs)
loss_fn = nn.MSELoss()
loss = loss_fn(outputs, targets)
loss.backward()
optimizer.step()
print(loss.item())
medium
A. A positive float value representing the loss before weight update
B. Zero, because optimizer.step() resets the loss
C. A tensor object, not a float
D. An error because backward() must be called after step()
Solution
Step 1: Understand loss calculation and printing
The loss is computed before backward and step, so loss.item() returns the scalar loss value as a float.
Step 2: Confirm optimizer.step() effect
Calling optimizer.step() updates weights but does not change the already computed loss tensor value.
Final Answer:
A positive float value representing the loss before weight update -> Option A
Quick Check:
loss.item() shows loss before update [OK]
Hint: Loss printed is before optimizer updates weights [OK]
Common Mistakes:
Thinking loss becomes zero after optimizer.step()
Expecting loss to be a tensor when printed
Calling backward() after step() causes error
4. Identify the error in this PyTorch training snippet:
D. Calling optimizer.step() before loss.backward()
Solution
Step 1: Check order of backward and step
Gradients must be computed by loss.backward() before calling optimizer.step() to update weights.
Step 2: Analyze given code order
The code calls optimizer.step() before loss.backward(), so weights update happens without gradients.
Final Answer:
Calling optimizer.step() before loss.backward() -> Option D
Quick Check:
Backward before step is required [OK]
Hint: Always call loss.backward() before optimizer.step() [OK]
Common Mistakes:
Swapping backward and step order
Forgetting to zero gradients
Assuming step() computes gradients
5. You want to train a PyTorch model for one step with input x and target y. Which sequence of commands correctly performs forward pass, loss calculation, backward pass, and weight update?
hard
A. optimizer.zero_grad(); outputs = model(x); loss = loss_fn(outputs, y); loss.backward(); optimizer.step()
B. outputs = model(x); loss = loss_fn(outputs, y); optimizer.step(); loss.backward(); optimizer.zero_grad()
C. loss = loss_fn(x, y); outputs = model(x); optimizer.zero_grad(); loss.backward(); optimizer.step()
D. optimizer.step(); optimizer.zero_grad(); outputs = model(x); loss = loss_fn(outputs, y); loss.backward()
Solution
Step 1: Understand correct training step order
The correct order is: clear gradients, forward pass, compute loss, backward pass, then update weights.
Step 2: Check each option's sequence
optimizer.zero_grad(); outputs = model(x); loss = loss_fn(outputs, y); loss.backward(); optimizer.step() follows the correct order. Others call optimizer.step() too early or compute loss before forward pass.
Final Answer:
optimizer.zero_grad(); outputs = model(x); loss = loss_fn(outputs, y); loss.backward(); optimizer.step() -> Option A
Quick Check:
Zero grad -> forward -> loss -> backward -> step [OK]
Hint: Clear grads, forward, loss, backward, then step [OK]
Common Mistakes:
Calling optimizer.step() before backward
Not zeroing gradients before backward
Computing loss before forward pass
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.