Weekly assignment is collected 2 pm the following Monday.
This week is collected 2 pm on the 16th November.
Logistic regression and Loss functions
Probablistic classification
Recap from the perceptron
In lecture one we used petal length and width to separate setosa from versicolor.
A straight line separates these two groups. The perceptron used the side of the line to choose a label.
Two Iris species with overlapping measurements
Keep the same features. Now label versicolor 0 and virginica 1.
Can you spot a flower that the fitted line puts on the wrong side?
Two Iris species with overlapping measurements
Keep the same features. Now label versicolor 0 and virginica 1.
The rings mark wrong decisions on both sides of the fitted line. We want to make predictions near the boundary and express how strongly the measurements favour each species.
What does the linear score tell us?
\[ z=w_1x_1+w_2x_2+b=w^\top x+b \]
The features are petal length and width. The weights tell us how those measurements contribute to the score, and the bias shifts it.
- A large positive \(z\) will give a high estimated probability of virginica.
- A large negative \(z\) will give a high estimated probability of versicolor.
We need a way to turn a score on the real line into a probability between 0 and 1.
A flower lands on the virginica side of the line. What’s the chance it really is virginica?
What if we take 1,000 flowers but split them 9:1 in favour of versicolor? We would have 900 versicolor and 100 virginica. Use the same rates for each species as on the plot.
| Side of the line | Virginica | Versicolor | Total |
|---|---|---|---|
| Virginica side | 94 | 54 | 148 |
| Versicolor side | 6 | 846 | 852 |
| Total | 100 | 900 | 1000 |
Let \(V\) mean “the flower is virginica” and \(S\) mean “the flower is on the virginica side”.
Start with the virginica flowers. Take the fraction of them on the virginica side.
\[ P(V\cap S)=P(V)\,P(S\mid V)=\frac{100}{1000}\times\frac{94}{100}=\frac{94}{1000} \]
Start with the flowers on the virginica side. Take the fraction of them that are virginica.
\[ P(V\cap S)=P(S)\,P(V\mid S)=\frac{148}{1000}\times\frac{94}{148}=\frac{94}{1000} \]
Bayes’ theorem
\[ P(V)\,P(S\mid V)=P(S)\,P(V\mid S) \]
\[ \Longrightarrow\qquad P(V\mid S)=\frac{P(S\mid V)\,P(V)}{P(S)} \]
\[ P(V\mid S)=\frac{(94/100)\times(100/1000)}{148/1000}=\frac{94}{148}\approx 0.635 \]
In this collection, a flower on the virginica side has about a 64% chance of being virginica.
Should a flower just across the line get the same probability as one deep in virginica territory?
Use petal width and length as evidence
Petal width \(x_w\) and petal length \(x_l\) determine where a flower lands on the plot. The side of the line gives us a region. The measurements give us a point within that region.
Before measuring the flower, we use the species proportions as our priors.
Once we have \((x_w,x_l)\), we update our belief by asking how common measurements near that point are for each species.
Give each flower class a weight of belief
We used \(P(S\mid V)\) when we knew which side the flower was on. With its two measurements, we use the density \(f_{\mathrm{virginica}}(x_w,x_l)\), which is higher where virginica measurements cluster.
\[ a_1=P(\mathrm{flower\ is\ virginica})\,f_{\mathrm{virginica}}(x_w,x_l) \]
\[ a_0=P(\mathrm{flower\ is\ versicolor})\,f_{\mathrm{versicolor}}(x_w,x_l) \]
Each product is an unnormalised weight of belief. We will divide by \(a_1+a_0\) to get probabilities.
Normalise the class weights
The bar \(\mid\) reads ‘given’. What is the probability of each species given this flower’s width and length?
\[ p=P(\mathrm{virginica}\mid x_w,x_l)=\frac{a_1}{a_1+a_0} \]
\[ 1-p=P(\mathrm{versicolor}\mid x_w,x_l)=\frac{a_0}{a_1+a_0} \]
Divide by the total weight of belief. The two probabilities add to 1.
Compare the class probabilities
Take the ratio of the two probabilities. The normalising total cancels.
\[ \frac{p}{1-p}=\frac{a_1}{a_0} \]
This ratio is the odds of class 1. If \(p=0.8\), the odds are \(0.8/0.2=4\), or four to one.
Use log-odds as the score
The probability \(p\) lies between 0 and 1. We want a score that can range from \(-\infty\) to \(+\infty\), so take the natural logarithm of the odds.
\[ z=\ln\!\left(\frac{p}{1-p}\right)=\ln\!\left(\frac{a_1}{a_0}\right) \]
Logistic regression models that score directly as \(w^\top x+b\). The linear form is the model assumption.
Read the two class densities
Take two equally common classes and use petal length alone. The two illustrative normal curves have the same spread. With equal priors, \(p=f_1(x)/(f_0(x)+f_1(x))\).
At 5 cm the curves have equal height, so \(p=0.5\). At 6 cm the class 1 curve is higher, so \(p\) is above 0.5.
Why equal spreads give linear log-odds
The two class priors cancel in the odds. We are left with a ratio of the curve heights.
\[ z=\ln\!\left(\frac{p}{1-p}\right)=\ln\!\left(\frac{f_1(x)}{f_0(x)}\right) \]
The common standard deviation of 1 cm gives both normal densities the same prefactor.
\[ \frac{f_1(x)}{f_0(x)}=\frac{e^{-(x-6)^2/2}}{e^{-(x-4)^2/2}} \]
\[ z=\frac{(x-4)^2-(x-6)^2}{2}=2x-10 \]
The \(x^2\) terms cancel because the spreads match. At 5 cm this gives \(z=0\), corresponding to \(p=0.5\).
Solve for the probability
\[ z=\ln\!\left(\frac{p}{1-p}\right) \]
Exponentiate both sides and clear the denominator.
\[ e^z=\frac{p}{1-p}\qquad\Longrightarrow\qquad e^z(1-p)=p \]
Collect the terms in \(p\).
\[ e^z=p+pe^z=p(1+e^z) \]
Isolate \(p\), then divide the numerator and denominator by \(e^z\).
\[ p=\frac{e^z}{1+e^z}=\frac{1}{1+e^{-z}} \]
The sigmoid function
\[ p=\sigma(z)=\frac{1}{1+e^{-z}} \]
At \(z=0\), \(p=0.5\). Larger positive scores give probabilities closer to 1.
Read the sigmoid
The circled flower has petals 3.7 cm long and 1.0 cm wide.
Its score is \(z\approx-3.11\), giving \(p=\sigma(z)\approx0.043\). The model strongly favours versicolor.
A flower on the virginica side
The circled flower has petals 5.9 cm long and 2.3 cm wide.
Its score is \(z\approx2.92\), giving \(p=\sigma(z)\approx0.949\). The model strongly favours virginica.
A flower on the boundary
Put a hypothetical flower exactly on the fitted line.
The score is \(z=0\), so \(p=\sigma(0)=0.5\). The two species have equal estimated probabilities.
Every point has a probability
Predict virginica when \(p\ge0.5\). The contour \(p=0.5\) is also \(z=0\), the straight decision boundary.
The other contours join points with the same estimated probability. All are parallel because each corresponds to a constant linear score.
Moving the decision threshold
Raise the threshold from 0.5 to 0.8 and the model needs stronger evidence before it assigns label 1.
The probabilities stay fixed. Points between the two lines switch from label 1 to label 0.
Choosing a cutoff for follow-up
Imagine a model that estimates the probability of cancer and flags cases for follow-up. A case is flagged when its estimate meets or exceeds the cutoff.
If we want to miss fewer cancers on the same set of cases, which way should we move the cutoff? What could happen to referrals for people without cancer?
A lower cutoff flags more cases
In this hypothetical set of 1,000 cases, 20 people have cancer. Apply both cutoffs to the same cases.
| Cutoff | Cancers flagged (TP) | Cancers missed (FN) | False referrals (FP) |
|---|---|---|---|
| 0.50 | 14 | 6 | 8 |
| 0.20 | 18 | 2 | 20 |
Lowering the cutoff flags four more of the cancer cases and sends twelve more people without cancer for follow-up.
Learning from loss
Two wrong labels can have different predictions
The perceptron gave a class decision that was either right or wrong. Both flowers here are virginica, so both decisions at the 0.5 cutoff count as mistakes.
The estimate of 0.49 is close to the decision boundary. The estimate of 0.01 strongly favours the wrong class. We want the loss to reflect that difference.
The data tell us the correct class for each flower.
We compare that label with the predicted probability to calculate a loss. A training algorithm uses the loss to work out how to update the weights and bias.
Pick the probability of the observed label
The model reports \(p\), the probability of virginica. Write \(p_{\mathrm{correct}}\) for the probability the model gave to the flower’s correct label.
| Observed label | Probability for the correct label |
|---|---|
| \(y=1\), virginica | \(p_{\mathrm{correct}}=p\) |
| \(y=0\), versicolor | \(p_{\mathrm{correct}}=1-p\) |
When the model gives the correct label very little probability, we want a larger loss. That loss guides how we change the weights.
Loss for one example
\[ \ell=-\ln(p_{\mathrm{correct}}) \]
If the model gives the correct label a probability near 1, the loss is close to zero. As that probability approaches zero, the penalty grows.
Calculate the two losses
Both true labels are 1. The model gives the first flower probability 0.49 and the second flower probability 0.01.
Find \(p_{\mathrm{correct}}\) for each flower, then calculate \(-\ln(p_{\mathrm{correct}})\). How does the loss distinguish these two mistakes?
The confident error costs more
| Prediction | Probability of the true label | Loss |
|---|---|---|
| Marginal error | 0.49 | 0.713 |
| Confident error | 0.01 | 4.605 |
The second prediction assigned very little probability to what happened, so it receives the larger penalty.
Use the same loss for label 0
Suppose \(p=0.9\) and the actual label is \(y=0\). The model assigned only 0.1 probability to the observed label.
\[ p_{\mathrm{correct}}=1-0.9=0.1\qquad\Longrightarrow\qquad \ell=-\ln(0.1)\approx2.303 \]
The calculation always starts by picking the probability of the label that occurred.
One formula for both labels
\[ \ell(y,p)=-y\ln p-(1-y)\ln(1-p) \]
| Observed label | First term | Second term | Loss |
|---|---|---|---|
| 1 | \(-\ln p\) | 0 | \(-\ln p\) |
| 0 | 0 | \(-\ln(1-p)\) | \(-\ln(1-p)\) |
This is binary cross-entropy. The factors \(y\) and \(1-y\) select the term for the observed label.
Substitute the two labels
For a versicolor flower, \(y=0\).
\[ \ell(0,p)=-(0)\ln(p)-(1-(0))\ln(1-p)=-\ln(1-p) \]
For a virginica flower, \(y=1\).
\[ \ell(1,p)=-(1)\ln(p)-(1-(1))\ln(1-p)=-\ln(p) \]
\[ \ell(y,p)=-y\ln(p)-(1-y)\ln(1-p) \]
From one loss to a training cost
Each of the \(N\) training flowers gives its own prediction \(p_i\) and loss \(\ell(y_i,p_i)\). Add the flower losses and divide by \(N\).
\[ J(w,b)=\frac{\ell(y_1,p_1)+\ell(y_2,p_2)+\cdots+\ell(y_N,p_N)}{N} \]
\[ J(w,b)=\frac{1}{N}\sum_{i=1}^{N}\ell(y_i,p_i) \]
This is the mean loss of the training flowers at the current weights and bias.
A training cost by hand
| Example | Label \(y\) | Predicted \(p\) |
|---|---|---|
| 1 | 1 | 0.8 |
| 2 | 0 | 0.3 |
| 3 | 1 | 0.4 |
Pick the probability of the observed label for each row. What is the mean log loss?
Substitute each label and prediction into the binary cross-entropy loss.
\[ \ell_1=-(1)\ln(0.8)-(1-1)\ln(1-0.8)=-\ln(0.8)\approx 0.223 \]
\[ \ell_2=-(0)\ln(0.3)-(1-0)\ln(1-0.3)=-\ln(0.7)\approx 0.357 \]
\[ \ell_3=-(1)\ln(0.4)-(1-1)\ln(1-0.4)=-\ln(0.4)\approx 0.916 \]
Average the three penalties
| Example | Probability for the correct label \(p_{\mathrm{correct}}\) | Loss |
|---|---|---|
| 1 | 0.8 | 0.223 |
| 2 | 0.7 | 0.357 |
| 3 | 0.4 | 0.916 |
\[ J=\frac{0.223+0.357+0.916}{3}\approx 0.499 \]
The third flower has the largest loss.
How can we lower the Iris training cost?
Newton–Raphson method
To solve \(f(x)=0\), follow the tangent at your current estimate to where it crosses the \(x\)-axis.
\[ x_{n+1}=x_n-\frac{f(x_n)}{f^{\prime}(x_n)}=\frac{x_n}{2}\qquad 4\to2\to1\to0.5 \]
How can we lower the Iris training cost?
We train the model by finding weights and a bias that give the flowers a low mean loss \(J\). Every flower uses the same weights, so changing either weight changes many scores at once.
\[ z_i=w_1x_{i1}+w_2x_{i2}+b \]
Here \(x_{i1}\) is flower \(i\)’s petal length and \(x_{i2}\) is its petal width. The two weights control how much these measurements change the score.
Which change to the weights would make the mean loss fall?
Work backwards from the Iris loss
The blue arrows show backpropagation, from the mean loss back to the petal weights. Each arrow uses the slope for one step of the model.
To update a weight or the bias, we need each slope along the chain from \(J\) back to the parameters.
\[ \frac{\partial J}{\partial w_j}=\sum_{i=1}^{N}\frac{\partial J}{\partial\ell_i}\,\frac{\partial\ell_i}{\partial p_i}\,\frac{\mathrm{d}p_i}{\mathrm{d}z_i}\,\frac{\partial z_i}{\partial w_j} \]
\[ \frac{\partial J}{\partial b}=\sum_{i=1}^{N}\frac{\partial J}{\partial\ell_i}\,\frac{\partial\ell_i}{\partial p_i}\,\frac{\mathrm{d}p_i}{\mathrm{d}z_i}\,\frac{\partial z_i}{\partial b} \]
The first slope is \(\partial J/\partial\ell_i=1/N\) because \(J\) is a mean. Work out the remaining slopes for each flower.
\[ \ell_i=-y_i\ln(p_i)-(1-y_i)\ln(1-p_i)\qquad p_i=\sigma(z_i)=\frac{1}{1+e^{-z_i}} \]
\[ \frac{\partial\ell_i}{\partial p_i}=-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\qquad \frac{\mathrm{d}p_i}{\mathrm{d}z_i}=p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=\frac{\partial\ell_i}{\partial p_i}\frac{\mathrm{d}p_i}{\mathrm{d}z_i}=\left(-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\right)p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=-y_i(1-p_i)+(1-y_i)p_i=p_i-y_i \]
\[ \ell_i=-y_i\ln(p_i)-(1-y_i)\ln(1-p_i)\qquad p_i=\sigma(z_i)=\frac{1}{1+e^{-z_i}} \]
\[ \frac{\partial\ell_i}{\partial p_i}=-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\qquad \frac{\mathrm{d}p_i}{\mathrm{d}z_i}=p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=\frac{\partial\ell_i}{\partial p_i}\frac{\mathrm{d}p_i}{\mathrm{d}z_i}=\left(-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\right)p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=-y_i(1-p_i)+(1-y_i)p_i=p_i-y_i \]
\[ \ell_i=-y_i\ln(p_i)-(1-y_i)\ln(1-p_i)\qquad p_i=\sigma(z_i)=\frac{1}{1+e^{-z_i}} \]
\[ \frac{\partial\ell_i}{\partial p_i}=-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\qquad \frac{\mathrm{d}p_i}{\mathrm{d}z_i}=p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=\frac{\partial\ell_i}{\partial p_i}\frac{\mathrm{d}p_i}{\mathrm{d}z_i}=\left(-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\right)p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=-y_i(1-p_i)+(1-y_i)p_i=p_i-y_i \]
\[ \ell_i=-y_i\ln(p_i)-(1-y_i)\ln(1-p_i)\qquad p_i=\sigma(z_i)=\frac{1}{1+e^{-z_i}} \]
\[ \frac{\partial\ell_i}{\partial p_i}=-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\qquad \frac{\mathrm{d}p_i}{\mathrm{d}z_i}=p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=\frac{\partial\ell_i}{\partial p_i}\frac{\mathrm{d}p_i}{\mathrm{d}z_i}=\left(-\frac{y_i}{p_i}+\frac{1-y_i}{1-p_i}\right)p_i(1-p_i) \]
\[ \frac{\partial\ell_i}{\partial z_i}=-y_i(1-p_i)+(1-y_i)p_i=p_i-y_i \]
\[ \frac{\partial z_i}{\partial w_j}=x_{ij}\qquad\frac{\partial z_i}{\partial b}=1 \]
\[ \frac{\partial\ell_i}{\partial w_j}=\frac{\partial\ell_i}{\partial z_i}\frac{\partial z_i}{\partial w_j}=(p_i-y_i)x_{ij}\qquad\frac{\partial\ell_i}{\partial b}=p_i-y_i \]
\[ \frac{\partial J}{\partial w_j}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i)x_{ij}\qquad\frac{\partial J}{\partial b}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i) \]
\[ \frac{\partial z_i}{\partial w_j}=x_{ij}\qquad\frac{\partial z_i}{\partial b}=1 \]
\[ \frac{\partial\ell_i}{\partial w_j}=\frac{\partial\ell_i}{\partial z_i}\frac{\partial z_i}{\partial w_j}=(p_i-y_i)x_{ij}\qquad\frac{\partial\ell_i}{\partial b}=p_i-y_i \]
\[ \frac{\partial J}{\partial w_j}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i)x_{ij}\qquad\frac{\partial J}{\partial b}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i) \]
\[ \frac{\partial z_i}{\partial w_j}=x_{ij}\qquad\frac{\partial z_i}{\partial b}=1 \]
\[ \frac{\partial\ell_i}{\partial w_j}=\frac{\partial\ell_i}{\partial z_i}\frac{\partial z_i}{\partial w_j}=(p_i-y_i)x_{ij}\qquad\frac{\partial\ell_i}{\partial b}=p_i-y_i \]
\[ \frac{\partial J}{\partial w_j}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i)x_{ij}\qquad\frac{\partial J}{\partial b}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i) \]
\[ \frac{\partial z_i}{\partial w_j}=x_{ij}\qquad\frac{\partial z_i}{\partial b}=1 \]
\[ \frac{\partial\ell_i}{\partial w_j}=\frac{\partial\ell_i}{\partial z_i}\frac{\partial z_i}{\partial w_j}=(p_i-y_i)x_{ij}\qquad\frac{\partial\ell_i}{\partial b}=p_i-y_i \]
\[ \frac{\partial J}{\partial w_j}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i)x_{ij}\qquad\frac{\partial J}{\partial b}=\frac{1}{N}\sum_{i=1}^{N}(p_i-y_i) \]
Choose a downhill direction
Look at the Iris training cost as we vary only the length weight \(w_1\). Keep the width weight and bias fixed at zero.
We are at \(w_1=0\). Should the next small step increase or decrease the length weight?
Draw the local slope
The dashed tangent shows how the cost changes near the current weight.
The cost falls as \(w_1\) increases a little, so \(\partial J/\partial w_1\) is negative.
Take a step downhill
Take a small step against the slope. The learning rate \(\eta\) sets its size.
The arrow moves to a larger weight and a lower cost. Its horizontal displacement is the weight update.
Move against the slope
Increase the length weight. The Iris cost slopes down to the right at the current point.
\[ w_{1,\mathrm{new}}=w_1-\eta\frac{\partial J}{\partial w_1} \]
At \(w_1=0\), the slope is about \(-0.363\). With learning rate \(\eta=0.1\), the new length weight is about \(0-0.1(-0.363)=0.036\).
With the other parameters held fixed, the mean loss falls from \(0.693\) to \(0.684\).
Move all the parameters together
The gradient collects one slope of \(J\) for each parameter. Each slope averages that parameter’s contributions from all training flowers.
\[ w_{1,\mathrm{new}}=w_1-\eta\frac{\partial J}{\partial w_1}\qquad w_{2,\mathrm{new}}=w_2-\eta\frac{\partial J}{\partial w_2} \]
\[ b_{\mathrm{new}}=b-\eta\frac{\partial J}{\partial b} \]
Calculate all three slopes at the current weights and bias, then apply the changes together. The learning rate \(\eta\) sets the size of the step.
A small learning rate
Both Iris weights and the bias start at zero. Use \(\eta=0.01\).
The tighter axes show the small changes. Over 24 updates, mean loss falls by only 0.013.
A learning rate that is too large
Restart the same Iris model with \(\eta=0.36\).
The weights overshoot. On the wider axes, the mean loss reaches 0.994.
A learning rate that fits the model
Restart the same Iris model with \(\eta=0.19\).
On the same scales as the small-rate example, the cost falls faster, reaching 0.644 after 24 updates.
Which scores need the largest correction?
Calculate \(p-y\) for each example. Which score should move up, which should move down, and which changes are largest?
| Example | True label \(y\) | Probability \(p\) |
|---|---|---|
| A | 1 | 0.49 |
| B | 1 | 0.01 |
| C | 0 | 0.99 |
| D | 1 | 0.99 |
Confident errors give large score gradients
| Example | \(p-y\) | Desired score change |
|---|---|---|
| A | −0.51 | Up |
| B | −0.99 | Up strongly |
| C | +0.99 | Down strongly |
| D | −0.01 | Up slightly |
B and C give the strongest score corrections. Both flowers were given a high probability of the wrong label.
Start at \(w_1=0\), \(w_2=1\) and \(b=-1.7\).
The length weight is zero, so \(z=x_2-1.7\). The starting boundary is at a petal width of 1.7 cm.
The circled virginica has petals 5.1 cm long and 1.5 cm wide.
Its score is \(z=-0.20\), giving \(p\approx0.450\). The model predicts versicolor. Which way should the line move?
Take one gradient step using the circled flower and \(\eta=0.02\).
\[ (\Delta w_1,\Delta w_2,\Delta b)=\eta(1-p)(5.1,1.5,1) \]
The new weights and bias are \((0.0561,1.0165,-1.6890)\). The same flower is now above the line with \(p\approx0.530\).
By hand update example
A virginica flower has petal length \(x_1=5\) cm and width \(x_2=2\) cm. Its label is \(y=1\). For this exercise, train on this flower alone.
\[ w_1=w_2=b=0\qquad\Longrightarrow\qquad z=0,\quad p=0.5 \]
Use \(\eta=0.1\). How do the length weight, width weight and bias change? What probability will the model give this flower after the update?
Calculate the three slopes
For this flower, the slope with respect to its score is \(p-y=0.5-1=-0.5\).
| Parameter | Multiplier | Loss slope |
|---|---|---|
| Length weight \(w_1\) | 5 | (−0.5) × 5 = −2.5 |
| Width weight \(w_2\) | 2 | (−0.5) × 2 = −1.0 |
| Bias \(b\) | 1 | (−0.5) × 1 = −0.5 |
All three slopes are negative. A small increase in any of these parameters would lower this flower’s loss.
Apply the three changes
Start from zero and subtract \(0.1\) times each slope.
\[ w_{1,\mathrm{new}}=0-0.1(-2.5)=0.25 \]
\[ w_{2,\mathrm{new}}=0-0.1(-1.0)=0.10 \]
\[ b_{\mathrm{new}}=0-0.1(-0.5)=0.05 \]
The same starting prediction gives all three changes.
The update raises the probability of label 1
Put the new weights and bias back into the score for the same flower.
\[ z_{\mathrm{new}}=0.25(5)+0.10(2)+0.05=1.50 \]
\[ p_{\mathrm{new}}=\sigma(1.50)\approx0.818 \]
The probability of the flower’s true label rises from \(0.500\) to \(0.818\). Its loss falls from \(0.693\) to \(0.201\).
Keep following the same flower
The last column gives the gap between the current probability and 1. That gap sets the size of the next step.
\[ (\Delta w_1,\Delta w_2,\Delta b)=0.1(1-p)(5,2,1) \]
| Updates | \(w_1\) | \(w_2\) | \(b\) | \(p\) | Loss | Next \(1-p\) |
|---|---|---|---|---|---|---|
| 0 | 0.000 | 0.000 | 0.000 | 0.500 | 0.693 | 0.500 |
| 1 | 0.250 | 0.100 | 0.050 | 0.818 | 0.201 | 0.182 |
Use the row for update 1 to work out the three changes for update 2.
Follow five updates
| Updates | \(w_1\) | \(w_2\) | \(b\) | \(p\) | Loss | Next \(1-p\) |
|---|---|---|---|---|---|---|
| 0 | 0.000 | 0.000 | 0.000 | 0.500 | 0.693 | 0.500 |
| 1 | 0.250 | 0.100 | 0.050 | 0.818 | 0.201 | 0.182 |
| 2 | 0.341 | 0.136 | 0.068 | 0.886 | 0.121 | 0.114 |
| 3 | 0.398 | 0.159 | 0.080 | 0.916 | 0.088 | 0.084 |
| 4 | 0.440 | 0.176 | 0.088 | 0.934 | 0.069 | 0.066 |
| 5 | 0.474 | 0.189 | 0.095 | 0.945 | 0.057 | 0.055 |
Each row’s \(1-p\) gives the next update through \(0.1(1-p)(5,2,1)\). As the probability approaches 1, all three changes shrink.
Large weights make sharper predictions
Both panels show the same 60 Iris training flowers. Multiply the original weights and bias by 20 for the right panel.
Both models place the boundary at \(z=0\). In the right panel, the probabilities reach nearly 0 or 1 much closer to the line.
A better training fit can do worse on new flowers
The solid markers show 40 more flowers. The circled versicolor gets a predicted probability of 99.7% for virginica.
| Model | Mean loss on training flowers | Mean loss on new flowers |
|---|---|---|
| Original weights | 0.209 | 0.295 |
| Weights and bias × 20 | 0.189 | 0.408 |
The larger weights lower training loss and raise the loss on new flowers. This is overfitting of the predicted probabilities.
Feature scale changes the gradient path
The plots use the same learning rate. With unequal feature scales, a step can be large in one direction and small in another.
Putting features on comparable scales makes the weight sizes easier to compare before applying a penalty.
L1 and L2 add a cost for large weights
With comparable feature scales, the magnitudes of the weights give a more consistent basis for a penalty.
\[ J_{\mathrm{L1}}=J+\lambda\sum_j |w_j| \]
\[ J_{\mathrm{L2}}=J+\frac{\lambda}{2}\sum_j w_j^2 \]
The parameter \(\lambda\ge0\) controls how much we charge for weight size. Apply the penalty to the feature weights.
L2 changes the gradient step
Write \(g_j=\partial J/\partial w_j\) for the data gradient. The L2 penalty adds \(\lambda w_j\).
\[ w_{j,\mathrm{new}}=w_j-\eta(g_j+\lambda w_j)=(1-\eta\lambda)w_j-\eta g_j \]
For \(0<\eta\lambda<1\), the first factor shrinks the current weight. The second term takes a step against the data gradient.
\(\lambda\) sets the penalty strength. \(\eta\) scales the whole update.
Where a loss contour meets the weight budget
Use the scaled loss contours. The L1 budget \(|w_1|+|w_2|\le1\) gives the diamond. The L2 budget \(w_1^2+w_2^2\le1\) gives the disk.
The highlighted contour is the lowest one that reaches each allowed region. Here the L1 diamond meets it at a corner, giving one weight exactly zero.
From measurements to a gradient step
- Calculate \(z=w^\top x+b\) and turn it into a probability with the sigmoid.
- Give each example loss \(-\ln(p_{\mathrm{correct}})\), using the probability of its correct label, and average those losses.
- Use the residual \(p-y\) to calculate the gradient. Step against it with a suitable learning rate.
- Scale the features and use a weight penalty when you want to control the size of the fitted coefficients.
Further reading Logistic regression · Bayes and class densities · Regularisation