Skip to content

Logistic Regression: Learning Probabilities with Gradient Descent | NTU Hsuan-Tien Lin Machine Learning Foundations Lecture 10

Oct 13, 20261 min
TL;DRLogistic regression uses θ(wᵀx) to estimate P(+1|x), yet the data contains only circles and crosses. This lecture derives the cross-entropy error ln(1 + exp(−y wᵀx)) from maximum likelihood and uses the chain rule to show that the gradient is a θ-weighted average of −yₙxₙ. Setting the gradient to zero gives a non-linear equation of w with no closed-form solution, so the lecture switches to fixed learning rate gradient descent, w ← w − η∇E_in(w), and walks to the bottom of the valley one step at a time.

🌏 中文版

Video status: Videos included. This article is written from the official MOOC handout 10_handout.pdf and transcripts of this lecture's four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/25 to 25/25), and wherever the text says "in the lecture" it is paraphrasing what is said in the video.

This is Foundations Lecture 10 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. The Roadmap on page 1 of the handout places it under the third of the four big questions, "How Can Machines Learn?", right after Foundations Lecture 9: Linear Regression. The previous lecture takes up a single line on that page: linear regression hypotheses with squared error have the analytic solution wLIN=X†y\mathbf{w}_{\text{LIN}} = X^\dagger \mathbf{y}. In the lecture, Lin opens by saying that Logistic Regression and Linear Regression differ by a single word, and this lecture is about what that word brings.

There are two differences. First, the output should be a probability between 0 and 1, but the data contains no probabilities, only whether something happened or not. Second, the error function that comes out of the derivation cannot be minimized in one step the way linear regression's can; you have to walk there step by step.

The prerequisites are the "set the gradient to zero" solution of linear regression, the update rule from Foundations Lecture 2: Perceptrons and PLA, the target distribution P(y∣x)P(y \mid \mathbf{x}) and pointwise error from Foundations Lecture 8: Noise and Error Measures, and partial derivatives, the chain rule, exponentials, and logarithms. After reading, you should be able to go from "pick the hypothesis with the largest likelihood" to the cross-entropy error on your own, compute its gradient with the chain rule, explain why there is no closed-form solution, derive the gradient descent update, and work out a few updates by hand on a small data set.

The derivation in this lecture runs in this order:

  1. Problem: the target function changes from ±1\pm 1 to the probability P(+1∣x)P(+1 \mid \mathbf{x}).
  2. Hypothesis: the score w⊤x\mathbf{w}^\top\mathbf{x} goes through a logistic function θ\theta.
  3. Error: maximum likelihood gives cross-entropy.
  4. Gradient: the chain rule gives ∇Ein(w)\nabla E_{\text{in}}(\mathbf{w}).
  5. Stuck: the gradient-equals-zero equation cannot be solved.
  6. Algorithm: gradient descent, one step at a time toward the bottom of the valley.

Turning the target into a probability: soft binary classification and the logistic hypothesis

YouTube:Logistic Regression :: Logistic Regression Problem @ Machine Learning Foundations (機器學習基石)

(Video 00:00–01:00 reviews the previous lecture, 01:00–04:03 covers the heart disease problem, 04:03–07:47 ideal and actual data, 07:47–13:06 the logistic hypothesis and the logistic function, and Fun Time follows.)

From "yes or no" to "how much risk"

Page 2 of the handout brings back the learning flow diagram with a hospital example: the input is a patient's age, gender, blood pressure, cholesterol level, and weight (the patient on the slide is 40 years old, male, blood pressure 130/85, cholesterol level 240, weight 70), and the output is "heart disease? yes". This is ordinary binary classification. Foundations Lecture 8 explained that noisy data is described by a target distribution P(y∣x)P(y \mid \mathbf{x}), and that under the 0/1 error the ideal target function is

f(x)=sign(P(+1∣x)−12)∈{−1,+1}f(\mathbf{x}) = \text{sign}\Big(P(+1 \mid \mathbf{x}) - \frac{1}{2}\Big) \in \{-1, +1\}

In other words, it looks at which side of one half the probability falls on. The handout notes that this choice is "because of classification err": what we care about is the fraction of mistakes. In the lecture, Lin adds that, to keep the figure simple, the box for the P(x)P(\mathbf{x}) that generates the inputs is not drawn.

Page 3 changes one word of the problem: heart disease becomes heart attack, and the output changes from yes to "80% risk". In the lecture, Lin explains that the question a doctor often has to answer is how likely a heart attack (or a cancer recurrence) is some months from now, not merely whether it will happen. What we want to know is then the value P(+1∣x)P(+1 \mid \mathbf{x}) itself, and the target function becomes

f(x)=P(+1∣x)∈[0,1]f(\mathbf{x}) = P(+1 \mid \mathbf{x}) \in [0, 1]

The handout calls this 'soft' binary classification: the output is no longer a hard circle or cross but a risk value.

The data we want and the data we can get

To learn an ff that outputs probabilities, the ideal data would attach a probability to every xn\mathbf{x}_n. The left column on page 4 of the handout calls this ideal (noiseless) data:

ideal (noiseless) dataactual (noisy) data
Example 1y1′=0.9=P(+1∣x1)y'_1 = 0.9 = P(+1 \mid \mathbf{x}_1)y1=∘y_1 = \circ, drawn from P(y∣x1)P(y \mid \mathbf{x}_1)
Example 2y2′=0.2=P(+1∣x2)y'_2 = 0.2 = P(+1 \mid \mathbf{x}_2)y2=×y_2 = \times, drawn from P(y∣x2)P(y \mid \mathbf{x}_2)
Example NNyN′=0.6=P(+1∣xN)y'_N = 0.6 = P(+1 \mid \mathbf{x}_N)yN=×y_N = \times, drawn from P(y∣xN)P(y \mid \mathbf{x}_N)

With the left column, things would be much like regression: find a hypothesis whose predictions at these points are close to 0.9, 0.2, and 0.6. But the left column is not available. In the lecture, Lin uses hospital records to explain: the record says whether the patient had a heart attack two or three months later, and nobody knows whether the probability was 0.9 or 0.2.

So all we have is the right column. The second slide of the same page rewrites the right column as numbers: with 1 for a circle and 0 for a cross, the three examples are y1′=1y'_1 = 1, y2′=0y'_2 = 0, and yN′=0y'_N = 0, each the outcome of "draw once from P(y∣xn)P(y \mid \mathbf{x}_n); did a circle come up?" In the lecture, Lin reads this as: the actual data is a noisy version of the ideal data. We wanted 0.9 and got 1 after sampling; we wanted 0.2 and got 0; the third example has probability 0.6, larger than one half, yet the sample happened to land on the other side and gave 0, a rather large error.

The handout wraps up in one sentence: "same data as hard binary classification, different target function". The data has exactly the same form as in ordinary binary classification; what is to be learned is different. The question for the rest of the lecture is how to learn a hypothesis that outputs probabilities when all we have is circles and crosses.

The logistic hypothesis: pass the score through an S-shaped function

The first step of the hypothesis is the same as in earlier lectures (page 5). Write the patient's features as x=(x0,x1,x2,…,xd)\mathbf{x} = (x_0, x_1, x_2, \dots, x_d), where x0x_0 is the constant term, and compute a weighted "risk score":

s=∑i=0dwixi=w⊤xs = \sum_{i=0}^{d} w_i x_i = \mathbf{w}^\top\mathbf{x}

Linear regression outputs ss directly, but ss can be any real number and we want a value between 0 and 1. The approach is to say that a higher score means higher risk, and then use an S-shaped function θ\theta to convert the score to a probability. The handout calls θ\theta the logistic function and writes the hypothesis as

h(x)=θ(w⊤x)h(\mathbf{x}) = \theta(\mathbf{w}^\top\mathbf{x})

In the figure on page 5 the horizontal axis is the score ss and the vertical axis runs from 0 to 1. The blue curve starts at the lower left hugging 0, climbs around s=0s = 0, and approaches 1 to the right. In the lecture, Lin reads it this way: with a score of 1126, go to 1126 on the horizontal axis, look up to the curve, and read off the value to output; the same goes for a score of −5-5.

The formula of the logistic function

Page 6 gives the formula:

θ(s)=es1+es=11+e−s\theta(s) = \frac{e^{s}}{1 + e^{s}} = \frac{1}{1 + e^{-s}}

The second equality divides the numerator and denominator by ese^s. The handout lists three values, and the lecture checks them one by one:

  • θ(0)=11+1=12\theta(0) = \frac{1}{1 + 1} = \frac{1}{2}, exactly in the middle.
  • As s→∞s \to \infty, using the right-hand form, e−s→0e^{-s} \to 0, so θ(s)→1\theta(s) \to 1. The lecture uses the left-hand form instead: the numerator and denominator both go to infinity and are about the same size, so the ratio goes to 1; Lin acknowledges that infinity divided by infinity needs a more rigorous definition mathematically.
  • As s→−∞s \to -\infty, using the left-hand form, the numerator es→0e^{s} \to 0 and the denominator approaches 1, so θ(s)→0\theta(s) \to 0.

The handout describes it as a "smooth, monotonic, sigmoid function of ss": smooth (differentiable everywhere), monotonic, and S-shaped. This article plugs in a few numbers to get a feel for how fast it changes:

ss−5-5−2-2−1-100112255
θ(s)\theta(s)0.00670.11920.26890.50.73110.88080.9933

The output changes fastest for scores between −2-2 and 22, and beyond ±5\pm 5 it is almost flat against 0 or 1. The table also shows a property that will be used later: θ(−1)+θ(1)=1\theta(-1) + \theta(1) = 1 and θ(−2)+θ(2)=1\theta(-2) + \theta(2) = 1; two scores placed symmetrically about zero have outputs that add up to 1.

