Challenge - 5 Problems
Validation Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple PyTorch validation loop
What will be the printed output after running this PyTorch validation loop snippet?
PyTorch
import torch from torch import nn model = nn.Linear(2, 1) model.eval() inputs = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) targets = torch.tensor([[1.0], [2.0]]) criterion = nn.MSELoss() with torch.no_grad(): outputs = model(inputs) loss = criterion(outputs, targets) print(f"Loss: {loss.item():.4f}")
Attempts:
2 left
💡 Hint
Remember that the model is untrained and initialized randomly, so the loss won't be zero.
✗ Incorrect
The model is a linear layer with random weights. The mean squared error loss between the model's output and targets will be around 0.5 for this input.
❓ Model Choice
intermediate1:30remaining
Choosing the correct model mode during validation
Which PyTorch model mode should be set during the validation loop to ensure correct behavior of layers like dropout and batch normalization?
Attempts:
2 left
💡 Hint
Think about which mode disables dropout and uses running statistics for batch norm.
✗ Incorrect
During validation, model.eval() sets the model to evaluation mode, disabling dropout and using running statistics for batch normalization, which is the correct behavior.
❓ Metrics
advanced1:30remaining
Calculating average validation accuracy
Given a validation loop that accumulates correct predictions and total samples, which formula correctly computes the average validation accuracy?
Attempts:
2 left
💡 Hint
Accuracy is the fraction of correct predictions out of all predictions.
✗ Incorrect
Accuracy is calculated as the number of correct predictions divided by the total number of samples evaluated.
🔧 Debug
advanced2:00remaining
Identifying the error in this validation loop snippet
What error will this PyTorch validation loop code raise?
PyTorch
model.eval() correct = 0 total = 0 for inputs, labels in val_loader: outputs = model(inputs) predicted = torch.argmax(outputs, dim=1) correct += (predicted == labels).sum().item() total += labels.size(0) accuracy = correct / total print(f"Validation accuracy: {accuracy:.2f}")
Attempts:
2 left
💡 Hint
Check the shapes of predicted and labels tensors before comparison.
✗ Incorrect
torch.argmax without dimension returns a single value, but labels is a batch tensor. Comparing them causes a shape mismatch and TypeError.
🧠 Conceptual
expert2:00remaining
Purpose of torch.no_grad() in validation loops
Why is it important to use torch.no_grad() during the validation loop in PyTorch?
Attempts:
2 left
💡 Hint
Think about what happens when gradients are not needed.
✗ Incorrect
torch.no_grad() disables gradient tracking, which reduces memory use and speeds up validation since no backward pass is needed.