S1416 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 σ(w0+w1x) of the positive class against the input x.
  • The decision boundary, where the probability crosses 1/2, is at x=−w0/w1. A larger w1 makes the transition sharper.
  • Each unit increase in x multiplies the odds p/(1−p) by ew1.

Predict

A logistic model has w0 = −3 and w1 = 1.5. What probability does it give the positive class at x = 3?

Name it

The model

p(𝒞1|𝐱)=σ(𝐰T𝝓(𝐱)),σ(a)=11+e−a

The activation a is the log-odds, ln⁡p1−p; increasing a feature by one multiplies the odds by ew. The sigmoid's derivative is σ(1−σ).

Fitting

With targets tn∈{0,1} and predictions yn, the negative log-likelihood is the cross-entropy

E(𝐰)=−∑n[tnln⁡yn+(1−tn)ln⁡(1−yn)],∇E=∑n(yn−tn)𝝓n

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 H=∑nyn(1−yn)𝝓n𝝓nT, 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
  1. Prediction. At x = 3 the model gives y = 0.818, so y−t=−0.182.
  2. Gradient. ∇E=(y−t)(1,x)=(−0.182,−0.547) Gradient descent raises both weights, pushing the probability at x = 3 toward 1.
  3. Hessian contribution. The slope entry from this example is y(1−y)x2=0.149×9=1.34

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 w1 = 1.5) to reach 0.5?

Show the solution
  1. Log-odds. a=ln⁡(0.9/0.1)=2.197
  2. Move x. The activation must fall by 2.197, so x must decrease by 2.197/1.5=1.46

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
  1. Probabilities. Normalize the exponentials: e2+e+1=11.11 so the probabilities are 0.665, 0.245 and 0.090.
  2. Loss. L=−ln⁡0.245=1.41nats
  3. Gradient. With respect to activation k it's yk−tk: 𝐲−𝐭=(0.665,−0.755,0.090)
  4. 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 w0 = −4 and w1 = 0.5 sees an example at x = 4 with target 1. What's the cross-entropy gradient with respect to w1?

Check

Where is the decision boundary of a logistic model with w0 = −2 and w1 = 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 w0 = −3, w1 = 1?

Check

A logistic model with w0 = −1 and w1 = 0.5 sees one example at x = 3 with target 1.

(a)

What is the gradient of the cross-entropy loss with respect to w1?

(b)

What is the second derivative with respect to w1?

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°.

Explain why

Why do logistic regression's weights grow without limit on perfectly separable data?

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.