Module 7 — Training Real Networks
Module 6 established that a deep network has the capacity to represent essentially any decision boundary. This expressive capacity has a corresponding cost: a network sufficiently flexible to separate the spirals is also sufficiently flexible to memorize the individual training points, including their noise component, and consequently to fail on data it has not encountered. This phenomenon is overfitting — the principal practical challenge in training real networks. This module presents the diagnostic procedure for detecting overfitting and three standard techniques for mitigating it.
Diagnostic: comparing training and validation loss
The standard diagnostic is to reserve a subset of the data — a validation set — that is not used during training, and to monitor its loss alongside the training loss:
- Training loss decreases monotonically — the network is fitting the data to which it has access.
- Validation loss initially decreases, then increases — the network is now fitting noise specific to the training set that does not generalize.
- The gap between the two curves is the empirical measure of overfitting. A small gap indicates a well-regularized model; a large and growing gap indicates memorization.
Three standard techniques constrain this gap:
- Weight decay (L2 regularization) — adds a penalty proportional to the squared magnitude of the weights, biasing the network toward simpler, smoother functions.
- Dropout — randomly deactivates a subset of neurons at each training step, preventing any single neuron from encoding a memorized training example.
- Early stopping — terminates training at the iteration that minimizes validation loss, before subsequent overfitting can occur.
The activity below trains a deliberately over-parameterized network on a small, noisy dataset, with the two loss curves diverging. Enabling weight decay and dropout and re-training reduces the gap between the curves and lowers the validation loss — the quantity relevant to generalization.
This activity needs JavaScript. The lesson below still covers everything.
from tensorflow.keras.layers import Dense, Dropout from tensorflow.keras.regularizers import l2 from tensorflow.keras.callbacks import EarlyStopping model.add(Dense(32, activation='relu', kernel_regularizer=l2(1e-3))) # weight decay model.add(Dropout(0.3)) # dropout: drop 30% of neurons model.fit(X, y, validation_split=0.3, # hold out 30% to watch callbacks=[EarlyStopping(patience=10)]) # stop when val stops improving
Each technique is configured with a single line of code. The validation_split argument produces the second loss curve displayed in the activity below; the remaining configuration constrains the gap between the curves.
Check your understanding
Answer a short set of questions on overfitting and regularization.
This activity needs JavaScript.