Session 22¶
Topic 3: Neural networks and PyTorch¶
- introduction to PyTorch
- automatic differentiation
- basic neural network models and training
- optimization hyperparameters π
- distributed training with data parallelism
- distributed training on multiple compute nodes
Today¶
Optimization hyperparameters¶
- learning rate and batch size
- number of epochs and early stopping
Optimization hyperparameters¶
From learning β understanding¶
so far:
- tensors β¦ store data
- autograd β¦ automatic differentiation
- models β¦ parameterized functions
- training β¦ how the model learns
β now we ask: why does training sometimes workβ¦
and sometimes fail?
The hidden knobs¶
training is controlled by hyperparameters ποΈ
- they are not learned from data
- but chosen by us
important examples:
- learning rate
- batch size
- number of epochs
- early stopping criteria
Why should we care?¶
same model, same data, butβ¦
other hyperparameters give completely different results
such as:
- loss converges fast β‘
- loss converges slowly π’
- loss diverges π₯
- overfits training data β
Recall: the training loop¶
β hyperparameters control how this loop behaves
The training loop in code¶
train_loader = DataLoader(..., batch_size=256) # β set batch size
optimizer = SGD(model.parameters(), lr=0.001) # β set learning rate
for epoch in range(num_epochs): # β set num. epochs
for i, data in enumerate(training_loader):
# perform one training step
if ...:
break # β early stopping
Demo: training on MNIST dataset¶
the βhello worldβ of neural networks
- dataset: handwritten digits (0β9)
- grayscale images (28 Γ 28 pixels)
- 10 classes β classification problem
Goal: train a simple convolutional network that recognizes digits from images
Learning rate¶
optimizer = SGD(model.parameters(), lr=0.001)
learning rate means step size of optimization
- too small β very slow learning π΄
- too large β unstable / diverging π₯
β most important hyperparameter
Batch size¶
train_loader = DataLoader(..., batch_size=256)
how many samples per gradient descent update
- small batch β noisy gradients π²
- large batch β stable but expensive π°
β trade-off: speed (increase batch size) vs generalization (decrease batch size)
Number of epochs¶
for epoch in range(num_epochs):
...
balance how long we train:
- too few epochs β underfitting
- too many epochs β overfitting
π‘ comes from the neural nets ability to approximate any mapping (features β targets)
Early stopping¶
a validation data set (not used for optimization):
- stop when validation accuracy is βgood enoughβ
- and stays stable for a few epochs
(βgood enoughβ is another hyperparameter)
β prevents wasting time + reduces overfitting
What we will explore π¬¶
you will run experiments by changing:
- learning rate
- batch size
- number of epochs
- early stopping settings
and observe:
- training speed
- stability
- final accuracy
Key idea¶
training is a dynamical process and hyperparameters shape:
- the path through parameter space
- the speed of convergence
- the final solution
Your hands-on tasks¶
donβt just run the code, but probe it, try:
batch_size= 8 β¦ 512- fewer / more
epochswith early stopping on / off lr= 0.001 vs 0.0001
ask:
- what changed?
- and why?
Key takeaways¶
- models learn from data
- but training behavior depends on hyperparameters
β understanding them gives you control over training
Looking ahead¶
from learning β scaling
- distributed training with PyTorch DDP
- converting single-GPU code to multi-GPU