When the Network Memorizes

Real data at last: 768 women from the Pima Indians Diabetes study, six clinical measurements each, and whether they developed diabetes within five years. Nothing here follows a hidden rule. Give a big network only a hundred patients and it will learn every one of them by heart, and its answers for new patients will get worse the longer it trains. That is overfitting, and this page lets you cause it and cure it.

Experiment

training patients
network
weight decay
Presets:
Press Train. The 200 validation patients are the same whatever you choose, so runs can be compared.

The network, weight by weight

Every line is one weight: darker means larger, dotted means negative. Train the overfitting preset and watch the lines darken as the weights grow to encode individual patients; train with weight decay and they stay faint.

Loss per epoch

training patientsvalidation patients (200, never trained on)best validation epoch (early stopping)

Mean cross-entropy after every epoch. The dotted grey line is the loss of a model that ignores the patient and just says "no diabetes" with the training-set frequency: any curve above it is doing worse than guessing.

epochtrain losstrain accval lossval acc

What is going on

Memorizing versus learning. A network with 32 → 32 hidden neurons has over 1,300 weights. With 100 patients it has more than enough freedom to find some contortion of the six measurements that separates exactly those 100 women, right down to the noise in their records. That contortion is not diabetes; it is those particular patients. Training loss goes to zero, and the loss on women it has never seen climbs and climbs.

The validation set is the only honest judge. Training loss can only tell you how well the network fits what it was shown. The 200 held-out patients tell you whether it learned anything transferable. This is why every real project splits its data before training, and why the standardization numbers (mean and spread of each measurement) are computed from the training rows only: the validation rows must not leak into the model in any way.

Four remedies, all in the presets above. Use a smaller network, so there is less freedom to memorize. Use more data, so memorizing becomes harder than learning the pattern. Add weight decay, which shrinks every weight a little on each step and stops any one of them growing large enough to encode a single patient. Or stop early: keep the weights from the epoch where validation loss was lowest, marked in green, and throw the rest of the training away.

Try each one and watch what it actually does. On its own, each of the first three only softens or delays the problem over 300 epochs: five times the data still lets the big network memorize eventually, and decay alone still leaves a gap. Combined, more data and weight decay hold the two curves together for the whole run. Early stopping works in every setting, which is why it is used in practically every real training job.

The ceiling is low, and that is real too. The best any of these runs reaches is about 75 to 78 percent of validation patients right. Always saying "no" would score 65 percent. This dataset is noisy, some measurements are missing, and diabetes is not fully predictable from six numbers. No amount of training pushes past that, and a model that claims to has memorized something.

Look at one validation patient

Standardizing is the one step you can check by hand: z = (value − training mean) ÷ training spread. A z of 0 is average, +2 is unusually high.

The data and how it was cleaned

Each row is one woman of Pima heritage aged 21 or over, from the US National Institute of Diabetes and Digestive and Kidney Diseases study. Columns: pregnancies, plasma glucose, diastolic blood pressure, triceps skin fold, serum insulin, body mass index, diabetes pedigree (family history score), age, and the outcome.

measurementzeroskept?

A recorded zero for glucose, blood pressure or BMI means "not measured". Skin fold and insulin are missing for so many women that we drop them; the other zeros are replaced with the median of the real training values. Rows were shuffled once with a fixed seed; the last 200 are the validation set in every experiment.

#pregglucbpBMIpedigreeagediabeticp

Same three moves as always

Forward pass, error signal p − y, blame passed back one layer at a time, every weight nudged against its gradient, learning rate 0.01. The only new line is weight decay: w ← w − η·(∂L/∂w + λ·w), which pulls every weight toward zero by a fraction λ each step. The script nn5.py reproduces these runs.

Why the loss curves look jagged

Plain stochastic gradient descent updates after every single patient, so the network is nudged a hundred times per epoch in a hundred slightly different directions. With a big network and a small learning rate the curve is a random walk downhill, and on validation data a walk uphill once memorizing starts.