Implement PyTorch training loop
AmazonApplied Scientist · OnsiteLast updated
This question evaluates practical implementation skills in PyTorch, focusing on model and device management, batch-wise training mechanics, gradient handling, and optimizer interaction.
Implement a basic PyTorch training loop
You are given a PyTorch neural network model, a DataLoader that yields (inputs, targets) batches, an optimizer, and a loss function.
Write a function train(model, train_loader, optimizer, loss_fn, device, num_epochs) that:
- Moves the model and input batches to the specified device (CPU or GPU).
- Runs for
num_epochsepochs. - For each batch, performs a forward pass, computes the loss, runs backpropagation, and updates the model parameters.
- Properly zeros gradients at the right time.
- Optionally prints or returns the average training loss per epoch.
Clearly show the order of operations inside the training loop (zeroing gradients, forward pass, loss computation, backward pass, optimizer step).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.