
Block 4.2 Advanced AI
Definition 1 A supervised-learning model maps an input to an output prediction and is fitted using observed input/output pairs.
| Problem | Target | Output |
|---|---|---|
| regression | one continuous quantity | one number |
| multivariate regression | several continuous quantities | several numbers |
| binary classification | one of two categories | probabilities over two categories |
| multiclass classification | one of \(N\) categories | probabilities over \(N\) categories |
A ten-second audio clip sampled at \(44.1\,\text{kHz}\) contains 441,000 samples.
\[ \vect{y} = \vmodel{\vect{x}} \qquad \hat{\params} \in \argmin_{\params}\loss[\params] \tag{1}\]
Notation
The book writes the parameter setting in square brackets and the ordinary function argument in round brackets. We keep that distinction.
Speed and stopping distance for 50 cars.
How can we model the relationship between speed and stopping distance? Could we use a line?
\[ y = \model{x} = \phi_0 + \phi_1 x \tag{2}\]
The fitted setting is \(\phi_0 =\) -17.6 feet and \(\phi_1 =\) 3.93 feet per mile per hour. A stationary car takes a negative distance to stop.
Three parameter settings on the same data.
One car was doing 14 mph and took 80 ft. Under candidate B, \(\phi_0 = 0\) and \(\phi_1 = 3\). What is the residual, and what is its sign?
The prediction is 42 ft; the residual is +38 ft. The line sits below the observation.
The scalar score is part of the model design.
Definition 2 The least-squares loss is the sum of the squares of the residuals over the training pairs.
\[ \loss[\params] = \sum_{i=1}^{I} \left(\model{x_i} - y_i\right)^{2} = \sum_{i=1}^{I} \left(\phi_0 + \phi_1 x_i - y_i\right)^{2} \tag{3}\]
\[ \text{MSE}[\params] = \frac{1}{I}\loss[\params] \tag{4}\]
Note that we can calculate the residuals either way; the squares will be identical:
| \(x_i\) | \(y_i\) | \(\phi_0 + \phi_1 x_i\) | residual | squared |
|---|---|---|---|---|
| 4 | 2 | 12 | -10 | 100 |
| 4 | 10 | 12 | -2 | 4 |
| 7 | 4 | 21 | -17 | 289 |
| 7 | 22 | 21 | +1 | 1 |
Sum the other 46 the same way, divide by 50: A scores 390.5 and B scores 261.3 ft².
B scores lower (better) on this dataset.
| Object | Takes | Returns |
|---|---|---|
| the model \(\model{x}\) | an input | a prediction |
| the loss \(\loss[\params]\) | a parameter setting | one non-negative scalar |
Data dependence
Add one more car and \(\loss\) becomes a different function! The notation suppresses the data, but the dependency remains.
Candidate lines and their points in parameter space.
| Space | Dim. | One point | Where? |
|---|---|---|---|
| input | 1 | speed (mph) | left plot, x-axis |
| output | 1 | stopping distance (ft) | left plot, y-axis |
| input–output | 2 | observation \((x,y)\) | left plot |
| parameter | 2 | one line/model setting | right plot |
Mean squared error as a surface and as contours.
Mean squared error contours in parameter space.
Definition 3 A contour is the set of parameter settings sharing one loss value.
\[ \min_{\params}\left[\loss[\params]\right] \in \reals \qquad\qquad \argmin_{\params}\left[\loss[\params]\right] \subseteq \reals^{2} \tag{5}\]
Definition 4 Fitting is choosing a parameter setting \(\hat{\params}\) that minimises the loss over the training data, i.e. an element of the argmin.
\[ \hat{\params} \in \argmin_{\params}\left[\sum_{i=1}^{I}\left(\phi_0 + \phi_1 x_i - y_i\right)^{2}\right] \tag{6}\]
| Search | \(\argmin\) | min MSE (ft²) |
|---|---|---|
| 201-point grid | (-17.500, 3.920) | 227.0872 |
| exact | (-17.579, 3.932) | 227.0704 |
\[ 3 + (-1) = 2 \qquad 3(-1) = -3 \]
\[ \begin{bmatrix}1\\2\\3\end{bmatrix} + \begin{bmatrix}4\\-1\\2\end{bmatrix} = \begin{bmatrix}5\\1\\5\end{bmatrix} \qquad 2\begin{bmatrix}1\\2\\3\end{bmatrix} = \begin{bmatrix}2\\4\\6\end{bmatrix} \]
Definition 5 For vectors of the same length, the dot product multiplies corresponding entries and adds the results.
\[ \inner{\vect{a}}{\vect{b}} = \vect{a}\transpose\vect{b} = \sum_{d=1}^{\dimension} a_d b_d \tag{7}\]
For non-zero vectors:
\[ \inner{\vect{a}}{\vect{b}} = \norm{\vect{a}}\,\norm{\vect{b}}\cos\theta \tag{8}\]
\[ \norm{\vect{z}}_p = \left(\sum_{d=1}^{\dimension}|z_d|^p\right)^{1/p}, \qquad p \ge 1 \tag{9}\]

