ANN: Gradient

Why the gradient matters

The gradient of the loss points in the direction of steepest increase. Training an ANN means taking a small step the opposite way, again and again, so the network gets a little less wrong.

That is the whole job of learning: not a mysterious jump to the right answer, but a careful walk downhill. Every weight in the network is a coordinate on that hillside. The gradient tells each weight which way is up, so we can move it down.

What the gradient is

Imagine you are walking down a hill in thick fog. You cannot see the valley. You can only feel the slope under your feet. The ground is steepest in one direction — that is the gradient. If you want to go down, you turn around and take a step the other way.

A neural network is in the same fog. The loss is the height of the hill: a single number that says how wrong the current predictions are. The weights and the bias are your position on the hill. The gradient \(\nabla J\) of the loss is the local slope. We do not follow it. We subtract it:

\[ w_{\text{new}} = w_{\text{old}} - \eta\,\nabla_w J(w_{\text{old}}, b_{\text{old}}) \]

\[ b_{\text{new}} = b_{\text{old}} - \eta\,\frac{\partial J}{\partial b}(w_{\text{old}}, b_{\text{old}}) \]

The number \(\eta\) (eta) is the learning rate: the size of each step.

MLTut infographic titled Gradient Descent. A colour bowl of the loss, with Start at the rim and Minimum at the bottom. The update rule is w new equals w old minus eta times the gradient, and the same for the bias. A lower panel shows Python code for linear regression and a fitted line.

Infographic of gradient descent: a loss bowl, the update rule, and a linear-regression example.

A compact map of the idea, the update rule, and the linear-regression example we run below.

Credit: MLTut

A U-shaped loss curve. A starting point sits on the left slope. A brown arrow labelled gradient (steepest increase) points up the hill; a green arrow labelled our step (opposite way) points down toward the minimum at the bottom.

A bowl of loss. At the starting point the gradient points up the hill, and our step points the opposite way, down toward the minimum.

A U-shaped loss curve. A starting point sits on the left slope. An arrow labelled gradient (steepest increase) points up the hill; an arrow labelled our step (opposite way) points down toward the minimum at the bottom.

A bowl of loss. At the starting point the gradient points up the hill, and our step points the opposite way, down toward the minimum.

Another everyday picture is the shower. The water is a little too hot. You do not slam the tap all the way to cold — you give it a small nudge, wait, and feel again. Too cold, you nudge back the other way. Each nudge is a step; the size of the nudge is the learning rate. After a few careful turns the temperature is right.

The same idea shows up when you season a soup, or fine-tune a bicycle saddle. You do not know the perfect setting in advance. You feel the error, you move a little the other way, you feel again.

The step size

The learning rate is a knob, not a result the network invents. If \(\eta\) is tiny, each step is a shuffle: you will get there, but it takes forever. If \(\eta\) is huge, you jump over the valley and may bounce up the other side, or fly off the hill altogether. A good rate is a steady walk: large enough to make progress, small enough to stay on the slope.

Three side-by-side bowls of loss. Left, too small: many tiny dots crawl down the slope. Middle, just right: a few steady steps reach the bottom. Right, too big: a zigzag path bounces from one wall to the other.

Three panels on the same loss bowl: a tiny learning rate crawls, a moderate one takes steady steps, and a large one overshoots and zigzags.

Three side-by-side bowls of loss. Left, too small: many tiny dots crawl down the slope. Middle, just right: a few steady steps reach the bottom. Right, too big: a zigzag path bounces from one wall to the other.

Three panels on the same loss bowl: a tiny learning rate crawls, a moderate one takes steady steps, and a large one overshoots and zigzags.

How it connects to backpropagation

A real network has thousands, or millions, of weights. The loss is a single number at the end. To move every weight, we need its own slope: “if I nudge this weight, does the loss go up or down?”

Backpropagation is how we get all those slopes in one backward pass. The loss sits at the last node. We start there, with a slope of \(1\), and we walk back through the computation graph, multiplying by the local derivative at each step. That is the chain rule, applied to the whole network. Lab#DL01-1 builds a Value that remembers its parents and stores a grad. Lab#DL01-2 fills those gradients in by hand. Lab#DL01-3 automates the backward pass, and Lab#DL01-4 uses it to train a small multilayer perceptron.

