Contents
03Supervised learning: learning from an answer key
3.1Risk and empirical risk
In supervised learning the training data are pairs (x_i,y_i) drawn independently from an unknown distribution p(x,y). What we really want is a model with small risk, its average loss on new data from the same distribution,
We cannot compute R, because p is unknown, so we replace the expectation by the average over the training set, the empirical risk,
For a fixed \theta, the law of large numbers guarantees that \hat R(\theta)\to R(\theta) as n grows. The catch is that \theta^\star is chosen using the same data, so \hat R(\theta^\star) is an optimistic estimate of R(\theta^\star); this gap between training and test error is the subject of the bias–variance tradeoff, Episode 2 of this series. Here we focus on what the loss should be and how to minimize it.
3.2Classification and regression
If y is a category (fraud or not, cat or dog), the task is classification; if y is a number (a house price, a temperature), it is regression. The two differ mainly in their loss: regression usually uses the squared error L=(y-\hat y)^2; classification uses the cross-entropy, which we now derive.
3.3Why cross-entropy? Maximum likelihood
Take binary classification with y\in\{0,1\}. Logistic regression models the probability of class 1 as
The sigmoid squashes any real score z into (0,1), and the set where p=0.5, that is w^\top x+b=0, is the decision boundary, a line in two dimensions. A natural way to choose w and b is maximum likelihood: pick the parameters under which the observed labels are most probable. Each label is a coin flip with probability p_i, so its probability is p_i^{\,y_i}(1-p_i)^{1-y_i} (this equals p_i if y_i=1 and 1-p_i if y_i=0). Assuming independent examples, the likelihood is the product
Taking the logarithm turns the product into a sum without moving the maximum; flipping the sign gives a quantity to minimize:
This is the binary cross-entropy, or log loss. Its behaviour is easy to remember: if the model gives the true class probability 0.9, the loss is -\log0.9=0.105; a coin-flip guess of 0.5 costs 0.693; a confident mistake, probability 0.1 for the true class, costs 2.303, and probability 0.01 costs 4.605. Cross-entropy punishes confident mistakes very hard. The same argument with K classes and a softmax output gives the multiclass cross-entropy -\log p_{\theta}(y_i\mid x_i). For regression, if we model y=f_\theta(x)+\varepsilon with Gaussian noise \varepsilon, maximizing the likelihood gives exactly the squared-error loss: both standard losses are negative log-likelihoods.
3.4The gradient, and gradient descent
To minimize (3) we need its gradient. Two facts make it short. First, the derivative of the sigmoid is \sigma'(z)=\sigma(z)\big(1-\sigma(z)\big). Second, for a single example the loss \ell=-y\log p-(1-y)\log(1-p) with p=\sigma(z) has, by the chain rule,
The derivative with respect to the score is simply prediction minus label. Since z=w^\top x+b, we have \partial z/\partial w=x and \partial z/\partial b=1, so
Each example pushes the parameters in proportion to its error p_i-y_i, in the direction of its own input. Gradient descent, an idea that goes back to Cauchy (1847), repeatedly takes a small step against the gradient,
where \eta>0 is the learning rate. The Hessian of (3), \frac1n\sum_i p_i(1-p_i)\,\tilde x_i\tilde x_i^\top with \tilde x_i=(x_i,1), is positive semidefinite, so the loss is convex and has no spurious local minima.
Worked example 1. Three one-dimensional examples: x=(-1,\,0,\,2) with labels y=(0,\,0,\,1). Start from w=b=0, so every p_i=\sigma(0)=0.5 and the loss is \log 2=0.693. The errors are p-y=(0.5,\,0.5,\,-0.5), so by (4)
One step with \eta=1 gives w=0.5 and b=-0.167. The new probabilities are (0.339,\,0.458,\,0.697): the two class-0 examples moved down, the class-1 example moved up, and the loss fell from 0.693 to 0.463.
The video’s animation. Figure 2 reproduces the gradient-descent animation of the video: 110 points from two Gaussian classes, a deliberately poor starting boundary, and 140 steps of (5) with \eta=0.3. The cross-entropy falls from 0.541 (31 of 110 points misclassified) to 0.222 after 10 steps (6 errors), to 0.095 after 40 steps (no errors) and to 0.049 after 139 steps. The final boundary misclassifies one point although its loss is lower than at step 40: gradient descent minimizes the smooth cross-entropy, a surrogate for accuracy, not the error count.
3.5The supervised toolbox and a real example
The classic supervised methods differ in the family of functions f_\theta and in how they search it: linear and logistic regression, k-nearest neighbours, naive Bayes, support vector machines, decision trees, random forests, gradient boosting and neural networks. Later episodes cover each one.
A classic real example is credit-card fraud. A public data set of 284,807 card transactions made by European cardholders over two days in September 2013 contains 492 frauds, only 0.172\% (Dal Pozzolo et al., 2015). Each transaction is an input x (28 of its features are principal components, Section 5, the other two are time and amount) with a label y\in\{\text{legitimate},\text{fraud}\}. The imbalance is a lesson in itself: a classifier that always answers “legitimate” is 1-492/284{,}807=99.83\% accurate and completely useless, which is why such problems are judged by precision and recall rather than accuracy.
Pause and think. In Figure 2 the loss keeps decreasing after step 40 although the training error is already zero. What happens to \|w\| if we keep going, and why?
Show the answer
It keeps growing. When the classes are separable, the cross-entropy can always be lowered further by scaling w and b up, which pushes every p_i closer to 0 or 1; the minimum is never attained. In practice we stop early or add a penalty \lambda\|w\|^2, which keeps the weights finite (regularization, Episode 4).