\[ \mat{A}=\begin{bmatrix}1&2\\3&4\end{bmatrix}, \qquad \mat{B}=\begin{bmatrix}5&6\\7&8\end{bmatrix} \]
\[ \mat{A}+\mat{B} =\begin{bmatrix}6&8\\10&12\end{bmatrix} \qquad 2\mat{A} =\begin{bmatrix}2&4\\6&8\end{bmatrix} \]
Row view: each output is a dot product.
\[ \mat{A}\vect{x} = \begin{bmatrix} a_{11} & \cdots & a_{1n}\\ \vdots & \ddots & \vdots\\ a_{m1} & \cdots & a_{mn} \end{bmatrix} \begin{bmatrix} x_1\\ \vdots\\ x_n \end{bmatrix} = \begin{bmatrix} a_{11}x_1+\cdots+a_{1n}x_n\\ \vdots\\ a_{m1}x_1+\cdots+a_{mn}x_n \end{bmatrix} \]
Each row of \(\mat{A}\) produces one entry of the output.
Geometrically, column \(j\) of \(\mat{A}\) is where the basis vector \(\vect{e}_j\) is sent.
\[ (m\times n)(n\times p)\rightarrow(m\times p) \qquad C_{ij}=\sum_{d=1}^{n}A_{id}B_{dj} \tag{10}\]
Defined? Resulting shape?
What about the reverse?
| Expression | Requirement | Result |
|---|---|---|
| \(a+b\) | scalars | scalar |
| \(ab\) | scalars | scalar |
| \(\vect{x}+\vect{y}\) | same vector shape | vector |
| \(a\vect{x}\) | scalar and vector | vector |
| \(\vect{x}\transpose\vect{y}\) | same vector length | scalar |
| \(\mat{A}+\mat{B}\) | same matrix shape | matrix |
| \(a\mat{A}\) | scalar and matrix | matrix |
| \(\mat{A}\vect{x}\) | inner dimensions agree | vector |
| \(\mat{A}\mat{B}\) | inner dimensions agree | matrix |
Addition matches shapes. Dot and matrix products match inner dimensions.
| Object | Written | Shape |
|---|---|---|
| scalar | \(x\) | one number |
| vector | \(\vect{x}\) | \((\dimension\times1)\) by default |
| matrix | \(\mat{A}\) | \((\dimension_1\times\dimension_2)\) |
| tensor | bold symbol | \(\dimension_1\times\dots\times\dimension_N\) |
\[ f(a\vect{u}+b\vect{v})=a f(\vect{u})+b f(\vect{v}) \] {#def-linear-map}
An affine map adds a translation:
\[ f(\vect{x})=\mat{W}\vect{x}+\vect{b} \tag{11}\]
nn.Linear is usually affine because it includes a bias“Linear regression” refers to linearity in the parameters. With an intercept, the fitted function is affine in the input.
\[ \vect{e}_1=\begin{bmatrix}1\\0\\0\end{bmatrix},\quad \vect{e}_2=\begin{bmatrix}0\\1\\0\end{bmatrix},\quad \vect{e}_3=\begin{bmatrix}0\\0\\1\end{bmatrix} \]
\[ \begin{bmatrix}3\\-1\\4\end{bmatrix} =3\vect{e}_1-\vect{e}_2+4\vect{e}_3 \tag{12}\]
The numbers are coordinates in a basis. Change the basis and the coordinates change; the vector does not.
\[ y = \phi_0 + \phi_1 x_1 + \phi_2 x_2 + \dots + \phi_{\dimension} x_{\dimension} \tag{13}\]
Indexing
Until now \(x_i\) meant example \(i\). Here \(x_1 \dots x_{\dimension}\) are the \(\dimension\) entries of one input. When both indices are needed, the example comes first: \(x_{id}\) is feature \(d\) of example \(i\).
\[ \tilde{\vect{x}} = \begin{bmatrix} 1 \\ x \end{bmatrix} \qquad \params = \begin{bmatrix} \phi_0 \\ \phi_1 \end{bmatrix} \qquad \model{x} = \inner{\tilde{\vect{x}}}{\params} \tag{14}\]
.weight and .biasInput vs parameters
The model is linear in the augmented input and remains affine in the original input. Appending a one is a bookkeeping convention, not a change of the map.
Definition 6 The design matrix stacks the augmented inputs, one example per row.
\[ \mat{X} = \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \vdots & \vdots \\ 1 & x_I \end{bmatrix} \qquad \mat{X}\params = \begin{bmatrix} \model{x_1} \\ \model{x_2} \\ \vdots \\ \model{x_I} \end{bmatrix} \tag{15}\]
Definition 7 The residual vector is the vector of targets minus the vector of predictions, one entry per example.
\[ \vect{r} = \vect{y} - \mat{X}\params \tag{16}\]
| Space | Dimension here | One point is |
|---|---|---|
| input | 1 | a speed |
| parameter | 2 | a whole line over every input |
| residual | 50 | one model’s mistakes, all of them |
Three spaces are now in play. None of them substitutes for another.
\[ \loss[\params] = \sum_{i=1}^{I}\left(y_i - \inner{\tilde{\vect{x}}_i}{\params}\right)^{2} \tag{17}\]
“Linear model” is ambiguous; this major avoids it unqualified.
\[ y = \phi_0 + \phi_1 x + \phi_2 x^{2} \tag{18}\]
| Still drawable | What it shows | What it hides |
|---|---|---|
| a 2-D slice, \(\phi_2\) held fixed | the surface at one setting of the third parameter | everything at every other setting |
| a projection | the range of loss values | which setting produced each |
| an isosurface | one loss level in 3-D | the values between levels |
For a model linear in its parameters under the squared loss, a least-squares minimiser satisfies the normal equations.
\[ \mat{X}\transpose\mat{X}\,\hat{\params} = \mat{X}\transpose\vect{y} \tag{19}\]
Normal equations: scope
The normal equations are specific to squared-error linear least squares. They do not carry over unchanged to absolute-error regression or to models nonlinear in their parameters.
The inverse formula \(\hat{\params}=(\mat{X}\transpose\mat{X})^{-1}\mat{X}\transpose\vect{y}\) is mathematically valid when full column rank holds, but numerical software should normally use a stable least-squares solver such as QR or SVD. Forming \(\mat{X}\transpose\mat{X}\) squares the condition number and can magnify numerical error.
Line and degree-11 polynomial fitted to the same data.
| Basis | Numerical rank | Condition number | Training MSE (ft²) | Prediction (ft) |
|---|---|---|---|---|
| raw powers of speed | 8 of 12 | 5.1 × 10¹⁹ | 188.8 | -103 to 95 |
| speed scaled to \([-1, 1]\) | 12 of 12 | 19,691 | 182.9 | 6 to 225 |
Rescaling and conditioning
The least-squares problem has not changed. Rescaling gives the same polynomial family a much better-conditioned numerical representation, so the computation is more reliable. It repairs the arithmetic in this example; it does not make the degree-eleven model a good predictor.
\[ N = k^{\,P} \tag{20}\]
Before the table: write down your estimate for twelve parameters, at the resolution from HOW WOULD YOU ACTUALLY FIND IT?
| parameters | evaluations at 201 points per axis |
|---|---|
| 2 | 40,401 |
| 3 | 8,120,601 |
| 12 | 4.3 × 10²⁷ |
Uniform-grid cost
This is a property of a uniform product grid at fixed per-axis resolution. It’s not a theorem about optimisation. Nor is it a claim that the problem is hard.
| parameters | evaluations | wall clock |
|---|---|---|
| 2 | 40,401 | 0.1 seconds |
| 3 | 8,120,601 | 19.0 seconds |
| 12 | 4.3 × 10²⁷ | 3.2 × 10¹⁴ years |
Local information
Using local directional information does not by itself guarantee convergence or a global minimum.
| Closed form | Local search | |
|---|---|---|
| requires | linear in parameters; squared loss | local directional information |
| additionally | full column rank for uniqueness; stable solver | method-specific conditions |
| cost | linear least-squares solve | iterative steps |
What would have to change about the model for that to stop being true?
Definition 8 The empirical risk is the average loss over the observed examples.
\[ \erisk[\params] = \frac{1}{I}\sum_{i=1}^{I} \exloss_i[\params] \tag{21}\]
Definition 9 The population risk is the expected loss over a specified distribution of input-output pairs — ideally the distribution whose performance we care about.
\[ \prisk[\params] = \expectsub{(x, y) \sim p}{\exloss[\params; x, y]} \tag{22}\]
That selection matters: training risk at the selected setting is generally an optimistic description of performance on new draws. Minimising empirical risk is therefore not the same thing as minimising population risk.
Definition 10 Underfitting means the fitted predictor performs poorly even on the training problem because the model family, optimisation or training procedure has not captured enough of the available structure.
Definition 11 Overfitting means performance on the training data is substantially better than performance on new data from the target distribution because the fitting procedure has adapted to sample-specific variation.
| model | parameters | training MSE (ft²) |
|---|---|---|
| line | 2 | 227.1 |
| line plus squared term | 3 | 216.5 |
| degree eleven | 12 | 182.9 |
Which of these would you ship, and what evidence would you need?
Closed notes, in pairs, then compared.
Review the item you could not reconstruct.
Not required: appendix B.5, matrix calculus, and problems 2.1 and 2.2.
Eoin O’Brien · eoin@eoin.ai