Alex Loftus
← Back to the lesson

From data to predictions and a loss function

The residual joins prediction and target. Its squared length measures total squared error.
The residual joins prediction and target. Its squared length measures total squared error.

One dot product makes one prediction

Put observations in the rows of a data matrix XX. A weight vector ww tells us how strongly each feature contributes. The matrix product computes all the predictions:

y^=Xw.\hat y=Xw.

If XX has mm rows and nn columns, ww has nn entries and y^\hat y has mm entries—one prediction per observation.

Compare the prediction arrow with the target arrow

The targets form another vector, yy. The difference y−y^y-\hat y is the residual vector. Its coordinates record the prediction errors, including their signs.

Suppose y=(1,3)y=(1,3) and y^=(2,1)\hat y=(2,1). The residual is (−1,2)(-1,2). Squaring and adding gives (−1)2+22=5(-1)^2+2^2=5. Average across the two observations:

L(w)=1m∥Xw−y∥2,L=52=2.5.L(w)=\frac{1}{m}\|Xw-y\|^2, \qquad L=\frac52=2.5.

This is mean squared error. Geometrically, it is the squared distance between the prediction and target vectors, divided by the number of observations. Perfect predictions make the arrows coincide and the residual zero.

Learning changes the weights

Changing ww moves XwXw within the column space of XX. Training searches for weights that reduce the loss. Gradient descent takes a small step opposite the loss gradient:

wnext=w−η∇L(w),η>0.w_{\text{next}}=w-\eta\nabla L(w),\qquad \eta>0.

Backpropagation computes gradients through a network; an optimizer uses them to update parameters. Lower training loss measures a better fit to the training examples. Performance on new examples needs its own evaluation.

Read a prediction correctly

Is a raw entry of XwXw necessarily a probability?

Answer: No. It is a real-valued score. A probability model needs an appropriate output mapping and training objective.