Basis Lab

Answers · How models work

Why does logistic regression use a sigmoid instead of a straight line?

In shortThe sigmoid turns an unbounded straight-line score into a probability between zero and one, and it pairs with the log score so fitting keeps the same gradient shape.

Logistic regression uses a sigmoid because the straight line underneath it is allowed to produce any real number, including minus four or eleven hundred, and neither can be a probability. The sigmoid maps that whole line into the interval from zero to one, so the output can be read as the probability of yes rather than as a score that happens to look smooth. The straight-line part is left alone; only how its output is read changes.

z=w1x1+⋯+wkxk+b,p^=11+e−zz = w_1x_1 + \cdots + w_kx_k + b, \qquad \hat p = \frac{1}{1+e^{-z}}

Check the arithmetic at three points. At z=0z=0, e0=1e^0=1, so p^=1/(1+1)=0.5\hat p=1/(1+1)=0.5. At z=2z=2, e−2e^{-2} is about 0.13530.1353, so p^=1/(1+0.1353)=0.881\hat p=1/(1+0.1353)=0.881. At z=−2z=-2, e2e^2 is about 7.3897.389, so p^=1/(1+7.389)=0.119\hat p=1/(1+7.389)=0.119. The value 0.50.5 is the boundary; positive zz pushes the probability above a half, negative zz below it; larger magnitude means more confidence, but the result never reaches 11 or 00 exactly because e−ze^{-z} is never zero and 1+e−z1+e^{-z} is never 11.

The squash is doing work

The natural guess is that the sigmoid is a cosmetic bend applied after the real model has done its job. It is not. A straight line cannot be a probability, and clipping a straight line at 00 and 11 is not a harmless repair: outside the clipped range the output is exactly 00 or 11 and the slope with respect to the knobs is exactly zero, so an example far on the wrong side of the boundary stops pulling the line back. The sigmoid has slope p^(1−p^)\hat p(1-\hat p), which is small far out but never zero, so every example still contributes a direction to move. The bend is also what makes the raw score zz readable as confidence: z=0z=0 is balanced, z=2z=2 is strong yes, z=−2z=-2 is strong no.

The score has to match the squash

Keeping the squared score after the squash is legal but bad. It charges at most one for a confident mistake, so saying 0.9990.999 when the answer is no costs 0.9992=0.9980010.999^2=0.998001, while saying 0.60.6 when the answer is no costs 0.62=0.360.6^2=0.36; the catastrophic error is less than three times the mild one. The matching score charges the negative logarithm of the probability given to what happened. For the same two mistakes it charges −log⁡(0.001)=6.908-\log(0.001)=6.908 and −log⁡(0.4)=0.916-\log(0.4)=0.916, so the confident mistake is about 7.57.5 times the mild one and can grow without limit as the probability goes to zero.

FIG 1
predicted probability of yessquared chargelog charge
0.9990.9980016.908
0.60.360.916
0.10.010.105
L=−1n∑i=1n[yilog⁡p^i+(1−yi)log⁡(1−p^i)]L = -\frac{1}{n}\sum_{i=1}^{n}\big[y_i\log\hat p_i + (1-y_i)\log(1-\hat p_i)\big]

In that expression only one term is ever active. When yi=1y_i=1 the second term is multiplied by 00, so the charge is −log⁡(p^i)-\log(\hat p_i); when yi=0y_i=0 the first term is multiplied by 00, so the charge is −log⁡(1−p^i)-\log(1-\hat p_i). The formula is the surprise at the answer that actually occurred, averaged over examples. It is the score that belongs with a probability output, not an arbitrary choice.

Why the slope barely changes

The pairing is what keeps the fitting loop familiar. The sigmoid has derivative p^(1−p^)\hat p(1-\hat p). The log score is built so that when you differentiate it with respect to zz, the p^(1−p^)\hat p(1-\hat p) from the sigmoid cancels against the denominators from the two logs, leaving p^−y\hat p-y. Then differentiating zz with respect to wjw_j gives xijx_{ij}, so the slope is the average of error times input. Apart from the factor of two that squared error carried, this is the same shape as the straight-line fit.

∂L∂wj=1n∑i=1n(p^i−yi)xijagainst∂Lsquared∂wj=2n∑i=1n(y^i−yi)xij\frac{\partial L}{\partial w_j} = \frac{1}{n}\sum_{i=1}^{n}(\hat p_i-y_i)x_{ij} \qquad\text{against}\qquad \frac{\partial L_{\text{squared}}}{\partial w_j} = \frac{2}{n}\sum_{i=1}^{n}(\hat y_i-y_i)x_{ij}

That is why the fitting procedure changes in two places only: the squash and the score. The step that updates the weights still uses error times input, now with the error written as a probability minus a zero or a one. No new optimiser is needed, because the gradient has the same form as before.

What changes if an assumption moves? The sigmoid is specifically for two outcomes. With three or more mutually exclusive outcomes the matching squash is the softmax, which maps a whole vector of scores to probabilities that sum to one; a separate sigmoid on each class would allow the probabilities to sum to more than one. The sigmoid guarantees a number between zero and one, not that the number is a calibrated frequency: if the fitted probabilities are systematically too high, that is a calibration problem to check separately. And the probability is not the action. The cut off that turns a probability into a yes is a decision about costs, not part of the fitted model; moving it changes false alarms and missed cases without changing the weights.

Worth keeping

  • Sigmoid output is always strictly between zero and one, so it can be read as a probability, while a straight line cannot.
  • The log score is unbounded for confident mistakes, which is why it trains probabilities differently from squared error.
  • The derivative of the log score with respect to the weights has the same error-times-input form as linear regression, apart from the factor of two.

Where this goes next

This question is one step of The Simplest Model That Learns, a course that works through its subject from the beginning. The page that teaches this part of it is Predicting Yes or No, and it reads the whole way through whether or not you have an account.

What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.

Read that pageThe whole course

Other questions people ask about this

  1. What is actually new in a neural network compared with linear regression?Only three things are new: the slopes are computed by working backwards, the score is no longer a bowl, and the model builds its own inputs. The prediction, the loss, the step and the held out check are unchanged.
  2. What is the difference between the inputs and the weights in a linear model?The input is supplied by each example and changes from row to row; the weights are the model's adjustable numbers, chosen once by fitting and shared by every example.
  3. How do I know if I should increase or decrease the slope to reduce the error?The sign of how fast the loss changes with the slope tells you: positive means lower the slope, negative means raise it.
  4. Why does linear regression square the errors instead of just adding their absolute values?Squaring makes large errors count much more than small ones, so it picks a different line from absolute value; absolute value is a different choice about costs, not a mistake.
  5. Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
  6. Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.

Read alongside