Putting it together, logistic regression uses

h(x)=11+exp⁡(−w⊤x)h(\mathbf{x}) = \frac{1}{1 + \exp(-\mathbf{w}^\top\mathbf{x})}

to approximate the target function f(x)=P(+1∣x)f(\mathbf{x}) = P(+1 \mid \mathbf{x}).

Fun Time: converting a probability back to circle or cross

Question (page 7): Consider any logistic hypothesis h(x)=11+exp⁡(−w⊤x)h(\mathbf{x}) = \frac{1}{1 + \exp(-\mathbf{w}^\top\mathbf{x})}. 'Convert' it to a binary classification prediction by taking sign(h(x)−12)\text{sign}\big(h(\mathbf{x}) - \frac{1}{2}\big). What is the equivalent formula for the binary classification prediction?

  1. sign(w⊤x−12)\text{sign}\big(\mathbf{w}^\top\mathbf{x} - \frac{1}{2}\big)
  2. sign(w⊤x)\text{sign}\big(\mathbf{w}^\top\mathbf{x}\big)
  3. sign(w⊤x+12)\text{sign}\big(\mathbf{w}^\top\mathbf{x} + \frac{1}{2}\big)
  4. none of the above

Answer: 2. The handout's reason is that when w⊤x=0\mathbf{w}^\top\mathbf{x} = 0, h(x)h(\mathbf{x}) is exactly 12\frac{1}{2}, so thresholding hh at 12\frac{1}{2} is the same as thresholding the score at 0. Filling in the middle step: θ\theta is strictly increasing, so θ(s)>12=θ(0)\theta(s) > \frac{1}{2} = \theta(0) if and only if s>0s > 0, and θ(s)<12\theta(s) < \frac{1}{2} if and only if s<0s < 0.

Choice 1 puts the threshold at a score of 12\frac{1}{2}, which corresponds to the probability θ(0.5)≈0.622\theta(0.5) \approx 0.622; that is, it says circle only when the probability exceeds 0.622. Choice 3 puts the threshold at a score of −12-\frac{1}{2}, corresponding to θ(−0.5)≈0.378\theta(-0.5) \approx 0.378. Neither is the same as "more than one half".

The conclusion the lecture draws from this question is that a logistic hypothesis is really just a counterpart of the earlier binary classification hypothesis. An addition in this article: the boundary is still the hyperplane w⊤x=0\mathbf{w}^\top\mathbf{x} = 0; what the logistic hypothesis reports in addition is the probability that corresponds to how far the score is from 0.

Defining the error: from likelihood to cross-entropy

YouTube:Logistic Regression :: Logistic Regression Error @ Machine Learning Foundations (機器學習基石)

(Video 00:00–01:46 puts the three linear models side by side, 01:46–06:18 covers likelihood, 06:18–09:45 the symmetry of the logistic function and the product form, 09:45–14:47 the rearrangement into cross-entropy, and Fun Time follows.)

Three linear models, one error missing

Page 8 puts the three models learned so far together. All of them first compute the same linear score s=w⊤xs = \mathbf{w}^\top\mathbf{x}; they differ in what happens to the score afterwards:

linear classificationlinear regressionlogistic regression
Hypothesis h(x)h(\mathbf{x})sign(s)\text{sign}(s)ssθ(s)\theta(s)
Output function in the figurestepslanted straight lineS-shaped curve
Error0/1squared?
The handout's reasonplausible (corresponds to small flipping noise)friendly (easy to minimize)to be decided

In the lecture, Lin explains the icons: the box for linear classification shows a step-shaped sign; linear regression shows a slanted line, meaning the score is output unchanged (identity); logistic regression shows an S shape. The reason for the 0/1 error is that it is the most convincing, right or wrong; the reason for the squared error is that it leads to a clean algorithm. The third cell is still empty, and the handout asks "how to define Ein(w)E_{\text{in}}(\mathbf{w}) for logistic regression?"

Likelihood: let h pretend to be f

Page 9 first spells out the relation between the target function and the target distribution. f(x)=P(+1∣x)f(\mathbf{x}) = P(+1 \mid \mathbf{x}) is equivalent to

