1.2 — Logistic regression: the sigmoid and binary cross-entropy
Goal: Derive the sigmoid model for binary classification, show its self-derivative identity, and obtain binary cross-entropy as the negative log-likelihood of a Bernoulli model.
1. Why this session exists
Linear regression handled y∈R. For binary classification we need y∈{0,1} and a model that outputs a probability y^∈(0,1). The sigmoid is the canonical bridge: it squashes R into (0,1) in the way you want to interpret y^ as a Bernoulli probability. Maximum likelihood for that Bernoulli model produces binary cross-entropy (BCE), and the gradient of BCE with respect to the pre-activation has a famously clean form — y^−y. This is the template every later session in the course will follow.
2. The sigmoid
Define the sigmoid function
σ:R→(0,1),σ(z)=1+e−z1.
Three properties to internalise.
Range and monotonicity.σ is strictly increasing from σ(−∞)=0 to σ(+∞)=1, with σ(0)=1/2.
Symmetry.1−σ(z)=σ(−z). (Multiply numerator and denominator of σ(−z) by ez to verify.)
Self-derivative identity. Differentiate
σ′(z)=dzd1+e−z1=(1+e−z)2e−z=1+e−z1⋅1+e−ze−z.
The first factor is σ(z). The second is σ(−z)=1−σ(z). Hence
σ′(z)=σ(z)(1−σ(z)).
The derivative is expressed in terms of the function value itself — a property that will save us repeated work during backprop.
3. The Bernoulli model for binary classification
Let y∈{0,1} and define the logit
z=w⊤x+b.
Model the conditional probability of class 1 by the sigmoid of the logit:
y^=p(y=1∣x;w,b)=σ(z),p(y=0∣x)=1−y^.
A compact single expression covering both classes:
p(y∣x;w,b)=y^y(1−y^)1−y.
(Plug y=0 and y=1 to recover the two Bernoulli probabilities.)
4. From MLE to binary cross-entropy
Given i.i.d. data {(xi,yi)}i=1m, the log-likelihood is
This is binary cross-entropy, the negative average log-likelihood of the Bernoulli model. Same recipe as 0.7 (cross-entropy = NLL) and 1.1 (least squares = NLL); only the family of distributions changed.
5. The clean gradient: ∂L/∂z=y^−y
Work on a single example, so drop the average and the index i. Let L=−ylogy^−(1−y)log(1−y^) with y^=σ(z). We compute ∂L/∂z via the chain rule
The y^(1−y^) in the denominator of the first factor cancels with the sigmoid derivative — the same cancellation we'll see again with softmax + cross-entropy in 1.8. This is the reason we pair sigmoid with BCE (and softmax with CE): the gradient at the pre-activation has no σ′ tail to amplify or shrink. Compare with pairing sigmoid + MSE, which leaves a σ′=σ(1−σ)∈(0,1/4] multiplier and creates a vanishing-gradient pathology when the model is confident.
6. The decision rule
At inference, the sigmoid model predicts class 1 when y^>1/2, equivalently z>0, equivalently w⊤x+b>0. The boundary is the hyperplane w⊤x+b=0. The classifier's class decisions depend only on the sign of the logit; the magnitude controls confidence.
This is the geometric handle we'll formalise in 1.4 (separating hyperplane, margin) and re-use throughout Chapters 1–6.
7. Takeaways
Sigmoid: smooth, monotone, (0,1)-valued, with self-derivative σ′(z)=σ(z)(1−σ(z)).
Sigmoid + Bernoulli MLE = BCE loss.
∂L/∂z=y^−y. Exact, simple, and the reason this is the canonical pairing.
Self-test
(a) Prove 1−σ(z)=σ(−z) directly from the definition.
(b) Show σ′(z)=σ(z)σ(−z).
(c) Compute ∂LBCE/∂w for a single example with y^=σ(w⊤x+b). Express it in terms of (y^−y) and x.
(d) Why would pairing sigmoid with squared-error loss 21(y^−y)2 be worse? Compute ∂L/∂z for that pairing and explain when the gradient vanishes.
References
[1]
Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. — Linear regression (1.1), logistic regression (1.2–1.3), softmax (1.7), MAP / Gaussian priors.