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.
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.
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.
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
- ANN: Activation Functions — ReLU, sigmoid, tanh and GELU, and why some slopes vanish
- ANN: Mathematical Model — the neuron as \(y = f(\sum_i w_i x_i + b)\)
- ANN: Overview
- Lab#DL01-1: Derivatives and the computation graph
- Lab#DL01-2: Backpropagation by hand
- Lab#DL01-4: From neuron to MLP and training
- Calculus essentials — the chain rule and one step of gradient descent
- Optimization essentials — gradient descent, the learning rate, and Adam
- ML math symbols, explained easy — the symbol \(\nabla\)
References
- [1] Aqsa Zafar / MLTut, Gradient Descent Neural Network — Quick and Super Easy Explanation!, 8 January 2024. The infographic in the margin, the update rule, the mean-squared-error gradients, and the linear-regression example follow this source. https://www.mltut.com/gradient-descent-neural-network-quick-and-super-easy-explanation/. Credit: MLTut.
- [2] Andrej Karpathy, The spelled-out intro to neural networks and backpropagation: building micrograd, Neural Networks: Zero to Hero, 2022. https://www.youtube.com/watch?v=VMj-3S1tku0.