P(y∣x)={f(x)for y=+11−f(x)for y=−1P(y \mid \mathbf{x}) = \begin{cases} f(\mathbf{x}) & \text{for } y = +1 \\ 1 - f(\mathbf{x}) & \text{for } y = -1 \end{cases}

because the probabilities of the two outcomes add up to 1.

Now consider a data set D={(x1,∘),(x2,×),…,(xN,×)}\mathcal{D} = \{(\mathbf{x}_1, \circ), (\mathbf{x}_2, \times), \dots, (\mathbf{x}_N, \times)\}. What is the probability that ff generates this data? Each example is produced by first drawing xn\mathbf{x}_n and then drawing that label from P(y∣xn)P(y \mid \mathbf{x}_n); the examples are independent, so the probability of the whole data set is a product:

P(x1)P(∘∣x1)×P(x2)P(×∣x2)×⋯×P(xN)P(×∣xN)P(\mathbf{x}_1)P(\circ \mid \mathbf{x}_1) \times P(\mathbf{x}_2)P(\times \mid \mathbf{x}_2) \times \dots \times P(\mathbf{x}_N)P(\times \mid \mathbf{x}_N)

Use the relation above to replace P(∘∣x)P(\circ \mid \mathbf{x}) with f(x)f(\mathbf{x}) and P(×∣x)P(\times \mid \mathbf{x}) with 1−f(x)1 - f(\mathbf{x}):

P(x1)f(x1)×P(x2)(1−f(x2))×⋯×P(xN)(1−f(xN))P(\mathbf{x}_1)f(\mathbf{x}_1) \times P(\mathbf{x}_2)\big(1 - f(\mathbf{x}_2)\big) \times \dots \times P(\mathbf{x}_N)\big(1 - f(\mathbf{x}_N)\big)

But ff is unknown. The lecture's move is to let hh pretend to be ff for a moment: pretend hh is the function that generated the data and replace every ff in the expression with hh. That gives "how likely it is that hh generates the same data", a quantity called the likelihood:

likelihood(h)=P(x1)h(x1)×P(x2)(1−h(x2))×⋯×P(xN)(1−h(xN))\text{likelihood}(h) = P(\mathbf{x}_1)h(\mathbf{x}_1) \times P(\mathbf{x}_2)\big(1 - h(\mathbf{x}_2)\big) \times \dots \times P(\mathbf{x}_N)\big(1 - h(\mathbf{x}_N)\big)

The handout then makes two points:

  • If h≈fh \approx f, then likelihood(h)≈\text{likelihood}(h) \approx probability using ff.
  • The probability using ff is usually large. In the lecture, Lin explains that the data in hand really was generated by ff, so in hindsight the probability that ff generates this data should not be small, and says this is an intuition.

Put the two together: a good hh should have a large likelihood. So the choice on page 10 is

g=argmaxh likelihood(h)g = \underset{h}{\text{argmax}}\ \text{likelihood}(h)

Among all hypotheses, pick the one with the largest likelihood as gg.

Working it once with the handout's three probabilities

What follows is an example made up for this article from the numbers on page 4 of the handout; it is not on the slides. Suppose there are only three examples, the true probabilities are f(x1)=0.9f(\mathbf{x}_1) = 0.9, f(x2)=0.2f(\mathbf{x}_2) = 0.2, and f(x3)=0.6f(\mathbf{x}_3) = 0.6, and the labels drawn are circle, cross, cross. The P(xn)P(\mathbf{x}_n) part is the same for every hypothesis, so set it aside and compare only the product of the remaining three factors:

Hypothesis outputs at the three pointsProductValue
ff itself: (0.9,0.2,0.6)(0.9, 0.2, 0.6)0.9×0.8×0.40.9 \times 0.8 \times 0.40.288
Close to ff: (0.8,0.3,0.5)(0.8, 0.3, 0.5)0.8×0.7×0.50.8 \times 0.7 \times 0.50.280
Says one half for everything: (0.5,0.5,0.5)(0.5, 0.5, 0.5)0.5×0.5×0.50.5 \times 0.5 \times 0.50.125
The opposite of ff: (0.1,0.8,0.4)(0.1, 0.8, 0.4)0.1×0.2×0.60.1 \times 0.2 \times 0.60.012

A hypothesis close to ff has a likelihood close to ff's 0.288; a hypothesis that contradicts ff is left with a likelihood of only 0.012. That is what the handout's first point means.

An addition in this article, on something to watch for. If any function were allowed as a hypothesis, (1,0,0)(1, 0, 0) would have likelihood 1×1×1=11 \times 1 \times 1 = 1, higher than ff's 0.288: it memorizes the labels it saw and states the probabilities with certainty. So for "the largest likelihood is close to ff" to hold, the hypothesis set has to be restricted (here to the form θ(w⊤x)\theta(\mathbf{w}^\top\mathbf{x}), with finite dVCd_{\text{VC}}) and there has to be enough data, the same condition under which the earlier lectures could go from "small EinE_{\text{in}}" to "small EoutE_{\text{out}}". The handout's word is "usually", not "always".

Symmetry: writing circles and crosses as one expression

The likelihood has two kinds of factors: h(xn)h(\mathbf{x}_n) for a circle and 1−h(xn)1 - h(\mathbf{x}_n) for a cross. Page 10 unifies them with a property of the logistic function. First verify the property:

1−θ(s)=1−11+e−s=e−s1+e−s=1es+1=θ(−s)1 - \theta(s) = 1 - \frac{1}{1 + e^{-s}} = \frac{e^{-s}}{1 + e^{-s}} = \frac{1}{e^{s} + 1} = \theta(-s)

The third equality multiplies the numerator and denominator by ese^{s}. For h(x)=θ(w⊤x)h(\mathbf{x}) = \theta(\mathbf{w}^\top\mathbf{x}), this is what the handout writes as

1−h(x)=θ(−w⊤x)=h(−x)1 - h(\mathbf{x}) = \theta(-\mathbf{w}^\top\mathbf{x}) = h(-\mathbf{x})

The lecture calls this property symmetry and asks the audience to look at the S-shaped curve on the right of the slide and convince themselves it is true. An addition in this article, on what it means in the picture: the curve is symmetric about the point (0,12)(0, \frac{1}{2}), and the heights at ss and −s-s add up to exactly 1.

So the cross factor 1−h(xn)1 - h(\mathbf{x}_n) can be written as h(−xn)h(-\mathbf{x}_n), and the likelihood becomes

likelihood(h)=P(x1)h(+x1)×P(x2)h(−x2)×⋯×P(xN)h(−xN)\text{likelihood}(h) = P(\mathbf{x}_1)h(+\mathbf{x}_1) \times P(\mathbf{x}_2)h(-\mathbf{x}_2) \times \dots \times P(\mathbf{x}_N)h(-\mathbf{x}_N)

A circle has label yn=+1y_n = +1 and a cross has yn=−1y_n = -1, so every factor is h(ynxn)h(y_n\mathbf{x}_n). The slide draws all the P(xn)P(\mathbf{x}_n) in gray, and in the lecture Lin explains why: when comparing different hh, everyone uses the same x1\mathbf{x}_1 through xN\mathbf{x}_N, so this part is the same for every hh and does not affect which is larger. Dropping it leaves

likelihood(logistic h)∝∏n=1Nh(ynxn)\text{likelihood}(\text{logistic } h) \propto \prod_{n=1}^{N} h(y_n\mathbf{x}_n)

The symbol "∝\propto" means proportional to; the constant of proportionality is the product of those P(xn)P(\mathbf{x}_n).

Take ln, add a minus sign, divide by N

Page 11 turns "maximize a product" into the familiar "minimize an average error" in four steps.

Step 1: replace hh with w\mathbf{w}. Every logistic hypothesis is determined by w\mathbf{w}, with h(ynxn)=θ(ynw⊤xn)h(y_n\mathbf{x}_n) = \theta(y_n\mathbf{w}^\top\mathbf{x}_n):

max⁡w likelihood(w)∝∏n=1Nθ(ynw⊤xn)\max_{\mathbf{w}}\ \text{likelihood}(\mathbf{w}) \propto \prod_{n=1}^{N} \theta\big(y_n\mathbf{w}^\top\mathbf{x}_n\big)

Step 2: take ln. The lecture's reason is that every EinE_{\text{in}} handled in earlier lectures was a sum, and products have not been handled; taking the logarithm turns the product into a sum, and since the logarithm is monotonic, the largest stays the largest. The natural logarithm ln⁡\ln is used to make differentiation convenient later.

max⁡w ln⁡∏n=1Nθ(ynw⊤xn)=max⁡w ∑n=1Nln⁡θ(ynw⊤xn)\max_{\mathbf{w}}\ \ln\prod_{n=1}^{N} \theta\big(y_n\mathbf{w}^\top\mathbf{x}_n\big) = \max_{\mathbf{w}}\ \sum_{n=1}^{N} \ln\theta\big(y_n\mathbf{w}^\top\mathbf{x}_n\big)

Step 3: add a minus sign, then multiply by 1N\frac{1}{N}. Everything so far has been a minimization, so a minus sign turns the max into a min. The 1N\frac{1}{N} is gray on the slide; in the lecture, Lin says it is just a constant scaling that treats every w\mathbf{w} equally and does not affect the optimal solution, and that it is put in to make the expression look like the earlier EinE_{\text{in}}:

min⁡w 1N∑n=1N−ln⁡θ(ynw⊤xn)\min_{\mathbf{w}}\ \frac{1}{N}\sum_{n=1}^{N} -\ln\theta\big(y_n\mathbf{w}^\top\mathbf{x}_n\big)

Step 4: substitute the formula for θ\theta. θ(s)=11+exp⁡(−s)\theta(s) = \frac{1}{1 + \exp(-s)}, so

−ln⁡θ(s)=−ln⁡11+exp⁡(−s)=ln⁡(1+exp⁡(−s))-\ln\theta(s) = -\ln\frac{1}{1 + \exp(-s)} = \ln\big(1 + \exp(-s)\big)

The lecture says the same thing: θ\theta has its expression in the denominator, pulling it out of the logarithm produces another minus sign, and that cancels the minus sign in front. Substituting s=ynw⊤xns = y_n\mathbf{w}^\top\mathbf{x}_n:

min⁡w 1N∑n=1Nln⁡(1+exp⁡(−ynw⊤xn))⏟Ein(w)\min_{\mathbf{w}}\ \underbrace{\frac{1}{N}\sum_{n=1}^{N} \ln\Big(1 + \exp\big(-y_n\mathbf{w}^\top\mathbf{x}_n\big)\Big)}_{E_{\text{in}}(\mathbf{w})}

Each term in the sum depends on a single example; it is the pointwise error measure from Foundations Lecture 8. The handout calls it the cross-entropy error:

err(w,x,y)=ln⁡(1+exp⁡(−yw⊤x))\text{err}(\mathbf{w}, \mathbf{x}, y) = \ln\Big(1 + \exp\big(-y\mathbf{w}^\top\mathbf{x}\big)\Big)

EinE_{\text{in}} is its average over the NN examples. The summary on page 25 of the handout gives it another name, negative log likelihood, which is exactly the three operations just performed.

Back to the three-example calculation: ff itself gives −13ln⁡(0.288)≈0.415-\frac{1}{3}\ln(0.288) \approx 0.415, and the hypothesis that says one half for everything gives −13ln⁡(0.125)=ln⁡2≈0.693-\frac{1}{3}\ln(0.125) = \ln 2 \approx 0.693. A larger likelihood means a smaller error, with the ranking exactly reversed, so minimizing EinE_{\text{in}} and maximizing the likelihood pick the same hypothesis.

The shape and the name of cross-entropy

The error is determined by the single number ys=yw⊤xys = y\mathbf{w}^\top\mathbf{x}. ys>0ys > 0 means the sign of the score agrees with the label (as a classifier it is correct), and ys<0ys < 0 means it is wrong. This article computes a few values:

yw⊤xy\mathbf{w}^\top\mathbf{x}−5-5−2-2−1-100112255
err5.00672.12691.31330.69310.31330.12690.0067

Three observations. When the example is classified correctly, the farther the score is from 0 the closer the error gets to 0, but it is never exactly 0. At ys=0ys = 0 the error is ln⁡2≈0.6931\ln 2 \approx 0.6931, the price paid when the hypothesis says "fifty-fifty". On the wrong side, the error is almost as large as −ys-ys (5.0067 at ys=−5ys = -5): the worse the mistake, the heavier the penalty, with no upper limit.

As for where the name comes from, the lecture says only that there are historical reasons, does not go into the details, and suggests that students with a reference book look it up. An addition in this article: a way of writing the error that makes the name visible, derived using only the formulas above. Relabel the examples as y′=1y' = 1 (circle) or y′=0y' = 0 (cross), and write h=h(x)h = h(\mathbf{x}):

  • When y=+1y = +1, err=ln⁡(1+e−s)=−ln⁡θ(s)=−ln⁡h\text{err} = \ln(1 + e^{-s}) = -\ln\theta(s) = -\ln h.
  • When y=−1y = -1, err=ln⁡(1+es)=−ln⁡θ(−s)=−ln⁡(1−h)\text{err} = \ln(1 + e^{s}) = -\ln\theta(-s) = -\ln(1 - h).

The two cases combine into one line:

err=−[y′ln⁡h+(1−y′)ln⁡(1−h)]\text{err} = -\Big[y'\ln h + (1 - y')\ln(1 - h)\Big]

Fall 2024 HW4 Q1 tests exactly this equivalent form after relabeling, and the problem also explains that the name cross-entropy comes from the form plog⁡q+(1−p)log⁡(1−q)p\log q + (1 - p)\log(1 - q).

Fun Time: four properties of cross-entropy

Question (page 12): The four statements below help us understand more about the cross-entropy error err(w,x,y)=ln⁡(1+exp⁡(−yw⊤x))\text{err}(\mathbf{w}, \mathbf{x}, y) = \ln\big(1 + \exp(-y\mathbf{w}^\top\mathbf{x})\big). Consider w⊤x≠0\mathbf{w}^\top\mathbf{x} \ne 0. Which statement is not true?

  1. For any w\mathbf{w}, x\mathbf{x}, and yy, err(w,x,y)>0\text{err}(\mathbf{w}, \mathbf{x}, y) > 0.
  2. For any w\mathbf{w}, x\mathbf{x}, and yy, err(w,x,y)<1126\text{err}(\mathbf{w}, \mathbf{x}, y) < 1126.
  3. When y=sign(w⊤x)y = \text{sign}(\mathbf{w}^\top\mathbf{x}), err(w,x,y)<ln⁡2\text{err}(\mathbf{w}, \mathbf{x}, y) < \ln 2.
  4. When y≠sign(w⊤x)y \ne \text{sign}(\mathbf{w}^\top\mathbf{x}), err(w,x,y)≥ln⁡2\text{err}(\mathbf{w}, \mathbf{x}, y) \ge \ln 2.

