AI / ML Theory
Chapter 1 · 22 min

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∈Ry \in \mathbb{R}. For binary classification we need y∈{0,1}y \in \{0, 1\} and a model that outputs a probability y^∈(0,1)\hat y \in (0, 1). The sigmoid is the canonical bridge: it squashes R\mathbb{R} into (0,1)(0, 1) in the way you want to interpret y^\hat 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\hat 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)=11+e−z.\sigma : \mathbb{R} \to (0, 1), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}.

Three properties to internalise.

Range and monotonicity. σ\sigma is strictly increasing from σ(−∞)=0\sigma(-\infty) = 0 to σ(+∞)=1\sigma(+\infty) = 1, with σ(0)=1/2\sigma(0) = 1/2.

Symmetry. 1−σ(z)=σ(−z)1 - \sigma(z) = \sigma(-z). (Multiply numerator and denominator of σ(−z)\sigma(-z) by eze^z to verify.)

Self-derivative identity. Differentiate

σ′(z)=ddz11+e−z=e−z(1+e−z)2=11+e−z⋅e−z1+e−z.\sigma'(z) = \frac{d}{dz} \frac{1}{1 + e^{-z}} = \frac{e^{-z}}{(1 + e^{-z})^2} = \frac{1}{1 + e^{-z}} \cdot \frac{e^{-z}}{1 + e^{-z}}.

The first factor is σ(z)\sigma(z). The second is σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z). Hence

 σ′(z)  =  σ(z) (1−σ(z)). \boxed{\ \sigma'(z) \;=\; \sigma(z)\,\bigl(1 - \sigma(z)\bigr).\ }

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}y \in \{0, 1\} and define the logit

z=w⊤x+b.z = w^\top x + b.

Model the conditional probability of class 11 by the sigmoid of the logit:

y^  =  p(y=1∣x;w,b)  =  σ(z),p(y=0∣x)=1−y^.\hat y \;=\; p(y = 1 \mid x; w, b) \;=\; \sigma(z), \qquad p(y = 0 \mid x) = 1 - \hat y.

A compact single expression covering both classes:

p(y∣x;w,b)=y^ y (1−y^)1−y.p(y \mid x; w, b) = \hat y^{\,y}\,(1 - \hat y)^{1 - y}.

(Plug y=0y = 0 and y=1y = 1 to recover the two Bernoulli probabilities.)


4. From MLE to binary cross-entropy

Given i.i.d. data {(xi,yi)}i=1m\{(x_i, y_i)\}_{i=1}^m, the log-likelihood is

log⁡p(y1:m∣X;w,b)=∑i=1m[ yilog⁡y^i+(1−yi)log⁡(1−y^i) ],\log p(y_{1:m} \mid X; w, b) = \sum_{i=1}^m \bigl[\,y_i \log \hat y_i + (1 - y_i) \log (1 - \hat y_i)\,\bigr],

with y^i=σ(w⊤xi+b)\hat y_i = \sigma(w^\top x_i + b). MLE maximises this. Negating to convert to a minimisation,

 LBCE(w,b)  =  −1m∑i=1m[ yilog⁡y^i+(1−yi)log⁡(1−y^i) ]. \boxed{\ L_{\text{BCE}}(w, b) \;=\; -\frac{1}{m} \sum_{i=1}^m \bigl[\,y_i \log \hat y_i + (1 - y_i) \log(1 - \hat y_i)\,\bigr].\ }

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\partial L / \partial z = \hat y - y

Work on a single example, so drop the average and the index ii. Let L=−ylog⁡y^−(1−y)log⁡(1−y^)L = -y \log \hat y - (1 - y) \log(1 - \hat y) with y^=σ(z)\hat y = \sigma(z). We compute ∂L/∂z\partial L / \partial z via the chain rule

∂L∂z=∂L∂y^⋅∂y^∂z.\frac{\partial L}{\partial z} = \frac{\partial L}{\partial \hat y} \cdot \frac{\partial \hat y}{\partial z}.

Each factor in turn.

∂L∂y^=−yy^+1−y1−y^=−y(1−y^)+(1−y)y^y^(1−y^)=y^−yy^(1−y^).\frac{\partial L}{\partial \hat y} = -\frac{y}{\hat y} + \frac{1 - y}{1 - \hat y} = \frac{-y(1 - \hat y) + (1 - y)\hat y}{\hat y (1 - \hat y)} = \frac{\hat y - y}{\hat y (1 - \hat y)}.

From §2,

∂y^∂z=σ′(z)=y^ (1−y^).\frac{\partial \hat y}{\partial z} = \sigma'(z) = \hat y\,(1 - \hat y).

Multiplying,

∂L∂z=y^−yy^(1−y^)⋅y^(1−y^)=y^−y.\frac{\partial L}{\partial z} = \frac{\hat y - y}{\hat y (1 - \hat y)} \cdot \hat y (1 - \hat y) = \hat y - y.  ∂LBCE∂z  =  y^−y. \boxed{\ \frac{\partial L_{\text{BCE}}}{\partial z} \;=\; \hat y - y.\ }

The y^(1−y^)\hat y (1 - \hat 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 σ′\sigma' tail to amplify or shrink. Compare with pairing sigmoid + MSE, which leaves a σ′=σ(1−σ)∈(0,1/4]\sigma' = \sigma(1 - \sigma) \in (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 11 when y^>1/2\hat y > 1/2, equivalently z>0z > 0, equivalently w⊤x+b>0w^\top x + b > 0. The boundary is the hyperplane w⊤x+b=0w^\top 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)(0,1)-valued, with self-derivative σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)).
  • Sigmoid + Bernoulli MLE = BCE loss.
  • ∂L/∂z=y^−y\partial L / \partial z = \hat y - y. Exact, simple, and the reason this is the canonical pairing.

Self-test

(a) Prove 1−σ(z)=σ(−z)1 - \sigma(z) = \sigma(-z) directly from the definition.

(b) Show σ′(z)=σ(z) σ(−z)\sigma'(z) = \sigma(z)\,\sigma(-z).

(c) Compute ∂LBCE/∂w\partial L_{\text{BCE}} / \partial w for a single example with y^=σ(w⊤x+b)\hat y = \sigma(w^\top x + b). Express it in terms of (y^−y)(\hat y - y) and xx.

(d) Why would pairing sigmoid with squared-error loss 12(y^−y)2\tfrac{1}{2}(\hat y - y)^2 be worse? Compute ∂L/∂z\partial L / \partial z for that pairing and explain when the gradient vanishes.


References

  1. [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.

    ↩