How a machine learns: gradient descent from scratch
Every neural network, every PINN, learns by repeating four moves — guess, measure the error, find the slope, step downhill. Here they are with ten apples, a pencil, and no assumptions.
You do not know the answer, so you guess. You check how wrong the guess is. You work out which direction makes it less wrong. You move a little in that direction. You repeat until the guess stops improving. That is gradient descent, and "learning" in machine learning means nothing more than this — done for millions of unknown numbers at once.
- what a parameter (or weight) is, using the price of an apple;
- what a loss is and why we square the error;
- how the slope (derivative) of the loss says which way to move;
- why the update has a minus sign;
- four steps of gradient descent worked completely by hand;
- what the learning rate does, and the exact condition for convergence in this example;
- two unknowns, then a straight-line fit, then a neuron — the same recipe every time.
1. The puzzle: what does one apple cost?
You bought 10 apples and paid ₹300. What is the price $p$ of one apple?
Of course $p = 300/10 = 30$. But pretend you cannot divide. You can only guess a price, check it, and improve it. This is exactly the situation of a neural network: it cannot "divide" its way to the answer, because its equations are far too complicated. It can only guess, check and improve.
| Apple puzzle | Machine-learning word |
|---|---|
| the unknown price $p$ | parameter or weight (the number we learn) |
| the rule "bill = 10 × price" | model: $\hat y = 10p$ (the hat means "predicted") |
| the real bill ₹300 | data or target $y = 300$ |
| how wrong the bill is | error $\hat y - y$ and loss |
| improving the guess | training / learning |
2. Measuring how wrong: the loss
Guess $p = 20$. The model predicts a bill of $10 \times 20 = 200$. The real bill was 300. The error is $200 - 300 = -100$: the guess is too cheap by ₹100.
We turn the error into one number that is never negative and is zero only when the guess is perfect. The usual choice is to square it: $$L(p) = (\text{prediction} - \text{target})^2 = (10p - 300)^2 .$$ This is the loss. Why square, and not just take the error?
- Sign does not matter. Too cheap ($-100$) and too expensive ($+100$) are equally wrong: both give $10\,000$.
- Big mistakes count more. An error of 20 costs 400, an error of 40 costs 1600 — four times as much.
- It is smooth. It has a derivative everywhere, which we need in the next step. ($|{\rm error}|$ has a corner at 0.)
| guess $p$ | prediction $10p$ | error $10p - 300$ | loss $(10p-300)^2$ |
|---|---|---|---|
| 20 | 200 | −100 | 10 000 |
| 25 | 250 | −50 | 2 500 |
| 30 | 300 | 0 | 0 |
| 35 | 350 | +50 | 2 500 |
| 40 | 400 | +100 | 10 000 |
Plotted against $p$ the loss is a U-shaped curve (a parabola) with its lowest point at the true price. Learning = finding the bottom of this valley.
3. Which way is downhill? The slope
Standing at a guess $p$ on the loss curve, the derivative $dL/dp$ is the slope of the curve there (Chapter 2). Let us compute it without skipping anything. Write $e = 10p - 300$, so $L = e^2$.
- How does $L$ change with $e$? $\;\dfrac{dL}{de} = 2e$.
- How does $e$ change with $p$? $\;e = 10p - 300$, so $\dfrac{de}{dp} = 10$.
- Chain rule: multiply the two. $\;\dfrac{dL}{dp} = \dfrac{dL}{de}\cdot\dfrac{de}{dp} = 2e \cdot 10 = 20\,(10p - 300).$
- At $p = 20$: $\;\dfrac{dL}{dp} = 20 \times (200 - 300) = 20 \times (-100) = -2000.$
The slope is negative: the curve goes down as $p$ increases. So to reduce the loss we should increase $p$ — which is right, since 20 is too cheap. At $p = 40$ the slope is $20 \times 100 = +2000$, positive: the curve goes up as $p$ increases, so we should decrease $p$.
The sign of the slope points uphill. The size of the slope says how steep it is (far from the bottom it is steep, near the bottom it is almost flat).
4. Why the minus sign?
The gradient-descent update is $$\boxed{\;p_{\text{new}} = p_{\text{old}} - \eta\,\frac{dL}{dp}(p_{\text{old}})\;}$$ where $\eta$ (Greek "eta") is a small positive number called the learning rate. The minus sign is there because the slope points uphill and we want to go downhill:
| situation | slope $dL/dp$ | $-\eta \cdot$ slope | effect on $p$ |
|---|---|---|---|
| guess too cheap ($p = 20$) | negative ($-2000$) | positive ($+4$ with $\eta = 0.002$) | $p$ goes up ✓ |
| guess too dear ($p = 40$) | positive ($+2000$) | negative ($-4$) | $p$ goes down ✓ |
| guess exact ($p = 30$) | $0$ | $0$ | $p$ stays ✓ |
With a plus sign the guess would move uphill every time: the loss would grow and the price would run away. That is "gradient ascent" — useful for finding a maximum, useless for learning.