Answer: 2. The handout's answer is "1126, really? :-)", and it encourages plotting the curve of the error with respect to some fixed yy and a varying score s=w⊤xs = \mathbf{w}^\top\mathbf{x}; after plotting, it is easy to see that the error is not bounded above and that the other three choices are correct. In the lecture, Lin jokes that a choice containing a strange number is usually the wrong one. The four choices are reasoned through one by one below, writing t=yw⊤xt = y\mathbf{w}^\top\mathbf{x}:

  • Choice 1 is true. exp⁡(−t)>0\exp(-t) > 0, so 1+exp⁡(−t)>11 + \exp(-t) > 1, and its ln⁡\ln is greater than 0.
  • Choice 2 is false. Take t=−1126t = -1126 (for example, in one dimension, w=1126w = 1126, x=1x = 1, y=−1y = -1); then err=ln⁡(1+e1126)>ln⁡(e1126)=1126\text{err} = \ln(1 + e^{1126}) > \ln(e^{1126}) = 1126. Push tt further in the negative direction and the error can be as large as you like.
  • Choice 3 is true. yy and the score have the same sign, so t>0t > 0, exp⁡(−t)<1\exp(-t) < 1, and err<ln⁡2\text{err} < \ln 2.
  • Choice 4 is true. yy and the score have opposite signs, so t<0t < 0, exp⁡(−t)>1\exp(-t) > 1, and err>ln⁡2\text{err} > \ln 2, which certainly satisfies "≥\ge".

The lecture singles out choices 3 and 4: a score of 0 is the switching point, with the error above ln⁡2\ln 2 on one side and held below ln⁡2\ln 2 on the other.

What the gradient looks like, and why there is no closed-form solution

YouTube:Logistic Regression :: Gradient of Logistic Regression Error @ Machine Learning Foundations (機器學習基石)

(Video 00:00–01:38 covers the properties of EinE_{\text{in}}, 01:38–07:28 derives the gradient with the chain rule, 07:28–10:30 explains why there is no closed-form solution, 10:30–14:12 revisits PLA and iterative optimization, and Fun Time follows.)

E_in is a valley

With EinE_{\text{in}} in hand, what remains is finding the w\mathbf{w} that minimizes it. The lecture first recalls why: this set of hypotheses has a finite VC dimension (see Foundations Lecture 7: The VC Dimension), so making EinE_{\text{in}} small makes EoutE_{\text{out}} small too. Two minimization methods have been learned so far: the mistake-correcting approach of linear classification, and linear regression's "compute the gradient and find where it is zero". The latter applies to smooth, differentiable functions.

Page 13 says the EinE_{\text{in}} of logistic regression is continuous, differentiable, twice-differentiable, and convex. A convex function looks like a valley, and the bottom of the valley is where the gradient is zero, so the goal is the same as in linear regression:

want∇Ein(w)=0\text{want}\quad \nabla E_{\text{in}}(\mathbf{w}) = \mathbf{0}

The handout does not prove "convex". In the lecture, Lin says the proof computes the matrix of second derivatives and confirms that it is positive definite, does not carry it out, and asks the audience to take it on trust for now. The check below is an addition in this article and is not on the slides; it uses the gradient derived in the subsection "Peeling the onion with the chain rule", and can be skipped on a first reading. Differentiating the gradient once more, with θ′(s)=θ(s)(1−θ(s))\theta'(s) = \theta(s)\big(1 - \theta(s)\big) and yn2=1y_n^2 = 1, gives the matrix of second derivatives

∇2Ein(w)=1N∑n=1Nθ(w⊤xn)(1−θ(w⊤xn))xnxn⊤\nabla^2 E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\sum_{n=1}^{N} \theta\big(\mathbf{w}^\top\mathbf{x}_n\big)\Big(1 - \theta\big(\mathbf{w}^\top\mathbf{x}_n\big)\Big)\mathbf{x}_n\mathbf{x}_n^\top

For any vector u\mathbf{u}, u⊤xnxn⊤u=(u⊤xn)2≥0\mathbf{u}^\top\mathbf{x}_n\mathbf{x}_n^\top\mathbf{u} = (\mathbf{u}^\top\mathbf{x}_n)^2 \ge 0, and the coefficient θ(1−θ)\theta(1 - \theta) in front is positive, so u⊤∇2Ein u≥0\mathbf{u}^\top\nabla^2 E_{\text{in}}\,\mathbf{u} \ge 0. This is positive semi-definiteness, which is already enough to guarantee that EinE_{\text{in}} is convex. The "positive definite" mentioned in the lecture needs one more condition: the xn\mathbf{x}_n must span the whole (d+1)(d + 1)-dimensional space, so that the expression above is strictly greater than 0 for every nonzero u\mathbf{u}.

Peeling the onion with the chain rule

EinE_{\text{in}} contains an ln⁡\ln, an exp⁡\exp, and w\mathbf{w}. The lecture's attitude is not to be afraid: differentiation has no secret trick, and doing it one layer at a time and stringing the layers together is the chain rule. Page 14 first gives temporary names to the intermediate quantities:

Ein(w)=1N∑n=1Nln⁡(1+exp⁡(−ynw⊤xn⏞◯)⏟□)E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\sum_{n=1}^{N} \ln\Big(\underbrace{1 + \exp(\overbrace{-y_n\mathbf{w}^\top\mathbf{x}_n}^{\bigcirc})}_{\square}\Big)

The piece inside the exponential is called ◯\bigcirc, and the whole piece inside the ln⁡\ln is called □\square. In the lecture, Lin explains why shapes are used instead of A, B, C: thinking of them as a circle and a square makes it easier to remember that they are only temporary intermediate values.

The ii-th component of the gradient is the partial derivative with respect to wiw_i. The chain rule splits it into three parentheses, which the lecture likens to peeling an onion from the outermost layer inward:

∂Ein(w)∂wi=1N∑n=1N(∂ln⁡(□)∂□)(∂(1+exp⁡(◯))∂◯)(∂ (−ynw⊤xn)∂wi)\frac{\partial E_{\text{in}}(\mathbf{w})}{\partial w_i} = \frac{1}{N}\sum_{n=1}^{N} \Big(\frac{\partial \ln(\square)}{\partial \square}\Big)\Big(\frac{\partial\big(1 + \exp(\bigcirc)\big)}{\partial \bigcirc}\Big)\Big(\frac{\partial\,(-y_n\mathbf{w}^\top\mathbf{x}_n)}{\partial w_i}\Big)

Each of the three parentheses is simple:

  1. The derivative of ln⁡(□)\ln(\square) with respect to □\square is 1□\frac{1}{\square}.
  2. The derivative of 1+exp⁡(◯)1 + \exp(\bigcirc) with respect to ◯\bigcirc: the constant 1 vanishes, and the derivative of exp⁡(◯)\exp(\bigcirc) is still exp⁡(◯)\exp(\bigcirc).
  3. −ynw⊤xn=−yn∑jwjxn,j-y_n\mathbf{w}^\top\mathbf{x}_n = -y_n\sum_j w_j x_{n,j}; only the j=ij = i term of the sum contains wiw_i, so the derivative is −ynxn,i-y_n x_{n,i}. The lecture reminds the audience not to drop the minus sign.

Substituting back:

∂Ein(w)∂wi=1N∑n=1N(1□)(exp⁡(◯))(−ynxn,i)=1N∑n=1N(exp⁡(◯)1+exp⁡(◯))(−ynxn,i)\frac{\partial E_{\text{in}}(\mathbf{w})}{\partial w_i} = \frac{1}{N}\sum_{n=1}^{N} \Big(\frac{1}{\square}\Big)\Big(\exp(\bigcirc)\Big)\Big(-y_n x_{n,i}\Big) = \frac{1}{N}\sum_{n=1}^{N} \Big(\frac{\exp(\bigcirc)}{1 + \exp(\bigcirc)}\Big)\Big(-y_n x_{n,i}\Big)

The second equality merges the first two parentheses and replaces □\square with 1+exp⁡(◯)1 + \exp(\bigcirc). The merged exp⁡(◯)1+exp⁡(◯)\frac{\exp(\bigcirc)}{1 + \exp(\bigcirc)} is exactly the first form of the logistic function, which the lecture calls our new friend θ\theta:

∂Ein(w)∂wi=1N∑n=1Nθ(◯)(−ynxn,i)\frac{\partial E_{\text{in}}(\mathbf{w})}{\partial w_i} = \frac{1}{N}\sum_{n=1}^{N} \theta(\bigcirc)\big(-y_n x_{n,i}\big)

The θ(◯)\theta(\bigcirc) is the same for every component; only the final xn,ix_{n,i} changes with ii. Stacking the results for i=0,1,…,di = 0, 1, \dots, d into a vector stacks the xn,ix_{n,i} into xn\mathbf{x}_n:

∇Ein(w)=1N∑n=1Nθ(−ynw⊤xn)(−ynxn)\nabla E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\sum_{n=1}^{N} \theta\big(-y_n\mathbf{w}^\top\mathbf{x}_n\big)\big(-y_n\mathbf{x}_n\big)

How to read it: each example contributes a vector −ynxn-y_n\mathbf{x}_n, multiplied by a weight between 0 and 1, θ(−ynw⊤xn)\theta(-y_n\mathbf{w}^\top\mathbf{x}_n), and everything is averaged. Page 15 of the handout calls it a "scaled θ\theta-weighted sum of −ynxn-y_n\mathbf{x}_n".

