§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 and we apply maximum likelihood — i.e. minimize the cross-entropy cross-entropy cost J = −E_{x,y∼p̂} log p_model(y|x): the default neural-net cost from maximum likelihood; its log undoes the exp of saturating output units. defined in ch. 6 — open in glossary between data and model:
Term by term
| 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 below | design decision | |
| the negative-log-likelihood form: its log is what UNDOES the exp inside saturating output units, keeping the gradient alive | the key trick | |
| 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 automatically gives you the cost — 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 . Regularization (chapter 7) removes this unbounded reward.
§6.2.1.2 Learning conditional statistics
Sometimes we want just one statistic of , not the whole distribution. With a powerful enough network we can treat the cost as a functional functional A mapping from functions to real numbers; viewing the cost as a functional lets calculus of variations show which statistic each loss recovers. defined in ch. 6 — open in glossary (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 mean absolute error The L¹ loss E‖y − f(x)‖₁; its minimizer predicts the MEDIAN of y|x (MSE predicts the mean). defined in ch. 6 — open in glossary recovers the median:
Term by term
| minimizing squared error (over infinite data) yields the function predicting the MEAN of y given x | L² statistic | |
| mean absolute error (L¹) instead yields the MEDIAN — different loss, different statistic | L¹ statistic | |
| both MSE and MAE give POOR gradients with saturating output units — another reason cross-entropy is preferred even to estimate a single statistic | the 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 ; the output layer transforms them to complete the task.
§6.2.2.1 Linear units — for Gaussian outputs
A linear output unit linear output unit An output unit that is a pure affine map ŷ = Wᵀh + b (no nonlinearity); used as the mean of a conditional Gaussian, so ML = MSE. Does not saturate. defined in ch. 6 — open in glossary is just an affine map with no nonlinearity: . Used as the mean of a conditional Gaussian , 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 , the net must predict . 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 sigmoid output unit ŷ = σ(wᵀh + b) for a Bernoulli output; paired with NLL loss J = ζ((1−2y)z) that saturates only when the model is already correct. defined in ch. 6 — open in glossary instead guarantees a strong gradient whenever the answer is wrong: . Motivate it by building an unnormalized distribution whose log is linear in the score — the logit logit The pre-sigmoid/pre-softmax score z = log P̃(y): an unnormalized log-probability that the output activation exponentiates and normalizes. defined in ch. 6 — open in glossary :
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, , and the log undoes the exp:
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):
Aside — why this matters, and a numerical note
With MSE, the loss saturates whenever 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 , not of — if underflows to 0, becomes , whereas is always finite because (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)?