0
0
PyTorchml~20 mins

Training loop structure in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Training Loop 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 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))
A0.0
B0.5
C0.25
D0.75
Attempts:
2 left
πŸ’‘ Hint
Remember the initial weights of nn.Linear are random, so the loss won't be zero.
❓ Model Choice
intermediate
1: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?
Aoptimizer.zero_grad()
Bloss.backward()
Coptimizer.step()
Dcriterion(outputs, targets)
Attempts:
2 left
πŸ’‘ Hint
Think about which function applies the calculated gradients to the weights.
❓ Hyperparameter
advanced
1: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?
ATraining may become unstable and loss might not decrease
BTraining will always be faster and more stable
CModel will automatically generalize better
DLoss function will change to a different type
Attempts:
2 left
πŸ’‘ Hint
Think about how too large steps affect finding the minimum loss.
πŸ”§ Debug
advanced
2: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
ACalling optimizer.step() twice without zero_grad causes error due to stale gradients
Bloss.backward() is missing before optimizer.step()
Coptimizer.zero_grad() is called after optimizer.step()
Dinputs and targets have mismatched shapes
Attempts:
2 left
πŸ’‘ Hint
Check how many times optimizer.step() is called without clearing gradients.
🧠 Conceptual
expert
1: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()?
ATo zero out the loss value before backpropagation
BTo initialize model weights to zero
CTo clear the optimizer’s learning rate cache
DTo reset gradients so they don’t accumulate across batches
Attempts:
2 left
πŸ’‘ Hint
Think about what happens if gradients from previous batches are not cleared.