An addition in this article, on what the weight means. By symmetry, θ(−ynw⊤xn)=1−θ(ynw⊤xn)=1−h(ynxn)\theta(-y_n\mathbf{w}^\top\mathbf{x}_n) = 1 - \theta(y_n\mathbf{w}^\top\mathbf{x}_n) = 1 - h(y_n\mathbf{x}_n), and h(ynxn)h(y_n\mathbf{x}_n) is the probability the current hypothesis assigns to "this example appearing with its actual label". So the weight is the probability the hypothesis assigns to this label not appearing: the more surprised the hypothesis is, the louder this example speaks in the gradient.

Setting the gradient to zero cannot be solved

Now solve ∇Ein(w)=0\nabla E_{\text{in}}(\mathbf{w}) = \mathbf{0} (page 15). When is a weighted sum the zero vector? The handout distinguishes two cases.

Case 1: all the weights θ(⋅)\theta(\cdot) are 0. θ\theta approaches 0 only when its argument goes to −∞-\infty, so every example needs −ynw⊤xn→−∞-y_n\mathbf{w}^\top\mathbf{x}_n \to -\infty. The handout writes this as "only if ynw⊤xn≫0y_n\mathbf{w}^\top\mathbf{x}_n \gg 0", and the lecture adds that strictly speaking it has to go to positive infinity. ynw⊤xn>0y_n\mathbf{w}^\top\mathbf{x}_n > 0 means the label and the score have the same sign, so this w\mathbf{w} classifies example nn correctly; for that to hold for every example, the data must be linearly separable. The lecture puts it the other way round: if the data is not linearly separable, then for any w\mathbf{w} at least one example has ynw⊤xny_n\mathbf{w}^\top\mathbf{x}_n below 0, and this route is closed.

Case 2: the weights are not all 0, but the weighted sum happens to cancel out. What has to be solved is then an equation in w\mathbf{w}, and w\mathbf{w} appears inside θ\theta, so it is a non-linear equation of w\mathbf{w}.

Compare with linear regression: there the gradient is 2N(X⊤Xw−X⊤y)\frac{2}{N}(X^\top X\mathbf{w} - X^\top\mathbf{y}), and setting it to zero gives a linear equation of the form "a matrix times w\mathbf{w} equals some vector", which we know how to solve, so the answer comes in a single step. The equation here is not linear. The handout concludes "closed-form solution? no :-(". In the lecture, Lin says that, as far as Lin knows, there is no closed-form solution as neat as linear regression's.

Looking back at PLA: iterative optimization

If the answer cannot be reached in one step, go back and look for a method that corrects itself step by step, and that is PLA. Page 16 reviews PLA: start from some w0\mathbf{w}_0 (say, the zero vector), and for t=0,1,…t = 0, 1, \dots:

  1. Find a mistake of wt\mathbf{w}_t, called (xn(t),yn(t))(\mathbf{x}_{n(t)}, y_{n(t)}), that is, sign(wt⊤xn(t))≠yn(t)\text{sign}(\mathbf{w}_t^\top\mathbf{x}_{n(t)}) \ne y_{n(t)}.
  2. Correct it: wt+1←wt+yn(t)xn(t)\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t + y_{n(t)}\mathbf{x}_{n(t)}.

When it stops, return the last w\mathbf{w}. The same page merges the two steps into one: pick some nn, and then

wt+1←wt+[ ⁣[sign(wt⊤xn)≠yn] ⁣] ynxn\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t + \big[\!\big[\text{sign}(\mathbf{w}_t^\top\mathbf{x}_n) \ne y_n\big]\!\big]\, y_n\mathbf{x}_n

The double square brackets are a boolean operation: 1 if the condition holds, 0 if it does not. If the chosen point is misclassified, the factor is 1 and ynxny_n\mathbf{x}_n is added as usual; if the chosen point is classified correctly, the factor is 0 and nothing is updated. The lecture asks the audience to confirm for themselves that this is the same as the original two steps.

Now split this expression into two parts:

wt+1←wt+1⏟η⋅([ ⁣[sign(wt⊤xn)≠yn] ⁣]⋅ynxn)⏟v\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t + \underbrace{1}_{\eta} \cdot \underbrace{\Big(\big[\!\big[\text{sign}(\mathbf{w}_t^\top\mathbf{x}_n) \ne y_n\big]\!\big] \cdot y_n\mathbf{x}_n\Big)}_{\mathbf{v}}

v\mathbf{v} is the direction of the update, and η\eta is how large a step to take in that direction. In the lecture, Lin says η\eta is a constant quietly slipped in here: PLA does not single out the step size, which amounts to always stepping 1, and its importance only shows up in the gradient descent section.

The handout's conclusion is "choice of (η,v)(\eta, \mathbf{v}) and stopping condition defines iterative optimization approach". Different directions, different step sizes, and different stopping conditions give different iterative optimization methods. PLA is one of them, and gradient descent will be another.

Fun Time: who has the largest weight in the gradient

Question (page 17): Consider the gradient ∇Ein(w)=1N∑n=1Nθ(−ynw⊤xn)(−ynxn)\nabla E_{\text{in}}(\mathbf{w}) = \frac{1}{N}\sum_{n=1}^{N} \theta(-y_n\mathbf{w}^\top\mathbf{x}_n)(-y_n\mathbf{x}_n). That is, each example (xn,yn)(\mathbf{x}_n, y_n) contributes to the gradient by an amount of θ(−ynw⊤xn)\theta(-y_n\mathbf{w}^\top\mathbf{x}_n). For any given w\mathbf{w}, which example contributes the most amount to the gradient?

  1. the example with the smallest ynw⊤xny_n\mathbf{w}^\top\mathbf{x}_n value
  2. the example with the largest ynw⊤xny_n\mathbf{w}^\top\mathbf{x}_n value
  3. the example with the smallest w⊤xn\mathbf{w}^\top\mathbf{x}_n value
  4. the example with the largest w⊤xn\mathbf{w}^\top\mathbf{x}_n value

Answer: 1. The handout's reason is that θ\theta is monotonic. Spelled out: θ\theta is increasing, so the largest θ(−ynw⊤xn)\theta(-y_n\mathbf{w}^\top\mathbf{x}_n) is the same as the largest −ynw⊤xn-y_n\mathbf{w}^\top\mathbf{x}_n, that is, the smallest ynw⊤xny_n\mathbf{w}^\top\mathbf{x}_n.

Choice 2 is the example with the smallest weight, the one classified most correctly and most confidently. Choices 3 and 4 look only at the score and not at the label: a point with a very low score whose label is −1-1 has a large positive ynw⊤xny_n\mathbf{w}^\top\mathbf{x}_n and a small weight, so the score alone cannot decide.

The lecture's reading is this: the example with the smallest ynw⊤xny_n\mathbf{w}^\top\mathbf{x}_n may have a very negative value, and negative means w\mathbf{w} makes a mistake on this example, so the mistaken points get especially large weights. This resembles PLA's "pick a mistaken point and correct it": PLA looks at one mistake, while the gradient combines every point, whether its mistake is big or small.

An addition in this article: the question asks about the weight θ\theta. The vector actually added into the gradient is θ(⋅) (−ynxn)\theta(\cdot)\,(-y_n\mathbf{x}_n), whose length is further multiplied by ∥xn∥\lVert\mathbf{x}_n\rVert, so the point with the largest weight is not necessarily the point with the longest vector.

Gradient descent: which way each step goes and how far

YouTube:Logistic Regression :: Gradient Descent @ Machine Learning Foundations (機器學習基石)

(Video 00:00–03:45 covers rolling the ball downhill and the greedy choice, 03:45–09:26 the linear approximation and the best direction, 09:26–14:47 the choice of η\eta and the fixed learning rate, 14:47–16:48 the full algorithm, 16:48–17:54 Fun Time, and the summary follows.)

The greedy choice: the largest drop in one step

The skeleton of iterative optimization is (page 18): for t=0,1,…t = 0, 1, \dots, do wt+1←wt+ηv\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t + \eta\mathbf{v}, and when it stops, return the last w\mathbf{w}. PLA's v\mathbf{v} comes from mistake correction. The EinE_{\text{in}} of logistic regression is a smooth valley, and the handout's idea is to "choose v\mathbf{v} to get the ball roll 'downhill'": in the lecture, Lin asks the audience to imagine placing a ball somewhere on the slope (corresponding to some w\mathbf{w}) and letting it roll; where it reaches the bottom of the valley is where the gradient is zero.

To make the derivation easier, start with two assumptions:

  • The direction v\mathbf{v} has unit length, ∥v∥=1\lVert\mathbf{v}\rVert = 1. In the lecture, Lin explains that all the information about how big a step to take goes into η\eta, and v\mathbf{v} handles only the direction.
  • The step size η\eta is positive.

For some given η>0\eta > 0, the greedy approach picks the direction that makes EinE_{\text{in}} drop the most in this step:

min⁡∥v∥=1 Ein(wt+ηv⏟wt+1)\min_{\lVert\mathbf{v}\rVert = 1}\ E_{\text{in}}(\underbrace{\mathbf{w}_t + \eta\mathbf{v}}_{\mathbf{w}_{t+1}})

The picture given in the lecture: think of η\eta as a limit of the human body, a stride of at most 30 centimeters; you have to choose a place to put your foot on the circle of radius 30 centimeters centered on yourself. In practice this circle may live in a space of many dimensions.

Linear approximation: look only at a short segment

Page 19 points out that this problem is no simpler: it is "still non-linear optimization, now with constraints", the constraint being ∥v∥=1\lVert\mathbf{v}\rVert = 1, and "not any easier than min⁡wEin(w)\min_{\mathbf{w}} E_{\text{in}}(\mathbf{w})".

