Feature scaling and curves
Last updated
Two practical tricks make linear models far more useful. Scaling the features turns a long thin valley back into a round bowl, so gradient descent gets to the bottom in a hundred steps instead of never. And adding powers of a feature as new features lets a linear model fit a curve. Both come up again in every later stage.
By the end of this lesson you can
- Explain why features on very different scales make gradient descent slow
- Standardise features and compare training before and after
- Fit a curve with a linear model by adding polynomial features
Why unscaled features make a long thin valley
Lesson 3.4 standardised the penguins' features before training, with a promise to explain. Here's the explanation.
The steepness of the loss bowl along each weight depends on the size of that weight's feature. Flipper lengths are around 200 mm, so nudging the flipper weight by 1 changes every prediction by about 200 g, and the loss shoots up. The bias's "feature" is always 1, so nudging by 1 changes every prediction by 1 g, and the loss hardly moves. The bowl is extremely steep in one direction and nearly flat in another: a long thin valley.
Gradient descent has to pick one learning rate for every direction. It must be small enough not to blow up in the steepest direction (lesson 3.3), and that's far too small to get anywhere in the flattest one. Squash one feature and watch:
Both features standardised: the bowl is nearly round, and gradient descent reaches the bottom in 10 steps.
At scale 1 the bowl is nearly round, and the walk goes almost straight to the bottom. Stretch flipper length by 10 and the learning rate has to shrink about sixtyfold; the walk zig-zags across the valley's steep sides and creeps along its floor.
Wrong way: 10,000 steps unscaled
Here's lesson 3.4's problem with the measurements in millimetres, as they come:
Bill lengths average 43.9 mm, depths 17.2, flipper lengths 200.9: very different from each other, and from the bias's 1. Learning rates of 0.0001 and 0.00003 blow up within 100 steps. 0.00002 is safe, and after 10,000 steps the error is still 461 g, worse than lesson 3.4's 391 g. The weights are nowhere near lesson 3.5's exact answer: has crawled from 0 to −5.81, when it should be −6,424.76, and the bill depth weight is −106 when it should be +20.
Nothing warned that the model hadn't finished. Its loss curve would be falling, slowly, the whole time; it might take millions of steps more.
Standardisation
Standardise each feature (lesson 0.5): subtract its mean and divide by its standard deviation, so every feature has mean 0 and spread 1.
The same problem, scaled, with learning rate 0.1: 100 steps reach an error of 391.1 g, the best possible. A hundred times fewer steps, and a right answer instead of a wrong one. Standardising doesn't change the predictions a linear model can make; it only changes how hard they are to find.
Two rules from Stage 2 still apply: work out the means and standard deviations from the training data only, and in scikit-learn put StandardScaler inside a pipeline, so it can't leak (lesson 2.10). Neural networks need the same care with their inputs, and lesson 7.7 adds normalisation inside the network as well.
Polynomial features
A car's stopping distance grows faster than its speed: double the speed and it more than doubles. Here are 40 cars (made up, but following the usual rule of thumb):
A straight line misses the curve: too high in the middle, too low at both ends. But "linear" in linear regression means linear in the weights, not in the inputs. Nothing stops you giving the model as a second feature:
That's still a linear model, a weighted sum of features, so everything in this stage still works: the bowl, gradient descent, the normal equations. It just has a curved feature. scikit-learn's PolynomialFeatures adds the powers for you:
The straight line's error is 3.9 m; with added, 2.8 m. The learned curve is , close to the rule the data was made from, .
Notice the second column: speeds near 60 have squares near 3,700. Powers of a feature have wildly different scales, so polynomial features need scaling too, if you train them by gradient descent. And higher powers are risky in another way: lesson 3.8 shows a degree-15 polynomial going wild.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- LeCun, Bottou, Orr and Müller, Efficient BackProp, in Neural Networks: Tricks of the Trade, Springer, 2012: why inputs should be centred and scaled before gradient descent
- scikit-learn: Preprocessing data: StandardScaler and PolynomialFeatures
- Goodfellow, Bengio and Courville, Deep Learning, chapter 4: Numerical Computation: how a badly conditioned bowl slows gradient descent (4.3.1)