Open Campus
Ask the professor
Episodes

The clip for this episode plays here.

Watch on YouTube
Clips from MIT 6.036 Lecture 4: Logistic regression (Fall 2020), by Tamara Broderick on YouTube. Each episode plays only its own part of the lecture.

A hard line is not enough

Why we want a model that can say how sure it is.

A such as the perceptron draws one boundary and says which side a point is on. It has no way to say “I am not certain here”.

Real data is messier. In the medical example from the lecture there is no perfect division on any single indicator, because so much about a person is unknown or random. We would like the model to say how likely each label is, as a .

Tricks such as averaging the perceptron can help with data that is not separable, but they are ad hoc and come without a nice guarantee.

The sigmoid

A smooth S-shaped curve from sure-yes to sure-no.

The (also called the logistic function) takes any score z and returns a value between 0 and 1. For a very low z it gets close to 0, and for a very high z it gets close to 1.

σ(z) = 1 / (1 + e^−z)

Multiplying the input by a number squeezes or stretches the curve, and adding a constant slides it left or right. Those two knobs are what training will tune.

g(x) = σ(θx + θ₀)

The 50-50 line

Where the model is exactly undecided.

With features x₁ and x₂ the model becomes g(x) = σ(θᵀx + θ₀). Where θᵀx + θ₀ equals 0, the output is exactly 1/2. That is a line (a hyperplane in more dimensions) and it is the .

g(x) = σ(θᵀx + θ₀)

Anywhere θᵀx + θ₀ is a constant, g has the same value, so the picture is a set of parallel lines. Move in the direction of θ and the output climbs toward 1. Move the other way and it falls toward 0.

Making a prediction

From a probability to a yes or a no.

To predict, take the positive label when θᵀx + θ₀ is greater than 0, and the other label when it is less than or equal to 0.

predict +1 if θᵀx + θ₀ > 0, otherwise −1

That is exactly the family of linear classifiers from earlier in the course. What is new is the confidence attached to each prediction, and a new way to choose θ and θ₀: pick the curve that fits the data best. The lecture shows a bad curve that gives a high probability of coat on days with no coat.

How likely is the data?

Scoring a curve by what really happened.

For each day, the curve gives a probability to what actually happened, whether a coat or no coat. The probability of the whole data set is the product of those probabilities, because the days are treated as independent events.

probability of data = product of the probability of each day

There is a catch: a product of many numbers between 0 and 1 gets very small, very fast. The lecture fixes this in the next episode with a logarithm.

From probability to loss

The log, the minus sign and the loss we minimize.

The log is monotonic, so the weights that maximize the probability are exactly the weights that maximize its log, and sums are friendlier than products. Earlier in the course we worked with losses, where lower is better, so we take the negative. Averaged over the data, this is the .

loss = −log( probability given to the true label )

One difference from before: the guess is now a probability between 0 and 1, not a plain yes or no.

Walking downhill

How gradient descent finds good weights.

Because the loss is smooth, it has a gradient, and the gradient points in the direction of steepest climb. starts somewhere, takes a step in the opposite direction with a chosen step size, and repeats.

θ ← θ − η · ∇loss(θ)

You can stop when the loss barely changes, when θ barely moves, or when the gradient is very small. The method is general: it minimizes any function, not only this loss.

Convexity: a guarantee

Why walking downhill reaches the bottom.

A function is if, for any two points on its graph, the straight segment between them never dips below the graph. A simple quadratic is convex. A function with two dips is not, because a segment between the dips goes below it.

For a convex function every local optimum is a global one, so gradient descent run long enough, with sensible settings, gets to the best answer. The lecture notes that the logistic regression loss is convex, so descent works well for it.

Watching it learn

What each step does to the curve.

Running gradient descent on the coat problem, the sigmoid changes at each step. It shifts, so the temperature where you start wearing a coat moves, and it gets steeper, because a steeper curve can raise the probability of what really happened.

The lecture ends this part with a worry: for some data the curve can keep getting steeper and still raise those probabilities. That leads straight to the next episode.

Not too sure

Why a regularizer holds the weights back.

As θ gets larger, every probability gets pushed to an extreme, and that always lowers the loss. But we do not want that: a huge θ is too confident, and we would need a lot of data to justify it. A adds a penalty for large weights.

penalty = λ · size of θ,  with λ ≥ 0

The constant λ is a trade-off: the larger it is, the more the penalty counts against fitting the data. A common way to choose it is to try a few orders of magnitude and compare them with cross-validation.

The full recipe

Logistic regression with a penalty, trained by gradient descent.

The loss with the penalty added is what describes logistic regression. We can classify with it and run gradient descent on it, which means taking the gradient of that objective at each step.

Gradient descent is not the only way to minimize this objective. The lecture closes with a practical note: with millions of data points it feels wasteful to add up all of them before taking a single step, which is a hint of what comes next.