The way out is to make it linear. In the lecture, Lin explains that the easy problems we have learned all involve linear expressions, and that any winding curve, looked at over a very short stretch, is no different from a short line segment. Mathematically this is the Taylor expansion. The one-dimensional version is f(x+δ)≈f(x)+f′(x) δf(x + \delta) \approx f(x) + f'(x)\,\delta; the multi-dimensional version replaces the slope with the gradient and the displacement δ\delta with ηv\eta\mathbf{v}:

Ein(wt+ηv)≈Ein(wt)+η v⊤∇Ein(wt)E_{\text{in}}(\mathbf{w}_t + \eta\mathbf{v}) \approx E_{\text{in}}(\mathbf{w}_t) + \eta\,\mathbf{v}^\top\nabla E_{\text{in}}(\mathbf{w}_t)

The handout notes the condition: "if η\eta really small". The lecture's explanation: when the curve bends a lot, a long segment ends up far from the curve; the segment has to be short for the approximation to be good.

Replacing the original expression with the approximation gives the "approximate greedy approach":

min⁡∥v∥=1 Ein(wt)⏟known+η⏟given positive v⊤∇Ein(wt)⏟known\min_{\lVert\mathbf{v}\rVert = 1}\ \underbrace{E_{\text{in}}(\mathbf{w}_t)}_{\text{known}} + \underbrace{\eta}_{\text{given positive}}\ \mathbf{v}^\top\underbrace{\nabla E_{\text{in}}(\mathbf{w}_t)}_{\text{known}}

Ein(wt)E_{\text{in}}(\mathbf{w}_t) is a constant that does not depend on v\mathbf{v}; η\eta is already given and positive, so it does not change which choice is smallest. The slide draws these two terms in gray. All that really has to be solved is:

min⁡∥v∥=1 v⊤∇Ein(wt)\min_{\lVert\mathbf{v}\rVert = 1}\ \mathbf{v}^\top\nabla E_{\text{in}}(\mathbf{w}_t)

The best direction is opposite to the gradient

When is the inner product of a unit vector with a known vector smallest? The lecture's analogy is two people who have quarrelled and keep as far apart as they can: the inner product is most negative when the directions are exactly opposite.

This article writes the step out in full. Let g=∇Ein(wt)\mathbf{g} = \nabla E_{\text{in}}(\mathbf{w}_t) and let ϕ\phi be the angle between the two vectors; then v⊤g=∥v∥ ∥g∥cos⁡ϕ=∥g∥cos⁡ϕ\mathbf{v}^\top\mathbf{g} = \lVert\mathbf{v}\rVert\,\lVert\mathbf{g}\rVert\cos\phi = \lVert\mathbf{g}\rVert\cos\phi. The smallest value of cos⁡ϕ\cos\phi is −1-1, reached at ϕ=180∘\phi = 180^\circ, that is, when v\mathbf{v} points in the direction of −g-\mathbf{g}. Then scale the length to 1 (page 20):

v=−∇Ein(wt)∥∇Ein(wt)∥\mathbf{v} = -\frac{\nabla E_{\text{in}}(\mathbf{w}_t)}{\lVert\nabla E_{\text{in}}(\mathbf{w}_t)\rVert}

Now v⊤g=−∥g∥\mathbf{v}^\top\mathbf{g} = -\lVert\mathbf{g}\rVert, and the approximation says EinE_{\text{in}} drops by about η∥g∥\eta\lVert\mathbf{g}\rVert. Substituting back into the update gives gradient descent: for small η\eta,

wt+1←wt−η ∇Ein(wt)∥∇Ein(wt)∥\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t - \eta\,\frac{\nabla E_{\text{in}}(\mathbf{w}_t)}{\lVert\nabla E_{\text{in}}(\mathbf{w}_t)\rVert}

The handout's comment on it is "a simple & popular optimization tool". The lecture adds that it can be used whenever the gradient can be computed.

η too small, too large, just right

With the direction settled, next comes η\eta. Page 21 compares three figures, all of the same valley:

