§6.2.1–6.2.2aCost Functions · Linear & Sigmoid Output Units

Part II DL pp. 178–185 · ~5 min read

  • cross-entropy cost
  • mean absolute error
  • functional
  • linear output unit
  • sigmoid output unit
  • logit

§6.2.1 Cost functions

Neural-network cost functions are, happily, almost identical to those of any parametric model. In most cases the model defines a distribution p(yx;θ)p(y \mid \boldsymbol{x}; \boldsymbol{\theta}) and we apply maximum likelihood — i.e. minimize the cross-entropy between data and model:

Term by term

J(θ)=Ex,yp^datalogpmodel(yx)J(\boldsymbol{\theta}) = -\mathbb{E}_{\boldsymbol{x}, y \sim \hat{p}_{\mathrm{data}}} \log p_{\mathrm{model}}(y \mid \boldsymbol{x})
pmodel(yx)p_{\mathrm{model}}(y \mid \boldsymbol{x})the output distribution you CHOOSE — and that choice automatically determines the cost. Gaussian ⇒ MSE (up to a constant; §5.5.1), Bernoulli ⇒ the sigmoid loss belowdesign decision
log-\logthe negative-log-likelihood form: its log is what UNDOES the exp inside saturating output units, keeping the gradient alivethe key trick
J(θ)J(\boldsymbol{\theta})usually combined with a regularizer (weight decay applies unchanged from §5.2.2; ch. 7 adds more)total cost

Why maximum likelihood removes a design chore

Specifying p(yx)p(y \mid \boldsymbol{x}) automatically gives you the cost logp(yx)-\log p(y \mid \boldsymbol{x}) — you never hand-design a loss per model. The one recurring demand: the gradient must stay large and predictable to guide learning. Functions that saturate (go flat) kill the gradient; the log in the NLL is engineered to cancel the exp that most output units use, precisely to prevent that.

One quirk: cross-entropy usually has no minimum for real models. A discrete model like logistic regression can approach probability 0 or 1 but never reach it; a real-valued model that controls its output variance can drive the density — and thus the negative log-likelihood — toward -\infty. Regularization (chapter 7) removes this unbounded reward.

§6.2.1.2 Learning conditional statistics

Sometimes we want just one statistic of yy, not the whole distribution. With a powerful enough network we can treat the cost as a functional (a map from functions to reals) and use calculus of variations to see which statistic each loss recovers. Squared error recovers the mean; the mean absolute error recovers the median:

Term by term

f=argminfEx,ypdatayf(x)2f(x)=Eyp(yx)[y]f=argminfEx,ypdatayf(x)1f(x)=median(yx)\begin{aligned} f^* &= \arg\min_f \mathbb{E}_{\boldsymbol{x},y \sim p_{\mathrm{data}}} \|y - f(\boldsymbol{x})\|^2 &&\Rightarrow&& f^*(\boldsymbol{x}) = \mathbb{E}_{y \sim p(y|\boldsymbol{x})}[y] \\ f^* &= \arg\min_f \mathbb{E}_{\boldsymbol{x},y \sim p_{\mathrm{data}}} \|y - f(\boldsymbol{x})\|_1 &&\Rightarrow&& f^*(\boldsymbol{x}) = \mathrm{median}(y \mid \boldsymbol{x}) \end{aligned}
MSEmean\text{MSE} \to \text{mean}minimizing squared error (over infinite data) yields the function predicting the MEAN of y given xL² statistic
MAEmedian\text{MAE} \to \text{median}mean absolute error (L¹) instead yields the MEDIAN — different loss, different statisticL¹ statistic
but\text{but…}both MSE and MAE give POOR gradients with saturating output units — another reason cross-entropy is preferred even to estimate a single statisticthe catch

§6.2.2 Output units

The choice of cost is tightly coupled to the output unit: the output representation fixes the form of the cross-entropy. (Any output unit can also serve as a hidden unit — §6.3 revisits them.) Throughout, suppose the network provides hidden features h=f(x;θ)\boldsymbol{h} = f(\boldsymbol{x}; \boldsymbol{\theta}); the output layer transforms them to complete the task.

§6.2.2.1 Linear units — for Gaussian outputs

A linear output unit is just an affine map with no nonlinearity: y^=Wh+b\hat{\boldsymbol{y}} = \boldsymbol{W}^\top\boldsymbol{h} + \boldsymbol{b}. Used as the mean of a conditional Gaussian p(yx)=N(y;y^,I)p(y \mid \boldsymbol{x}) = \mathcal{N}(y; \hat{\boldsymbol{y}}, \boldsymbol{I}), maximizing log-likelihood = minimizing MSE. Because linear units do not saturate, they pose little trouble for gradient-based optimizers.

§6.2.2.2 Sigmoid units — for Bernoulli outputs

For a binary yy, the net must predict P(y=1x)[0,1]P(y = 1 \mid \boldsymbol{x}) \in [0,1]. The tempting fix — threshold a linear unit to [0,1] — is a trap: any time the linear value strays outside the unit interval, its gradient is 0, and learning stalls. The sigmoid output unit instead guarantees a strong gradient whenever the answer is wrong: y^=σ(wh+b)\hat{y} = \sigma(\boldsymbol{w}^\top\boldsymbol{h} + b). Motivate it by building an unnormalized distribution whose log is linear in the score z=wh+bz = \boldsymbol{w}^\top\boldsymbol{h} + b — the logit :

Sigmoid from unnormalized log-probabilities (eqs 6.20 → 6.23)
logP~(y)=yz\log \tilde{P}(y) = yz

step 1/4: Assume the unnormalized LOG-probability is linear in y and the score z = wᵀh + b (the "logit").

The payoff is in the loss. With maximum likelihood, J=logP(yx)J = -\log P(y \mid \boldsymbol{x}), and the log undoes the exp:

The sigmoid NLL saturates only when CORRECT (eqs 6.24 → 6.26)
J(θ)=logP(yx)=logσ((2y1)z)J(\boldsymbol{\theta}) = -\log P(y \mid \boldsymbol{x}) = -\log \sigma\big((2y-1)z\big)

step 1/3: The negative log-likelihood of the sigmoid-Bernoulli output.

See it directly — the NLL grows without bound when wrong (living gradient), while MSE flattens on the wrong side (dead gradient):

Loss vs the logit z for a target-1 example — drag the probe to the "very wrong" left side
0.04.08.0-808logit z
cross-entropy (NLL)= 4.018MSE= 0.964
logit z-4.00

Aside — why this matters, and a numerical note

With MSE, the loss saturates whenever σ(z)\sigma(z) saturates — the gradient dies whether the model is right OR wrong. That is why maximum likelihood is almost always preferred for sigmoid outputs. One software caution: write the NLL as a function of zz, not of y^=σ(z)\hat{y} = \sigma(z) — if σ\sigma underflows to 0, logy^\log \hat{y} becomes -\infty, whereas logσ\log \sigma is always finite because σ(0,1)\sigma \in (0, 1) (open interval).

Next: the multiclass generalization (softmax) and richer output types (mixture density networks): softmax and beyond.

Check yourself — cost functions and output units

1.What does choosing the output distribution p(y|x) automatically give you?

2.Minimizing MSE recovers one statistic of y|x, and minimizing mean absolute error recovers another. Which?

3.In the loss-vs-logit plot, you drag the probe far to the left (a very wrong target-1 prediction). How do the two curves differ?

4.Why is thresholding a linear unit into [0,1] a bad way to output a Bernoulli probability?

5.Why write the sigmoid NLL as a function of the logit z rather than of ŷ = σ(z)?

5 questions