Taylor's theorem (Chapter 2) says that for a small move $\Delta p$, $L(p + \Delta p) \approx L(p) + L'(p)\,\Delta p$. Choose $\Delta p = -\eta L'(p)$. Then $$L(p - \eta L'(p)) \approx L(p) - \eta\,\big(L'(p)\big)^2 \;\le\; L(p),$$ because a square is never negative. So, for a small enough $\eta$, every step lowers the loss unless the slope is already zero — i.e. unless we are at the bottom.
5. Four steps by hand
Start at $p_0 = 20$ with learning rate $\eta = 0.002$. The slope is $L'(p) = 20(10p - 300) = 200\,(p - 30)$.
| step | guess $p$ | bill $10p$ | error | loss | slope $200(p-30)$ | new $p = p - 0.002\times$slope |
|---|---|---|---|---|---|---|
| 0 | 20 | 200 | −100 | 10 000 | −2000 | 20 + 4 = 24 |
| 1 | 24 | 240 | −60 | 3 600 | −1200 | 24 + 2.4 = 26.4 |
| 2 | 26.4 | 264 | −36 | 1 296 | −720 | 26.4 + 1.44 = 27.84 |
| 3 | 27.84 | 278.4 | −21.6 | 466.56 | −432 | 27.84 + 0.864 = 28.704 |
| 4 | 28.704 | 287.04 | −12.96 | 167.96 | −259.2 | 28.704 + 0.5184 = 29.2224 |
Look at the distance to the answer: $10 \to 6 \to 3.6 \to 2.16 \to 1.296 \to 0.7776$. Each step multiplies it by $0.6$. After 20 steps the distance is $10 \times 0.6^{20} \approx 0.0004$: the machine has "learned" that an apple costs ₹30.

6. The learning rate: how big a step?
In the apple problem everything can be computed exactly. With $n$ apples and true price $p^*$, the loss is $L = (np - np^*)^2 = n^2(p - p^*)^2$ and the slope is $2n^2(p - p^*)$. One step gives $$p_{\text{new}} - p^* = (p - p^*) - \eta\cdot 2n^2(p - p^*) = \big(1 - 2\eta n^2\big)(p - p^*).$$ So the distance to the answer is multiplied by the factor $1 - 2\eta n^2$ every step. With $n = 10$, $\eta = 0.002$: factor $1 - 0.4 = 0.6$, as in the table.
- $0 < \eta < 1/n^2$ (here $\eta < 0.01$): $|{\rm factor}| < 1$, the guess converges.
- $\eta = 1/(2n^2) = 0.005$: factor $0$, the answer in one step.
- $1/(2n^2) < \eta < 1/n^2$: factor negative, the guess overshoots and zig-zags but still converges.
- $\eta > 1/n^2$: $|{\rm factor}| > 1$, the guess diverges.

The safe learning rate depends on the problem ($n^2$ here — the curvature of the loss). In a network, different weights see different curvatures, so no single $\eta$ is ideal for all of them. That is exactly why adaptive methods such as Adam (Adaptive Moment Estimation) scale the step for each weight separately.
7. Two unknowns: apples and bananas
Two shopping baskets:
- Basket 1: 3 apples + 2 bananas = ₹130.
- Basket 2: 1 apple + 4 bananas = ₹110.
Unknowns: apple price $a$ and banana price $b$ (two weights). Loss: the average of the squared errors of both baskets, $$L(a,b) = \tfrac12\Big[(3a + 2b - 130)^2 + (a + 4b - 110)^2\Big].$$ Now there are two slopes, one per unknown — the partial derivatives (Chapter 2): $$\frac{\partial L}{\partial a} = 3(3a+2b-130) + (a+4b-110),\qquad \frac{\partial L}{\partial b} = 2(3a+2b-130) + 4(a+4b-110).$$ Together they form the gradient $\nabla L = (\partial L/\partial a,\ \partial L/\partial b)$, an arrow pointing uphill on the loss surface. Gradient descent updates both prices at once: $$a \leftarrow a - \eta\,\frac{\partial L}{\partial a},\qquad b \leftarrow b - \eta\,\frac{\partial L}{\partial b}.$$
8. From prices to weights: fitting a straight line
Now real data: $N$ points $(x_i, y_i)$, say hours studied and marks obtained. Model: a straight line $\hat y = w x + b$ with two unknowns, the slope $w$ and intercept $b$ — the two "prices". Loss: mean squared error $$L(w,b) = \frac1N\sum_{i=1}^{N}\big(w x_i + b - y_i\big)^2 .$$ Slopes (chain rule, exactly as in section 3, applied to each term): $$\frac{\partial L}{\partial w} = \frac2N\sum_i (w x_i + b - y_i)\,x_i,\qquad \frac{\partial L}{\partial b} = \frac2N\sum_i (w x_i + b - y_i).$$ Updates: $w \leftarrow w - \eta\,\partial L/\partial w$, $b \leftarrow b - \eta\,\partial L/\partial b$. Same four moves.
Data: $(1, 3)$ and $(2, 5)$ (the true line is $y = 2x + 1$). Start at $w = 0$, $b = 0$, $\eta = 0.1$. Predictions $0, 0$; errors $-3, -5$. $\partial L/\partial w = \tfrac22[(-3)(1) + (-5)(2)] = -13$, $\;\partial L/\partial b = \tfrac22[-3 - 5] = -8$. Update: $w = 0 - 0.1(-13) = 1.3$, $b = 0 - 0.1(-8) = 0.8$. New predictions $2.1$ and $3.4$ — already much closer to $3$ and $5$. Keep going and $(w,b) \to (2, 1)$.
9. From a line to a neuron to a network
A neuron is a line followed by a bend: $\hat y = \sigma(wx + b)$ (Chapter 12). The loss and the update are the same; only the slope gets one more factor from the chain rule: $$\frac{\partial L}{\partial w} = \frac2N\sum_i\big(\hat y_i - y_i\big)\,\sigma'(wx_i+b)\,x_i .$$ A network is thousands of neurons, so thousands or millions of weights, each with its own slope. Writing those slopes by hand is impossible. They are computed automatically and efficiently by backpropagation — the subject of Chapter 14.
- Predict with the current weights (the forward pass).
- Measure the loss.
- Slope: compute the gradient of the loss with respect to every weight (backpropagation).
- Step: every weight $\leftarrow$ weight $- \eta \times$ its slope.
- Repeat until the loss stops decreasing.
10. Why not just solve the equation?
For the apples, $10p = 300$ gives $p = 30$ instantly. For the line, setting both partial derivatives to zero gives two linear "normal equations" that can be solved directly. So why bother with steps?
- For a neural network, "slope = 0" is a huge nonlinear system — millions of equations with millions of unknowns and no formula for the solution.
- Gradient descent needs only the ability to evaluate the slope, which backpropagation provides cheaply.
- It is memory-light: no matrix of size (number of weights)² is ever formed. Newton's method, which uses second derivatives, would need exactly that.
Gradient descent on a least-squares loss $\tfrac12\|Ax - b\|^2$ is the Richardson (Landweber) iteration $x \leftarrow x - \eta A^{\mathsf T}(Ax - b)$. It converges for $0 < \eta < 2/\lambda_{\max}(A^{\mathsf T}A)$, at a rate governed by the condition number — the same story as the apple factor $1 - 2\eta n^2$. See Chapter 11, section 3.
11. One example at a time: batch, mini-batch, stochastic
With a million data points, computing the exact average slope at every step is expensive. Instead:
| variant | slope computed from | pros / cons |
|---|---|---|
| (Full) batch gradient descent | all $N$ examples | exact slope, expensive per step |
| Stochastic gradient descent (SGD) | one random example | very cheap, very noisy |
| Mini-batch SGD | a random handful (e.g. 32 or 256) | the usual compromise; fits GPUs well |
One epoch = one pass through all the data. In PINNs the "data" are often collocation points, and a fresh random batch of collocation points can be drawn at every step.
n, bill = 10, 300 # 10 apples cost 300
p, eta = 20.0, 0.002 # first guess and learning rate
for step in range(12):
error = n * p - bill # prediction minus target
loss = error ** 2
slope = 2 * error * n # chain rule: dL/dp
print(f"step {step:2d}: p = {p:8.4f} loss = {loss:10.3f} slope = {slope:9.2f}")
p = p - eta * slope # step AGAINST the slope
print("learned price:", round(p, 4))
- A weight is an unknown number in the model; learning means adjusting weights to reduce the loss.
- The derivative of the loss points uphill; the update $w \leftarrow w - \eta\,\partial L/\partial w$ steps downhill — hence the minus sign.
- For small $\eta$, each step reduces the loss (Taylor's theorem).
- Too large an $\eta$ zig-zags or diverges; the safe size depends on the curvature of the loss.
- Many weights: one partial derivative per weight, all updated together; backpropagation computes them.
Further reading
- M. P. Deisenroth, A. A. Faisal & C. S. Ong, Mathematics for Machine Learning, Cambridge University Press, 2020 — chapter 7 (free PDF from the authors).
- I. Goodfellow, Y. Bengio & A. Courville, Deep Learning, MIT Press, 2016 — section 4.3.
- S. Boyd & L. Vandenberghe, Convex Optimization, Cambridge University Press, 2004 — section 9.3.
- A. Cauchy, "Méthode générale pour la résolution des systèmes d'équations simultanées", C. R. Acad. Sci. Paris 25, 1847, 536–538 — the first description of gradient descent.