In Probability and Statistics you met the least-squares line, the line with the smallest sum of squared residuals, and used formulas for its slope and intercept. Here you will see where those formulas come from, and a second way to find the same line: the way that scales up to models with millions of parameters.
Key idea
Why it works
Worked examples
Worked example: The exact answer
- Sums: $\sum x_{i} = 10$, $\sum y_{i} = 18$, $\sum x_{i}^{2} = 30$, $\sum x_{i}y_{i} = 47$, and $n = 5$.
- Normal equations: $30m + 10b = 47$ and $10m + 5b = 18$.
- Double the second, $20m + 10b = 36$, and subtract: $10m = 11$, so $m = 1.1$. Then $5b = 18 - 11$, so $b = 1.4$.
- The residuals are $-0.4$, $0.5$, $0.4$, $-0.7$, $0.2$, so $L = \frac{0.16 + 0.25 + 0.16 + 0.49 + 0.04}{5} = 0.22$.
Answer: $\hat{y} = 1.1x + 1.4$, with $L = 0.22$
Worked example: One step of gradient descent
- At $(0, 0)$ every prediction is $0$, so the residuals are the $y_{i}$ themselves.
- $\frac{\partial L}{\partial m} = -\frac{2}{5}\sum x_{i}y_{i} = -\frac{2}{5} \cdot 47 = -18.8$ and $\frac{\partial L}{\partial b} = -\frac{2}{5}\sum y_{i} = -\frac{2}{5} \cdot 18 = -7.2$.
- $(0, 0) - 0.05\langle -18.8, -7.2 \rangle = (0.94, 0.36)$.
- The loss falls from $L(0, 0) = 15.6$ to $L(0.94, 0.36) \approx 2.12$. More steps creep toward $(1.1, 1.4)$.
Answer: $(m, b) = (0.94, 0.36)$
Worked example: A line through the origin
- With one parameter the loss is a parabola: $$L(m) = \frac{1}{3}\left[(3 - m)^{2} + (2 - 2m)^{2} + (7 - 3m)^{2}\right] = \frac{1}{3}\left(14m^{2} - 56m + 62\right).$$
- $L'(m) = \frac{1}{3}(28m - 56) = 0$ gives $m = 2$, the vertex of the parabola.
- In general $m = \frac{\sum x_{i}y_{i}}{\sum x_{i}^{2}}$; here $\frac{28}{14} = 2$.
Answer: $\hat{y} = 2x$
In the picture the loss map is a long, tilted valley, because $\sum x_{i}^{2} = 30$ is much bigger than $n = 5$. As in gradient descent on a long valley, a step size above about $0.15$ makes the path blow up, and a small one crawls along the valley floor. For a straight line the exact answer is easy, so why bother with steps? Because most models have no formula. A neural network's loss is also a mean of squared errors, but of a bent-line function of its weights, and there gradient descent, with derivatives from backpropagation, is the tool that works.
Common mistake
Common mistake