FigureWhat it showsThe handout's comment
too smallmany tiny stairs drawn down the left slope, descending slowly one stair at a timetoo slow :-(
too largea zigzag line jumping back and forth between the left and right slopestoo unstable :-(
just rightlarge steps where the slope is steep high up (labeled large η\eta) and small steps near the bottom (labeled small η\eta)use changing η\eta

The lecture describes the three cases as three ways of walking. Someone with a very small η\eta is conservative, crawling like a snail; they will get there eventually, only slowly. Someone with a very large η\eta believes they are walking on a short line segment, but with a big stride the linear approximation is no longer accurate, and they miss their footing and fall onto the opposite slope. Nothing guarantees that the landing point is lower; it might even be higher. Because the Taylor expansion no longer applies, nobody knows what happens after a very large step.

The idea of the third figure is that η\eta should change with the slope. The lecture's reasoning: where the slope is steep it is safe to stride a bit farther; where the slope is already gentle, a very large stride may carry you past the optimum in one go. This is a heuristic that feels reasonable, and the handout writes it as

η better be monotonic of ∥∇Ein(wt)∥\eta \text{ better be monotonic of } \lVert\nabla E_{\text{in}}(\mathbf{w}_t)\rVert

The slope is the length of the gradient, and the step size should go up and down with it.

Fixed learning rate: cancelling the two lengths

Page 22 gives the simplest way to do this: make the step size proportional to the length of the gradient. The slide uses color to tell two η\eta's apart: the original step size is red and the ratio is purple. Here the red one is written ηred\eta_{\text{red}} and the purple one η\eta (the subscript is this article's addition):

ηred=η⋅∥∇Ein(wt)∥\eta_{\text{red}} = \eta \cdot \lVert\nabla E_{\text{in}}(\mathbf{w}_t)\rVert

Substituting into the gradient descent update, the gradient lengths in the numerator and denominator cancel:

wt+1←wt−ηred ∇Ein(wt)∥∇Ein(wt)∥=wt−η ∇Ein(wt)\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t - \eta_{\text{red}}\,\frac{\nabla E_{\text{in}}(\mathbf{w}_t)}{\lVert\nabla E_{\text{in}}(\mathbf{w}_t)\rVert} = \mathbf{w}_t - \eta\,\nabla E_{\text{in}}(\mathbf{w}_t)

In the lecture, Lin says the ratio is named purple because it mixes the red η\eta and the blue gradient. The result is a simpler update: there is no need to normalize the gradient, just multiply it by a fixed η\eta. This η\eta is called the fixed learning rate, and the method is fixed learning rate gradient descent:

wt+1←wt−η ∇Ein(wt)\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t - \eta\,\nabla E_{\text{in}}(\mathbf{w}_t)

What is "fixed" is the ratio η\eta. The length actually stepped is η∥∇Ein(wt)∥\eta\lVert\nabla E_{\text{in}}(\mathbf{w}_t)\rVert, which still changes: a steep slope means a long step, and near the bottom of the valley the gradient approaches zero and the step shrinks automatically. On the name, the lecture explains that "learning rate" was coined by machine learning people, and optimization people do not necessarily call it that; a small η\eta learns slowly, a somewhat larger η\eta learns faster, and one that is too large is unstable.

The logistic regression algorithm

Page 23 puts everything together:

  1. Initialize w0\mathbf{w}_0.
  2. For t=0,1,…t = 0, 1, \dots:
    1. Compute the gradient ∇Ein(wt)=1N∑n=1Nθ(−ynwt⊤xn)(−ynxn)\nabla E_{\text{in}}(\mathbf{w}_t) = \frac{1}{N}\sum_{n=1}^{N} \theta\big(-y_n\mathbf{w}_t^\top\mathbf{x}_n\big)\big(-y_n\mathbf{x}_n\big).
    2. Update by wt+1←wt−η ∇Ein(wt)\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t - \eta\,\nabla E_{\text{in}}(\mathbf{w}_t).
  3. Until ∇Ein(wt+1)=0\nabla E_{\text{in}}(\mathbf{w}_{t+1}) = \mathbf{0}, or enough iterations.
  4. Return the last wt+1\mathbf{w}_{t+1} as gg.

For the stopping condition, the handout writes that the gradient equals zero. The lecture is more practical: with floating-point arithmetic an exact 0 is hard to get, so usually the gradient is only required to be close enough to zero (roughly at the bottom is good enough); another common condition is to stop after enough steps and pretend to have reached a reasonably good place.

The last line on the slide is "similar time complexity to pocket per iteration". The lecture explains: the most expensive part of each iteration is computing the gradient, which averages the contributions of all NN examples; pocket also uses all NN examples in each iteration to check whether the new w\mathbf{w} has a good enough EinE_{\text{in}}, so the two cost about the same.

Numerical example: five patients, two steps by hand, then run to the bottom

What follows is an example made up for this article, not from the slides; the numbers were verified with Python. It keeps the heart attack setting but only one feature: x1x_1 is the standardized score of some lab test, and y=+1y = +1 means the patient later had a heart attack. With x0=1x_0 = 1 added, x=(1,x1)\mathbf{x} = (1, x_1) and w=(w0,w1)\mathbf{w} = (w_0, w_1).

nnx1x_1yny_n−ynxn-y_n\mathbf{x}_n
1−2-2−1-1(1,−2)(1, -2)
2−1-1−1-1(1,−1)(1, -1)
300+1+1(−1,0)(-1, 0)
411−1-1(1,1)(1, 1)
522+1+1(−1,−2)(-1, -2)

People with a high test value have heart attacks more often, but patient 3 (a middling value, yet a heart attack) and patient 4 (a fairly high value, yet fine) break the ordering. No threshold classifies all five correctly; the data is not linearly separable.

Step 0: w0=(0,0)\mathbf{w}_0 = (0, 0), η=0.1\eta = 0.1. Every example has score 0 and weight θ(0)=0.5\theta(0) = 0.5, and Ein(w0)=ln⁡2≈0.6931E_{\text{in}}(\mathbf{w}_0) = \ln 2 \approx 0.6931. The gradient is

∇Ein(w0)=15⋅0.5⋅[(1,−2)+(1,−1)+(−1,0)+(1,1)+(−1,−2)]=0.55(1,−4)=(0.1,−0.4)\nabla E_{\text{in}}(\mathbf{w}_0) = \frac{1}{5}\cdot 0.5\cdot\Big[(1,-2) + (1,-1) + (-1,0) + (1,1) + (-1,-2)\Big] = \frac{0.5}{5}(1, -4) = (0.1, -0.4)

Update:

w1=(0,0)−0.1×(0.1,−0.4)=(−0.01,0.04)\mathbf{w}_1 = (0, 0) - 0.1 \times (0.1, -0.4) = (-0.01, 0.04)

w1w_1 becomes positive, meaning a higher test value means higher risk; the direction is right.

Step 1: compute the gradient at w1=(−0.01,0.04)\mathbf{w}_1 = (-0.01, 0.04). The five weights are now different:

nnScore w1⊤xn\mathbf{w}_1^\top\mathbf{x}_nynw1⊤xny_n\mathbf{w}_1^\top\mathbf{x}_nWeight θ(−ynw1⊤xn)\theta(-y_n\mathbf{w}_1^\top\mathbf{x}_n)
1−0.09-0.090.090.090.4775
2−0.05-0.050.050.050.4875
3−0.01-0.01−0.01-0.010.5025
40.030.03−0.03-0.030.5075
50.070.070.070.070.4825

Examples 3 and 4 have negative ynw⊤xny_n\mathbf{w}^\top\mathbf{x}_n: they are misclassified, and their weights exceed 0.5. Example 4 has the smallest value and the largest weight, consistent with the conclusion of "Fun Time: who has the largest weight in the gradient". Multiply each weight by its −ynxn-y_n\mathbf{x}_n and average:

first component=15(0.4775+0.4875−0.5025+0.5075−0.4825)=0.48755=0.0975second component=15(−2(0.4775)−0.4875+0+0.5075−2(0.4825))=−1.90005=−0.3800\begin{aligned} \text{first component} &= \tfrac{1}{5}\big(0.4775 + 0.4875 - 0.5025 + 0.5075 - 0.4825\big) = \tfrac{0.4875}{5} = 0.0975 \\ \text{second component} &= \tfrac{1}{5}\big(-2(0.4775) - 0.4875 + 0 + 0.5075 - 2(0.4825)\big) = \tfrac{-1.9000}{5} = -0.3800 \end{aligned}

Update:

w2=(−0.01,0.04)−0.1×(0.0975,−0.3800)=(−0.0198,0.0780)\mathbf{w}_2 = (-0.01, 0.04) - 0.1 \times (0.0975, -0.3800) = (-0.0198, 0.0780)

EinE_{\text{in}} at the three points in time is 0.6931, 0.6766, and 0.6615, dropping at every step.

Running to the bottom. Continuing with η=0.1\eta = 0.1, the gradient length falls below 10−310^{-3} at step 344, and the run finally converges to

w≈(−0.6227,1.0904),Ein≈0.4844\mathbf{w} \approx (-0.6227, 1.0904),\qquad E_{\text{in}} \approx 0.4844
nnx1x_1yny_nh(xn)=θ(w⊤xn)h(\mathbf{x}_n) = \theta(\mathbf{w}^\top\mathbf{x}_n)Weight θ(−ynw⊤xn)\theta(-y_n\mathbf{w}^\top\mathbf{x}_n)
1−2-2−1-10.0570.057
2−1-1−1-10.1530.153
300+1+10.3490.651
411−1-10.6150.615
522+1+10.8260.174

What this result means.

  • The model reports risk: about 6% for a test value of −2-2 and about 83% for +2+2. Thresholding at one half, as in "Fun Time: converting a probability back to circle or cross", puts the boundary at x1=0.6227/1.0904≈0.571x_1 = 0.6227 / 1.0904 \approx 0.571; examples 3 and 4 are still misclassified, and the 0/1 error is 25\frac{2}{5}. The risks of these two are estimated as 0.349 and 0.615, both not far from one half; the model was never confident about them.
  • The likelihood rises from 0.55=0.031250.5^5 = 0.03125 at w0\mathbf{w}_0 to e−5×0.4844≈0.0887e^{-5 \times 0.4844} \approx 0.0887.
  • The gradient is zero by way of Case 2 from the subsection "Setting the gradient to zero cannot be solved". None of the weights is 0; the weighted sum cancels: the first component is 0.057+0.153−0.651+0.615−0.174=00.057 + 0.153 - 0.651 + 0.615 - 0.174 = 0, and the second is −2(0.057)−0.153+0.615−2(0.174)≈0-2(0.057) - 0.153 + 0.615 - 2(0.174) \approx 0. The two misclassified examples have the largest weights and pull the three correctly classified ones to a draw.

Trying different η\eta. Same data, same start at w0=(0,0)\mathbf{w}_0 = (0, 0), changing only the fixed learning rate:

η\etaEinE_{\text{in}} after step 1Steps until the gradient length falls below 10−310^{-3}What happens
0.010.69153,460each step drops only a tiny bit, the handout's too slow
0.10.6766344steady descent
10.563432steady descent, ten times faster
30.49558the fastest of the values in this table (trying others, η=4\eta = 4 needs only 5 steps, η=7\eta = 7 slows to 21 steps, and η=8\eta = 8 never gets there)
100.8739neverhigher than the starting 0.6931 after step 1; afterwards it gradually settles into bouncing between the two points (−0.038,2.153)(-0.038, 2.153) and (−1.003,0.676)(-1.003, 0.676), where EinE_{\text{in}} is 0.6148 and 0.5308, and the gradient length stays at 0.176
1008.0000neverEinE_{\text{in}} cycles every three steps, jumping among roughly 4, 14, and 6; it is 13.87 at step 1,000 and 5.84 at step 5,000

The η=10\eta = 10 row corresponds to the middle figure on page 21: one step crosses the bottom of the valley and lands on the opposite slope. Here the landing point of step 1 is even higher than the starting point, which is the situation the lecture warns about: after crossing to the opposite slope, you end up higher. How large an η\eta counts as too large depends on the data, and the numbers in this table do not carry over to other problems.

One more experiment, with the normalized version, that is, the update on page 20 that moves a fixed length η\eta at every step, with η=0.05\eta = 0.05. It gets near the bottom of the valley within 30 steps, but after that every step still has to cover the full length of 0.05, so it can only stride back and forth between two points on either side of the bottom: even steps land at (−0.631,1.076)(-0.631, 1.076), with gradient length 0.0044; odd steps land at (−0.607,1.120)(-0.607, 1.120), with gradient length 0.0087. All the way to step 5,000 these two points keep alternating, and the gradient length stays above 0.004 and never falls to 10−310^{-3}. This shows why page 21 asks for a step size that changes with the gradient length; in the fixed learning rate version the step shrinks by itself near the bottom, and the problem does not arise.

Fun Time: the first step from the zero vector

Question (page 24): If w0=0\mathbf{w}_0 = \mathbf{0}, and take η=0.1\eta = 0.1. What is w1\mathbf{w}_1 in the logistic regression algorithm?

  1. +0.1⋅1N∑n=1Nynxn+0.1 \cdot \frac{1}{N}\sum_{n=1}^{N} y_n\mathbf{x}_n
  2. −0.1⋅1N∑n=1Nynxn-0.1 \cdot \frac{1}{N}\sum_{n=1}^{N} y_n\mathbf{x}_n
  3. +0.05⋅1N∑n=1Nynxn+0.05 \cdot \frac{1}{N}\sum_{n=1}^{N} y_n\mathbf{x}_n
  4. −0.05⋅1N∑n=1Nynxn-0.05 \cdot \frac{1}{N}\sum_{n=1}^{N} y_n\mathbf{x}_n

Answer: 3. The handout only says to substitute using θ(0)=12\theta(0) = \frac{1}{2}. The full substitution: with w0=0\mathbf{w}_0 = \mathbf{0}, every example has −ynw0⊤xn=0-y_n\mathbf{w}_0^\top\mathbf{x}_n = 0, so every weight is 12\frac{1}{2},

∇Ein(w0)=1N∑n=1N12(−ynxn)=−12⋅1N∑n=1Nynxn\nabla E_{\text{in}}(\mathbf{w}_0) = \frac{1}{N}\sum_{n=1}^{N} \frac{1}{2}\big(-y_n\mathbf{x}_n\big) = -\frac{1}{2}\cdot\frac{1}{N}\sum_{n=1}^{N} y_n\mathbf{x}_n w1=0−0.1×(−12⋅1N∑n=1Nynxn)=+0.05⋅1N∑n=1Nynxn\mathbf{w}_1 = \mathbf{0} - 0.1 \times \Big(-\frac{1}{2}\cdot\frac{1}{N}\sum_{n=1}^{N} y_n\mathbf{x}_n\Big) = +0.05 \cdot \frac{1}{N}\sum_{n=1}^{N} y_n\mathbf{x}_n

Choice 1 leaves out θ(0)=12\theta(0) = \frac{1}{2}. Choices 2 and 4 have the wrong sign: the gradient contains a minus sign and the update subtracts the gradient, and two minuses make a plus. The five-patient example can be used to check the answer: 15∑nynxn=(−0.2,0.8)\frac{1}{5}\sum_n y_n\mathbf{x}_n = (-0.2, 0.8), and multiplying by 0.05 gives (−0.01,0.04)(-0.01, 0.04), which is the w1\mathbf{w}_1 computed earlier.

The handout's comment on this result is that a scaled average of ynxny_n\mathbf{x}_n is somewhat 'one-step' better than the zero vector. In the lecture, Lin explains that averaging the products of yy and x\mathbf{x} is a fairly good direction, a little better than the zero vector, because it has already rolled downhill once.

This lecture in four sentences

The summary on page 25, one sentence per section:

  • Problem: P(+1∣x)P(+1 \mid \mathbf{x}) as target and θ(w⊤x)\theta(\mathbf{w}^\top\mathbf{x}) as hypotheses.
  • Error: cross-entropy (negative log likelihood).
  • Gradient: θ\theta-weighted sum of data vectors.
  • Gradient descent: roll downhill by −∇Ein(w)-\nabla E_{\text{in}}(\mathbf{w}).

The preview of the next lecture is "linear model'S' for classification". In the lecture, Lin says that linear classification, linear regression, and logistic regression are all linear methods, and the next lecture will bring them together and apply them to more complicated problems.

Self-check

1. An example has x=(1,3)\mathbf{x} = (1, 3) and y=+1y = +1, and currently w=(1,−2)\mathbf{w} = (1, -2). Compute h(x)h(\mathbf{x}), this example's cross-entropy error, and the vector it contributes to the gradient sum, θ(−yw⊤x)(−yx)\theta(-y\mathbf{w}^\top\mathbf{x})(-y\mathbf{x}). In which direction does this example push w\mathbf{w}?

Answer: the score is s=1−6=−5s = 1 - 6 = -5 and h(x)=θ(−5)≈0.0067h(\mathbf{x}) = \theta(-5) \approx 0.0067; the hypothesis considers +1+1 almost impossible, yet the label is +1+1. The error is ln⁡(1+e5)≈5.0067\ln(1 + e^{5}) \approx 5.0067. The weight is θ(5)≈0.9933\theta(5) \approx 0.9933, and the contributed vector is 0.9933×(−1,−3)≈(−0.993,−2.980)0.9933 \times (-1, -3) \approx (-0.993, -2.980). The update subtracts the gradient, so this example pushes w\mathbf{w} in the direction of (+1,+3)(+1, +3), that is, of yxy\mathbf{x}, raising its own score.

2. A hypothesis outputs h(xn)=0.8h(\mathbf{x}_n) = 0.8 on all three examples, whose labels are circle, circle, cross. Ignoring P(xn)P(\mathbf{x}_n), what is the likelihood? What is EinE_{\text{in}}? What is the score w⊤xn\mathbf{w}^\top\mathbf{x}_n of these three examples?

Answer: the likelihood is 0.8×0.8×0.2=0.1280.8 \times 0.8 \times 0.2 = 0.128. Ein=−13ln⁡(0.128)≈0.6852E_{\text{in}} = -\frac{1}{3}\ln(0.128) \approx 0.6852; computed term by term it is 13(0.2231+0.2231+1.6094)\frac{1}{3}(0.2231 + 0.2231 + 1.6094), the same result. Solving θ(s)=0.8\theta(s) = 0.8 gives e−s=0.25e^{-s} = 0.25 and s=ln⁡4≈1.386s = \ln 4 \approx 1.386. Criterion: the two ways of computing EinE_{\text{in}} must agree, which is what "EinE_{\text{in}} is the negative log likelihood divided by NN" means.

3. In the five-patient example, switch to η=1\eta = 1 and take one step from w0=(0,0)\mathbf{w}_0 = (0, 0). What is w1\mathbf{w}_1? Can it be obtained directly from the η=0.1\eta = 0.1 result without recomputing the gradient?

Answer: yes. The gradient at the first step does not depend on η\eta and is (0.1,−0.4)(0.1, -0.4) either way, so w1=−1×(0.1,−0.4)=(−0.1,0.4)\mathbf{w}_1 = -1 \times (0.1, -0.4) = (-0.1, 0.4), ten times the value for η=0.1\eta = 0.1. Here Ein≈0.5634E_{\text{in}} \approx 0.5634. From the second step on this conversion no longer works, because w1\mathbf{w}_1 differs along the two paths and so does the gradient.

4. Two one-dimensional examples (with x0=1x_0 = 1 added): x1=−1x_1 = -1 labeled −1-1, and x1=+1x_1 = +1 labeled +1+1. Take w=(0,c)\mathbf{w} = (0, c) with c>0c > 0. What function of cc is EinE_{\text{in}}? Where does gradient descent stop? What does this say about the stopping condition?

Answer: both examples have ynw⊤xn=cy_n\mathbf{w}^\top\mathbf{x}_n = c, so Ein=ln⁡(1+e−c)E_{\text{in}} = \ln(1 + e^{-c}): 0.3133 at c=1c = 1, 0.0067 at c=5c = 5, and about 4.5×10−54.5 \times 10^{-5} at c=10c = 10. It keeps decreasing with cc and approaches 0 without reaching it; both weights are θ(−c)\theta(-c), always positive. When the data is linearly separable, a zero gradient occurs only in the limit where the length of w\mathbf{w} goes to infinity, which is the "all θ\theta are 0" case. So the algorithm never stops because the gradient is exactly zero; it relies on the two conditions "gradient small enough" or "enough iterations".

5. Explain in your own words: (a) why the best unit direction is −∇Ein/∥∇Ein∥-\nabla E_{\text{in}} / \lVert\nabla E_{\text{in}}\rVert; (b) how long each step actually is in the fixed learning rate version; (c) what η\eta and v\mathbf{v} are when PLA is put into the framework wt+1←wt+ηv\mathbf{w}_{t+1} \leftarrow \mathbf{w}_t + \eta\mathbf{v}.

Criteria: (a) should mention that for very small η\eta the Taylor expansion approximates Ein(wt+ηv)E_{\text{in}}(\mathbf{w}_t + \eta\mathbf{v}) by Ein(wt)+ηv⊤∇Ein(wt)E_{\text{in}}(\mathbf{w}_t) + \eta\mathbf{v}^\top\nabla E_{\text{in}}(\mathbf{w}_t), that only the inner product term depends on v\mathbf{v}, and that the inner product is smallest when the unit vector is opposite to the gradient. (b) The length is η∥∇Ein(wt)∥\eta\lVert\nabla E_{\text{in}}(\mathbf{w}_t)\rVert, scaling with the gradient; the answer should point out that what is "fixed" is the ratio. (c) η=1\eta = 1, and v\mathbf{v} is ynxny_n\mathbf{x}_n when the chosen point is misclassified and the zero vector when it is classified correctly; even better if the answer goes on to note that PLA looks at one example per step while gradient descent looks at all NN.

Exercises and corresponding reading

The corresponding week in two semesters. The Fall 2026 course page puts Foundations Lectures 9 and 10 in the same week (W5, 10/07). The corresponding Fall 2024 slides are 10u; this article uses only the MOOC handout and has not compared the two versions page by page.

Textbook. The Fall 2024 course page maps this lecture to Section 3.3 of Learning from Data (LFD).

Homework. None of the problems below has official solutions.

  • Fall 2024 HW3 (released 2024-10-07, due 10-21) Q4: for samples drawn from a uniform distribution on [θ,1][\theta, 1], what is the likelihood of a given estimate. It practices the move "pretend some parameter is the true one and compute how likely it is to generate the data in hand".
  • HW3 Q9: switch to a different sigmoid hypothesis and follow the steps of logistic regression to derive the new EinE_{\text{in}} and gradient. The method is to walk through this article's "Take ln, add a minus sign, divide by N" and "Peeling the onion with the chain rule" once more.
  • Fall 2024 HW4 (released 2024-10-21, due 11-04) Q1: after the labels change from {−1,+1}\{-1, +1\} to {0,1}\{0, 1\}, which expression is equivalent to the cross-entropy from the lecture. The subsection "The shape and the name of cross-entropy" has already derived it once.
  • HW4 Q5 (extension): derive Newton's method from the second-order Taylor expansion and apply it to the cross-entropy of logistic regression; it can be treated as an advanced version of gradient descent.

The guide to the whole assignment is in Foundations Homework.

Hands-on. Write the four steps of "The logistic regression algorithm" as a dozen or so lines of Python. First reproduce this article's numbers on the five-patient data (w1\mathbf{w}_1, w2\mathbf{w}_2, and the converged w\mathbf{w}), then vary η\eta from 0.01 to 100, plot EinE_{\text{in}} against the number of iterations, and compare with the three figures on page 21.

Further reading. Stanford CS229 Lecture 3: Maximum Likelihood and Logistic Regression derives the same model with {0,1}\{0, 1\} labels; Stanford CS109's Maximum Likelihood Estimation and Logistic Regression cover likelihood from the angle of a probability course.

Next lecture. Foundations Lecture 11: Linear Models for Classification compares these three linear models side by side, asks whether linear regression and logistic regression can be used for classification, turns gradient descent into stochastic gradient descent (SGD), and finally handles multiclass classification.

Course video sources

Original videos: Logistic Regression Problem, Logistic Regression Error, Gradient of Logistic Regression Error, Gradient Descent

Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.

Update Log

  • 2026-10-13: The series now has one article per lecture; this lecture is a newly split-out article, written from the handout and the video transcripts.

References