Challenge - 5 Problems
Forward Pass Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple forward pass and loss calculation
What is the value of the loss after running this PyTorch code snippet?
PyTorch
import torch import torch.nn as nn model = nn.Linear(2, 1) inputs = torch.tensor([[1.0, 2.0]]) target = torch.tensor([[1.0]]) output = model(inputs) criterion = nn.MSELoss() loss = criterion(output, target) print(loss.item())
Attempts:
2 left
💡 Hint
The loss is the mean squared error between the model output and the target.
✗ Incorrect
The model produces an output tensor of shape (1,1). The MSELoss compares this output with the target tensor of the same shape and returns a positive scalar loss value.
❓ Model Choice
intermediate2:00remaining
Choosing the correct optimizer step sequence
Which sequence of PyTorch commands correctly performs a training step including forward pass, loss calculation, backward pass, and optimizer step?
Attempts:
2 left
💡 Hint
Remember to clear gradients before backward pass and update weights after backward.
✗ Incorrect
The correct order is to zero gradients, do forward pass, compute loss, call backward to compute gradients, then call optimizer.step() to update weights.
🔧 Debug
advanced2:00remaining
Identify the error in backward pass usage
What error will this PyTorch code raise when running the backward pass?
PyTorch
import torch import torch.nn as nn model = nn.Linear(3, 1) inputs = torch.tensor([[1.0, 2.0, 3.0]]) target = torch.tensor([[1.0]]) output = model(inputs) criterion = nn.MSELoss() loss = criterion(output, target) loss.backward(retain_graph=True) loss.backward()
Attempts:
2 left
💡 Hint
Calling backward twice requires special care with retain_graph.
✗ Incorrect
Calling loss.backward() twice without retain_graph=True on the second call causes a RuntimeError because the computation graph is freed after the first backward call.
❓ Hyperparameter
advanced2:00remaining
Effect of learning rate on optimizer step
If you increase the learning rate in the optimizer, what is the most likely effect on the training step?
Attempts:
2 left
💡 Hint
Higher learning rates mean bigger steps in weight updates.
✗ Incorrect
Increasing learning rate causes larger weight updates which can lead to unstable training and divergence if too high.
🧠 Conceptual
expert2:00remaining
Why zero gradients before backward pass?
Why is it necessary to call optimizer.zero_grad() before loss.backward() in a training loop?
Attempts:
2 left
💡 Hint
Think about how gradients behave across multiple backward calls.
✗ Incorrect
PyTorch accumulates gradients on each backward call, so zero_grad() clears old gradients to avoid mixing updates from different batches.