121о. Training networks in depth: weight initialization, learning rate, momentum, hyperparameter search
121н covered how to keep a network from memorizing its training windows. This section covers what decides whether it learns anything at all and how fast: which weights to start from and why their scale decides whether the signal reaches the tenth layer, which learning rate to take and what happens when it is too small or too large, what momentum does and why it is almost the same thing as a larger step, why the step is reduced towards the end of training, and how to search for all of this at once — on a grid or at random points. At the end — what of it reaches the device.
Foundations: numpy 2.3.3 in the stm-ml:1 image, the script train121o.py
runs every experiment, and a repeated run gives the same files. The data
come from the accelerometer window generator of 121м: 64 samples on
three axes, integer mg, five classes (rest, knock, shake, double tap,
tilt), the event at a random place in the window, its own seed 1231.
There are three sets: 1000 windows for training (per class 207, 196,
201, 205, 191), 1000 for validation and 1000 for test. There are ten
times more training windows than in 121н: here the speed of training
matters, not overfitting. The network is fully connected
192 → 64 → 64 → 5 with ReLU, softmax with cross-entropy (121и), batch
10, 30 epochs. All experiments share the same initial random numbers
and the same order of examples: only the thing under study changes.
What it is called in the documentation
Original Meaning weight initialization which numbers training starts from Xavier (Glorot) initialization initial weights with spread 1/√inputs He (Kaiming) initialization initial weights with spread √(2/inputs), for ReLU exploding / vanishing activations the signal growing or fading from layer to layer learning rate η the step: the factor on the gradient in an SGD step momentum μ the share of the previous velocity kept in the next step learning rate schedule, step decay how the step changes over training; reduction in steps hyperparameter a setting the gradient does not tune grid search, random search trying values on a grid and at random points effective learning rate η/(1 − μ) the step actually taken with momentum
121о.1 Initial weights: why the scale decides
Weights cannot start from zeros: then all neurons of a layer compute the same thing, get the same gradient and stay identical. They are taken at random, and the only question is the spread σ. Let us see what it does to the signal. A network of 10 hidden layers with 64 neurons each, not yet trained, on the same 1000 windows; the input spread is 0.075 g. The table gives the standard deviation of each layer's outputs:
| Initial weights | Layer 1 | Layer 2 | Layer 4 | Layer 6 | Layer 8 | Layer 10 |
|---|---|---|---|---|---|---|
| σ = 1 | 0.618 | 3.63 | 106 | 3510 | 134000 | 3580000 |
| σ = 1/√n, Xavier | 0.0446 | 0.0327 | 0.0149 | 0.00772 | 0.00462 | 0.00192 |
| σ = √(2/n), He | 0.0631 | 0.0654 | 0.0596 | 0.0618 | 0.074 | 0.0616 |
Where this comes from. A weighted sum z = Σ wᵢ·aᵢ over n inputs with independent weights has variance n·σ²·E[a²]. ReLU zeroes half the values of a symmetric z, and E[a²] of its output is half the variance of z. So from layer to layer the variance is multiplied by n·σ²/2. With σ = 1 and n = 64 that is 32, and the spread grows by √32 ≈ 5.66 per layer — in the experiment by 5.64 on average. With σ = 1/√n the factor is 1/2, and the spread falls by 1/√2 ≈ 0.707 — in the experiment 0.705. Only with σ = √(2/n) is the factor exactly 1, and at the tenth layer the spread is the same as at the first: 0.0616 against 0.0631.
Explosion shows at once: outputs in the millions, softmax gives one to a single class, and the cost is huge. Vanishing is more dangerous because it is invisible: at the tenth layer the signal is 23 times weaker than at the first, the gradient towards the first layers comes back just as weakened (121л), and they barely learn. With two layers this is still tolerable, with ten it is not.
What the scale does not change: with zero biases ReLU switches on by the sign of the sum, and the sign does not depend on the factor σ. That is why in the last layer 6 neurons out of 64 are silent on all 1000 windows with any of the three methods.
Now training the network 192 → 64 → 64 → 5 with η = 0.03 with each method:
| Initial weights | Cost before training | Cost after epoch 1 | Validation after epoch 1 | After epoch 5 | After epoch 30 | Test |
|---|---|---|---|---|---|---|
| σ = 1 | 37.584 | 2.666 | 562 | 687 | 750 | 740 |
| Xavier | 1.612 | 1.590 | 298 | 526 | 821 | 801 |
| He | 1.618 | 1.539 | 386 | 645 | 835 | 825 |
A cost of 1.609 (ln 5) is a network that gives all five classes equal probability. With Xavier and He weights training starts right there, and with σ = 1 it starts at 37.584: the network is confidently wrong. The first steps are huge, and after the first epoch it is even ahead (562 against 386), but by epoch 30 it falls behind: 750 against 835, on test 740 against 825. Large initial weights are the same large weights that L2 fights in 121н.3, and the gradient first has to clear them away. He beats Xavier here too, with two layers.
121о.2 The learning rate
An SGD step is w ← w − η·∂C/∂w. The gradient says where to go; η says how far. Too small a step means the right direction but crawling progress. Too large a step jumps over the valley of the cost and lands on its other slope, higher than before. A grid of η without momentum, He initial weights:
| η | Cost after epoch 1 | Cost after epoch 30 | Correct on training, of 1000 | On validation | Best validation in 30 epochs | First epoch with 800 on validation |
|---|---|---|---|---|---|---|
| 0.001 | 1.615 | 1.538 | 475 | 454 | 454 | — |
| 0.003 | 1.610 | 1.310 | 636 | 600 | 600 | — |
| 0.01 | 1.591 | 0.891 | 768 | 716 | 716 | — |
| 0.03 | 1.539 | 0.269 | 913 | 835 | 841 | 17 |
| 0.1 | 1.281 | 0.019 | 1000 | 868 | 868 | 7 |
| 0.3 | 0.994 | 0.002 | 1000 | 870 | 875 | 3 |
| 1 | 0.871 | 0.086 | 971 | 852 | 862 | 10 |
| 3 | 1.169 | 1.490 | 303 | 299 | 406 | — |
The step changes the result more than anything else in this section: with the same network and the same data — from 454 to 870 answers. From 0.001 to 0.3, larger is better: each step is three times longer, and in the same 30 epochs the network gets further. At η = 1 the cost already jumps: it falls not monotonically but with spikes, and on validation the curve jerks by up to 185 answers from epoch to epoch. At η = 3 the network does not learn at all: the cost stays near 1.5, accuracy at 299 of 1000, not much more than the fifth that answering a single class gives.
Why a large step makes the network "die" rather than merely oscillate. One step that is too long drives the sums of many ReLU neurons negative on all windows; their output is zero, their gradient is zero, and nothing will pull them back. After 30 epochs at η = 3 there are 36 such neurons of 64 in the first hidden layer and 44 of 64 in the second, at η = 1 — 9 and 1, at η = 0.3 — none. The cost is not infinite — the remaining neurons and the biases hold it — but there is nothing left to learn with.
A practical rule from the table: search η in multiples (0.001, 0.003, 0.01…), find where training starts to fall apart, and take 3–10 times less. Here it falls apart at 3, and the best are 0.1 and 0.3.
121о.3 Momentum
Plain SGD steps along the current gradient and forgets the previous one. In a long, gently sloping valley the gradient is small along the floor and large across the slopes: the step bounces across and crawls along. Momentum adds memory — a velocity v that accumulates steps:
v ← μ·v − η·∂C/∂w, w ← w + v.
In code that is two lines instead of one:
v[j] = mom * v[j] - eta * gp[j]
p[j] += v[j]
If the gradient stays the same for a long time, the velocity reaches v = −η·g/(1 − μ): with μ = 0.9 the step is ten times longer, with μ = 0.99 — 100 times. Across the valley the gradient changes sign, and the contributions cancel. Hence a test: if momentum is mostly a larger step, networks with the same η/(1 − μ) should learn the same way.
| μ | η | Cost after epoch 30 | On validation | Best validation | First epoch with 800 |
|---|---|---|---|---|---|
| 0 | 0.01 | 0.891 | 716 | 716 | — |
| 0.5 | 0.01 | 0.457 | 821 | 821 | 28 |
| 0.9 | 0.01 | 0.015 | 861 | 864 | 6 |
| 0.99 | 0.01 | 0.015 | 844 | 851 | 4 |
| 0 | 0.03 | 0.269 | 835 | 841 | 17 |
| 0.5 | 0.015 | 0.268 | 833 | 841 | 17 |
| 0.9 | 0.003 | 0.259 | 835 | 837 | 17 |
| 0.99 | 0.0003 | 0.287 | 841 | 841 | 20 |
With the same η = 0.01, momentum 0.9 raised validation from 716 to 861: the slow step became fast. But in the lower half, where the η·(1 − μ) pairs give the same effective step 0.03, all four networks are almost identical: 835, 833, 835 and 841, and all pass 800 at epochs 17–20. On this task momentum is mostly a way to get a large step, not something beyond it. Its benefit lies elsewhere: it smooths the noise of the mini-batches, and in deeper networks with a more curved cost the difference from a plain large step is more noticeable. At μ = 0.99 and η = 0.01 the effective step is already 1 — and validation jerks again: best 851, at the end 844.
Hence the main trap: changing μ without touching η means changing the effective step. Going from 0.9 to 0.99 is a step 10 times larger.
121о.4 The step schedule
At the end of training a large step gets in the way: the network is already in the valley, but the step throws it from slope to slope, and validation answers jump. The simplest remedy is a single drop: train with η = 0.1 until epoch 20, then reduce the step 10 times.
| Step | Cost after epoch 30 | Correct on training | On validation | On test | Validation jumps over the last 5 epochs |
|---|---|---|---|---|---|
| constant 0.1 | 0.019 | 1000 | 868 | 847 | 29 |
| 0.1, from epoch 21 0.01 | 0.058 | 995 | 862 | 842 | 5 |
"Jumps" are the sum of changes in the number of correct validation answers from epoch to epoch. After the drop they fell from 29 to 5: the network stopped bouncing. Accuracy did not grow — 862 against 868 on validation, 842 against 847 on test. The schedule makes the outcome predictable: the weights of the last epoch hardly depend on where exactly training stopped. Without it one has to rely on early stopping (121н.2) and pick the epoch by validation — with all its luck of selection.
121о.5 Hyperparameter search: a grid or random points
Hyperparameters are everything the gradient does not tune: η, μ, batch size, λ, the number of layers and neurons, the number of epochs. They are chosen by the validation set, and each attempt is a whole training run. An order that saves attempts:
- the step first. It changes the outcome more than anything else (121о.2); until it is found, comparing the rest is pointless;
- short runs. For a coarse search 10 epochs are enough instead of 30: bad points show at once;
- in multiples. η and 1 − μ are searched on a logarithmic scale: the difference between 0.001 and 0.01 is the same as between 0.01 and 0.1.
Two ways to search η and μ together, 16 training runs of 10 epochs each: a 4 × 4 grid (η = 0.003 / 0.01 / 0.03 / 0.1, μ = 0 / 0.5 / 0.9 / 0.95) and 16 random points (η from 0.001 to 0.32 on a logarithmic scale, 1 − μ from 0.01 to 1 likewise).
| Method | Distinct η | Points with 800 or more on validation | Best point | Validation | Test |
|---|---|---|---|---|---|
| 4 × 4 grid | 4 | 7 | η = 0.03, μ = 0.9 | 865 | 841 |
| random points | 14 | 6 | η = 0.12, μ = 0.38 | 865 | 840 |
The best found is the same, 865, but the points differ — and yet both have a close effective step η/(1 − μ): 0.3 for the grid and 0.19 for the random one. This is the same step of 0.1–0.3 that gave the best in 121о.2. Random search tried 14 distinct η instead of 4: when only one of two parameters mostly matters, a grid spends attempts repeating the same η with different μ, while random points try a new one every time. The price is failed points: all 6 random points with an effective step above 2 gave no more than 605.
The best random point was trained on to 30 epochs: 867 on validation (best during training — 874), 851 on test. The test was looked at once, after all choices, — that is the number for the model's passport. Validation is above test here too: the best of 32 points was chosen, and its 865 contains the luck of selection (121н.2).
121о.6 What reaches the device
Initial weights, step, momentum and schedule live only on the computer: the device receives trained weights and computes the forward pass. The traces on the device are these:
- reproducibility. Seeds, η, μ, the schedule and the number of epochs go into the model's passport next to the test accuracy: without them the same network cannot be retrained when new recordings arrive;
- training on the device itself. If the network is fine-tuned in place, momentum needs a second copy of all weights. The network 192 → 64 → 64 → 5 has 16837 parameters: in float that is 67348 bytes, with velocities 134696. Out of the 192 KB of STM32F407 RAM that is more than two thirds; without momentum about a third;
- activation scale. A network started from He weights keeps the spread of layer outputs of the same order, and the int8 activation scales (119) of different layers are similar. A network with a fading signal quantizes worse: in deep layers the values occupy only a few of the lowest int8 levels.
121о.7 Pitfalls
Initial weights σ = 1. Cost before training 37.584 instead of 1.609, result 750 against 835. In a ten-layer network the outputs are in the millions.
Xavier weights under ReLU. The signal falls by 1/√2 at every layer; at the tenth it is 23 times weaker than at the first. For ReLU — He.
A step on the edge of collapse. At η = 1 validation jerks from epoch to epoch, at η = 3 the network does not learn at all: 299 of 1000, 36 and 44 neurons of 64 are dead. Take 3–10 times less than the collapse point.
μ without η. Going from momentum 0.9 to 0.99 is a step 10 times larger. Change one — recompute the other: the effective step is η/(1 − μ).
A grid over two parameters. A 4 × 4 grid tries only 4 values of the main parameter; 16 random points try 14.
Final accuracy from validation. The best of 32 points is inflated; the model's passport gets the test, looked at once.
121о.8 Summary
- Signal spread in 10 layers of 64 neurons: with σ = 1 it grows on average by 5.64 per layer (theory √32 ≈ 5.66), with Xavier it falls by 0.705 (theory 0.707), with He at the tenth layer 0.0616 against 0.0631 at the first.
- Training 192 → 64 → 64 → 5 at η = 0.03: σ = 1 — cost before training 37.584, test 740; Xavier — 801; He — 825.
- Step: from 454 at η = 0.001 to 870 at η = 0.3; at η = 3 — 299.
- Momentum 0.9 at η = 0.01: validation from 716 to 861; at equal η/(1 − μ) = 0.03 — 835, 833, 835, 841.
- Step drop at epoch 21: validation jumps from 29 to 5, test 842 against 847.
- Search: a 4 × 4 grid and 16 random points — 865 each on validation, 4 and 14 distinct η; the best random point after 30 epochs — 851 on test.
- Gradient check of the network with two hidden layers: 84 derivatives, difference 4.3·10⁻⁸.
Further reading: Michael Nielsen, Neural Networks and Deep Learning, neuralnetworksanddeeplearning.com.
Next: section 122 "Integer types and conversions: promotion, sign, overflow".