So the gradient is not a separate trick from backpropagation. Backprop is the method that computes the gradient of the loss with respect to every weight. Gradient descent is then the walk: subtract \(\eta\) times that gradient.

How it connects to activation functions

On the way back, the signal has to pass through every activation. At each neuron the incoming gradient is multiplied by the derivative of that activation. If the derivative is about \(1\), the message keeps its size. If the derivative is \(0.25\), or \(0.01\), or \(0\), the message shrinks — and after a few layers it can vanish. Then the early weights barely move, and the network stops learning in depth.

That is why activations need useful derivatives, not just a nice curve. Sigmoid is a beautiful S-shape, but its slope is at most \(0.25\), and almost \(0\) in the tails. Stack a few sigmoids and the gradient fades. ReLU is less pretty, but for a positive input its slope is \(1\), so the signal can travel. The details, and the four functions you will meet first, are on ANN: Activation Functions.

Two rows of four circles. The top row, labelled a useful slope (about 1) keeps the message, multiplies by 1 at each step with thick arrows. The bottom row, labelled a tiny slope (0.25) shrinks it at every layer, multiplies by 0.25 with thinner arrows.

Two rows of neurons. On the top, a slope of 1 keeps the backward signal; on the bottom, a slope of 0.25 shrinks it at every layer.

Two rows of four circles. The top row, labelled a useful slope (about 1) keeps the message, multiplies by 1 at each step with thick arrows. The bottom row, labelled a tiny slope (0.25) shrinks it at every layer, multiplies by 0.25 with thinner arrows.

Two rows of neurons. On the top, a slope of 1 keeps the backward signal; on the bottom, a slope of 0.25 shrinks it at every layer.

A line that learns

The same walk works for a single straight line, which is a good first picture before a full network. We fit \(\hat{y} = w x + b\) to noisy points from the true line \(y = 4x + 1\). The loss is the mean squared error

\[ J(w,b)=\frac{1}{2m}\sum_{i=1}^{m}\bigl(\hat{y}^{(i)}-y^{(i)}\bigr)^{2}, \]

and its gradients (the 2 from the square cancels with the \(2\) in the denominator) are

\[ \nabla_w J=\frac{1}{m}X^{\top}(Xw+b-y) \]

\[ \frac{\partial J}{\partial b}=\frac{1}{m}\sum_{i=1}^{m}\bigl(\hat{y}^{(i)}-y^{(i)}\bigr) \]

In words: the bias slope is the average error; the weight slope is the average error, each point weighted by its \(x\). Then we apply the same update as above, with \(\eta=0.1\), for \(100\) steps. The cell is live Python in the browser; change a number and run it again.

The true line was \(w=4\), \(b=1\). After those \(100\) steps the fit is \(w=3.7159\), \(b=1.4135\) — close, and on the right side of the fog. A longer walk, or a slightly different rate, would move it a little nearer.

Summary

Idea In one line
Gradient The direction of steepest increase of the loss
Update \(w \leftarrow w - \eta\,\nabla J\) — we move the opposite way
Learning rate \(\eta\) The step size: too big overshoots, too small crawls
Backpropagation The chain rule that gets a slope for every weight
Activations Their derivatives must not shrink that slope to nothing

FAQ

Why do we subtract the gradient, instead of adding it? Because the gradient points uphill. Adding it would climb the loss. Subtracting it walks down.

Is the gradient one number? For one weight, yes: a slope. For a whole network it is a list, one slope per weight and bias. We still call the whole list “the gradient.”

Who chooses the learning rate? You do, as a hyperparameter, before training. If the loss explodes or jumps around, lower it. If nothing moves, raise it a little.

Do we need backpropagation for a single line? No. The two formulae above are already the gradients. Backprop is the same idea, organised so it still works when the model is a deep chain of layers.

Why do some activations “kill” the gradient? If the derivative is almost zero at the values a neuron takes, that neuron multiplies the backward signal by almost zero. Early layers then stop learning. That is the vanishing-gradient problem, in one sentence.

See also

References

Back to top