One step on the circled virginica raised its predicted probability from 0.450 to 0.530. That completes our recap of logistic regression.
Today we will cover
Loss curves and generalisation
ROC curves and decision thresholds
Support vector machines
Cross-validation
Loss curves
The landscape gives loss at possible weights. The curve on the right records loss at the weights we visited.
Definitions
Step changes the weights once.
Epoch means one pass through the training data. It can contain many steps.
Checkpoint records the weights at a particular point in the run.
In the sensor example we will use one full-batch step per epoch.
The training loss halves
Training loss falls from 0.40 to 0.20. Does the model now predict new examples better?
Fit the curve on the blue readings
The blue readings are the training data. Use them to fit the green curve.
Add the held-out readings
We kept the red readings aside during training. Score the same fitted curve on them to get the validation loss.
The hold-out loss tells us more
Both runs fit their training examples better. The hold-out curves tell us whether the improvement carries over to new examples.
Put some data aside before fitting
Each split has a job. Training examples supply the gradients. Validation and test examples measure performance on data outside the fit.
How much data should we hold out?
Which split leaves you with more examples to validate the model?
One per cent can be a large hold-out
Dataset size
Training
Validation
Test
1,000
600
200
200
1,000,000
980,000
10,000
10,000
60/20/20 was a common rule of thumb for smaller datasets. At large scale, a 98/1/1 split can still leave many examples for validation and test.
Choose hold-out sizes that let you measure the performance differences you care about.
Which set makes each decision?
Which data tell us how to update the weights?
Which data tell us which checkpoint to keep?
Which data give a final estimate of performance?
Training, validation and test
Split
When we use it
Example
Training
During the fit
Calculate the next gradient
Validation
While choosing settings
Choose a degree or stopping epoch
Test
After making those choices
Report the final performance
Repeatedly choosing a model using the test set would make it another validation set.
Fit the response of a sensor
These simulated readings include measurement noise. Fit a flexible curve to the blue points and score it on the orange points.
Epoch 1
The fit is still taking shape. The orange readings are held out of every update.
Epoch 17
The fitted curve now follows more of the broad pattern in the readings.
Epoch 250
The curve follows more of the small swings between training readings. Look at where the orange readings fall relative to it.
The training loss keeps falling
The model family and training set stay fixed. Each epoch gives the same model another update.
Now reveal the validation loss
Both losses fall early. After epoch 17, the validation loss rises while the training loss keeps falling.
Which checkpoint would you keep?
Epoch
Training MSE
Validation MSE
1
0.104
0.146
17
0.010
0.061
250
0.007
0.107
You need to predict new readings. Take a moment to choose a checkpoint.
Keep the best validation checkpoint
Epoch
Training MSE
Validation MSE
1
0.104
0.146
17
0.010
0.061
250
0.007
0.107
Choose epoch 17 and save those weights. Once all settings are decided, the test set gives the final check.
Should we stop at the first rise?
Validation loss often fluctuates. In this example, it rises at epoch 5. Would you stop training here?
Allow a few epochs for improvement
With a patience of three epochs, stop at epoch 10 and restore the best saved weights from epoch 7.
What if we change the model?
Keep the same readings. This time fit a separate polynomial for each degree.
A low degree gives few shapes to choose from. A high degree can follow more bends. What would you look for in the training and validation errors?
Degree 1 misses the bend
Even the best straight line has substantial error on both splits. This model underfits the broad shape.
Degree 5 follows the signal
This fit picks up the broad variation and reduces the loss on both splits.
Degree 8 follows finer detail
The extra flexibility brings the training loss down. Validation loss rises, including near the sparsely sampled edge.
One point for each finished fit
The horizontal axis is model degree. In this sweep, degree 5 has the lowest validation loss.
Bias and variance
Each thin line comes from a fresh sample of 20 readings from the same simulated sensor. Which model misses the signal in a consistent way? Which changes most with the sample?
Bias is a systematic miss
The mean of the straight-line fits sits above the true signal at the marked setting. A model that can bend gives the fit a way to follow the signal there.
Variance is sensitivity to the sample
At the same setting, the more flexible model gives quite different predictions when we fit it to different samples. More data or a less flexible model may make it more stable.
Bias, variance and test error
Fix the sensor setting \(x\). Let \(\hat f_D(x)\) be the prediction from training sample \(D\), and \(f(x)\) the true mean response.
The last term is measurement noise. The next slide reports standard deviation, the square root of variance.
Compare the fits at one sensor setting
At the marked setting (0.86), the true signal is 0.657 V.
Model
Mean prediction
Mean minus signal
SD across six fits
Degree 1
1.076 V
+0.419 V
0.058 V
Degree 12
0.645 V
-0.012 V
0.185 V
The straight line has a bigger systematic miss here. The flexible curve has more spread. Both contribute to prediction error.
Reading loss curves in practice
If both losses stay high
If both losses stay high
Then ask what counts as high. Compare with an attainable loss on the same task. If training has converged and a better fit is possible, change the model or its inputs.
If training loss barely falls or rises
If training loss barely falls or rises
Then check the learning rate. Try a larger step if progress is very slow. Try a smaller step if updates overshoot.
If the validation gap persists
If the validation gap persists
Then check for overfitting. Try an earlier checkpoint, a simpler fit or more training data. Compare validation loss on the same examples.
If validation loss is lower
If validation loss is lower
Then check how you measured both losses. Use the same loss and prediction settings. A validation sample can also be easier by chance.
Would more training readings help?
Would more training readings help?
If validation loss keeps falling as the sample grows, collecting more readings may help. Each point here refits the same model family with a different amount of data.
A boundary that fits every training part
Model 1 fits every training label. Model 2 draws a clean straight line. Which boundary would you trust on new parts?
Show the same boundaries on new parts
Model 1 got 92 of 100 correct. Model 2 got 85. Model 1 catches the ringed pocket of defects. Its complex boundary captures a real feature of the simulation.
ROC curves
How do the decisions change when we move the cutoff?
From a score to an inspection decision
A camera system scores each manufactured part for possible surface defects. A flagged part goes to a person for a closer look.
These twelve example scores come with actual labels from inspection. Higher scores indicate stronger evidence of a defect.
Flag every score of 0.55 or higher
How many of the flagged parts are defective? How many defects did we miss?
Read the four counts
Actual condition
Flagged
Left alone
Defective
4 true positives
2 false negatives
Clear
2 false positives
4 true negatives
A false positive uses a review slot. A false negative lets a defective part pass.
Raise the cutoff to 0.80
Two defects are caught and no clear parts are flagged. Four defects pass.
Lower the cutoff to 0.40
Five defects are caught and three clear parts are flagged. One defect passes.
At cutoff 0.55 we catch 4 of the 6 defects, and flag 2 of the 6 clear parts.
TPR is the fraction of actual defects caught. FPR is the fraction of actual clear parts flagged.
One cutoff gives one point
Plot FPR = 2/6 on the horizontal axis and TPR = 4/6 on the vertical axis.
Add the other cutoffs
Lowering the cutoff sends more parts to review. Each threshold gives its own trade-off.
Sweep through all the scores
The receiver operating characteristic (ROC) curve traces the cutoff points. Each newly flagged defect moves it up. Each newly flagged clear part moves it right.
Area under the ROC curve
The area summarises the ranking across cutoffs. An ideal ordering has area 1. A chance ranking has expected area 0.5.
When two ROC curves cross
A catches more defects when no false alerts are allowed. At FPR = 2/6, B can catch all six. The useful part of the curve depends on the inspection policy.
You have six review slots
Cutoff
Defects caught
Clear parts flagged
Total reviews
0.80
2
0
2
0.55
4
2
6
0.40
5
3
8
Which of these cutoffs catches the most defects within the six-slot limit?
Choose a cutoff for the constraint
0.55 catches four defects and fills six review slots. At 0.40 the system would ask for eight reviews.
Use validation data to choose the cutoff. Evaluate the chosen rule on new parts.
What if defects are rare?
Suppose a cutoff catches 80% of defects and flags 2% of clear parts. In this example 100 out of 10,000 parts are defective.
Actual condition
Parts
Flagged
Defective
100
80
Clear
9,900
198
Most flagged parts are clear. The false positive rate acts on a much larger group.