Challenge - 5 Problems
Training Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this PyTorch training loop snippet?
Consider the following PyTorch training loop snippet. What will be the printed loss value after one batch?
PyTorch
import torch import torch.nn as nn model = nn.Linear(2, 1) criterion = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.1) inputs = torch.tensor([[1.0, 2.0]]) targets = torch.tensor([[1.0]]) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step() print(round(loss.item(), 4))
Attempts:
2 left
π‘ Hint
Remember the initial weights of nn.Linear are random, so the loss won't be zero.
β Incorrect
The initial weights are random, so the output will not match the target exactly. The MSE loss will be a positive value around 0.5 for this input and target.
β Model Choice
intermediate1:30remaining
Which part of the training loop updates the model's weights?
In a typical PyTorch training loop, which line is responsible for updating the model's weights?
Attempts:
2 left
π‘ Hint
Think about which function applies the calculated gradients to the weights.
β Incorrect
optimizer.step() applies the gradients computed by loss.backward() to update the model's weights.
β Hyperparameter
advanced1:30remaining
What effect does increasing the learning rate have in a training loop?
If you increase the learning rate in your training loop, what is the most likely effect on training?
Attempts:
2 left
π‘ Hint
Think about how too large steps affect finding the minimum loss.
β Incorrect
A high learning rate can cause the model to overshoot minima, making training unstable and preventing loss from decreasing properly.
π§ Debug
advanced2:00remaining
Why does this training loop raise an error?
Examine this PyTorch training loop snippet. Why does it raise a RuntimeError?
PyTorch
import torch import torch.nn as nn model = nn.Linear(2, 1) criterion = nn.MSELoss() 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 = criterion(outputs, targets) loss.backward() optimizer.step() optimizer.step() # Called twice without zero_grad
Attempts:
2 left
π‘ Hint
Check how many times optimizer.step() is called without clearing gradients.
β Incorrect
Calling optimizer.step() twice without clearing gradients accumulates gradients incorrectly, causing a RuntimeError.
π§ Conceptual
expert1:30remaining
Why do we call optimizer.zero_grad() at the start of each training iteration?
In PyTorch training loops, why is optimizer.zero_grad() called before computing the loss and calling loss.backward()?
Attempts:
2 left
π‘ Hint
Think about what happens if gradients from previous batches are not cleared.
β Incorrect
optimizer.zero_grad() clears old gradients; otherwise, gradients accumulate and cause incorrect updates.