How to Train a Neural Network with PyTorch: A Step-by-Step Guide
PyTorch is one of the most flexible deep learning frameworks, making it easy to train neural networks with dynamic computation graphs. In this tutorial, you’ll learn the essential steps to train a neural network on custom data, from loading data to saving the trained model.
Before training, understand that PyTorch uses torch.Tensor for data and autograd for automatic differentiation. Every tensor can track operations for gradient computation, which powers the training loop.
1. Prepare Your Data
Use torch.utils.data.Dataset and DataLoader to handle batching, shuffling, and parallel loading. For image data, apply transformations with torchvision.transforms.
- Define a custom Dataset or use a built-in one like
torchvision.datasets.MNIST. - Create a DataLoader with
batch_size,shuffle=Truefor training.
2. Define the Model
Subclass nn.Module and define your layers in __init__ and the forward pass in forward.
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(784, 10)
def forward(self, x):
return self.fc(x)
3. Choose Loss and Optimizer
Select an appropriate loss function from torch.nn (e.g., CrossEntropyLoss for classification) and an optimizer from torch.optim (e.g., Adam or SGD).
4. Train the Model
Iterate over epochs and batches, performing the standard training loop:
- Zero gradients with
optimizer.zero_grad(). - Forward pass:
outputs = model(inputs). - Compute loss:
loss = criterion(outputs, labels). - Backward pass:
loss.backward(). - Update weights:
optimizer.step().
5. Evaluate and Save
After training, evaluate on a validation set to check generalization. Save the model using torch.save(model.state_dict(), 'model.pth').
In just a few lines of code, you can train a neural network with PyTorch. Master these core components, and you’ll be ready to tackle more advanced architectures like CNNs and transformers.