S14 · Lesson 16 · Logistic regression
Modeling a class probability with the logistic sigmoid of a linear function, log-odds and the odds ratio per unit of input, the cross-entropy error and its strikingly simple gradient, Newton's method for the weights (iteratively reweighted least squares), and the decision boundary
Fisher's discriminant and the perceptron draw a line but don't say how confident to be on each side. Logistic regression does: it passes a linear function of the input through the logistic sigmoid, which turns any real number into a probability, and fits the weights by maximizing the likelihood of the labels. Its gradient has the same form as linear regression's, prediction minus target times input, and it's the building block of every neural network's output layer for classification.
- The predicted probability of the positive class against the input x.
- The decision boundary, where the probability crosses 1/2, is at . A larger makes the transition sharper.
- Each unit increase in x multiplies the odds by .
Predict
A logistic model has = −3 and = 1.5. What probability does it give the positive class at x = 3?
The activation is −3 + 1.5 × 3 = 1.5, and = 0.818. The boundary, where the activation is 0, is at x = 2; one unit past it, the odds have grown by = 4.48 from even.
Name it
The model
The activation a is the log-odds, ; increasing a feature by one multiplies the odds by . The sigmoid's derivative is .
Fitting
With targets and predictions , the negative log-likelihood is the cross-entropy
There's no closed-form minimum, but E is convex, so gradient descent or Newton's method finds the unique optimum. Newton's Hessian is , and each step is a weighted least-squares problem: iteratively reweighted least squares (IRLS).
Separable data
If a hyperplane separates the classes perfectly, the likelihood keeps rising as : the weights blow up and the probabilities become overconfident 0s and 1s. Regularization (a prior on w) keeps them finite.
Worked example
For the model above and a training example at x = 3 with t = 1, find the error gradient and the Hessian contribution.
Show the solution
- Prediction. At x = 3 the model gives y = 0.818, so .
- Gradient. Gradient descent raises both weights, pushing the probability at x = 3 toward 1.
- Hessian contribution. The slope entry from this example is
Answer the gradient is (−0.182, −0.547), and the example adds 1.34 to the Hessian's slope entry.
Worked example
A model predicts 0.9 for some input. What's the log-odds, and how far must x move (with = 1.5) to reach 0.5?
Show the solution
- Log-odds.
- Move x. The activation must fall by 2.197, so x must decrease by
Answer the log-odds are 2.197, and x must decrease by 1.46.
Worked example
A three-class softmax model gives activations (2, 1, 0) for an example whose true class is the second. Find the probabilities, the loss, and the gradient of the loss with respect to the activations.
Show the solution
- Probabilities. Normalize the exponentials: so the probabilities are 0.665, 0.245 and 0.090.
- Loss.
- Gradient. With respect to activation k it's :
- What it does. It has the same simple form as logistic regression's: each activation moves in proportion to how far its probability is from the target, so training pulls the second activation up and pushes the first down hardest.
Answer the probabilities are 0.665, 0.245 and 0.090, the loss is 1.41 nats, and the gradient is (0.665, −0.755, 0.090).
Check
What's σ(0.5)?
Check
A logistic model outputs 0.6. What's its activation (the log-odds)?
Check
A model with = −4 and = 0.5 sees an example at x = 4 with target 1. What's the cross-entropy gradient with respect to ?
Check
Where is the decision boundary of a logistic model with = −2 and = 0.5?
Check
A logistic model has weight 0.7 on a feature. By what factor do the odds change when the feature increases by 1?
Check
What does one example at x = 3 contribute to the Hessian's slope entry for a model with = −3, = 1?
Check
A logistic model with = −1 and = 0.5 sees one example at x = 3 with target 1.
(a)
What is the gradient of the cross-entropy loss with respect to ?
(b)
What is the second derivative with respect to ?
Check
A logistic model has bias a and weight w; consider an input x.
Step 1
What probability does it give class 1?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the cross-entropy loss if the target is 1?
Step 3
And if the target is 0?
Explain why
Why do logistic regression's weights grow without limit on perfectly separable data?
Maximum likelihood rewards making every training example's predicted probability of its own class as close to 1 as possible. If some hyperplane separates the classes perfectly, scaling up the weights along it leaves the boundary where it is but makes the sigmoid steeper, pushing every correct probability closer to 1 and raising the likelihood a little more each time. Nothing stops the process, so the weights diverge and the model becomes certain of everything near the boundary, which is badly overconfident on new data. Adding a penalty on the weights, equivalently a Gaussian prior, makes the optimum finite.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.