SECTION 02 / 4

Output Functions and Loss Functions — Linear, Softmax, Squared Error, Cross-Entropy

4 min read
On this page 7 headings
BUILD INTUITION WITH NUMBERS

Turn two scores into probabilities and a loss

Two class scores are 0 and ln(3). The correct class is the second one. Use natural logarithms.

  1. Exponentiate the scores.

    e0=1,eln⁡3=3e^0=1,\qquad e^{\ln3}=3
  2. Normalise by their sum.

    y^=[14,34]=[0.25,0.75]\hat y=\left[\frac14,\frac34\right]=[0.25,0.75]
  3. Cross-entropy uses the probability assigned to the correct class.

    L=−ln⁡(0.75)≈0.2877L=-\ln(0.75)\approx0.2877

What changed? Softmax compares scores; cross-entropy rewards high probability on the correct class.

Quick check: What would the loss be if the first class were correct instead?

−ln(0.25) ≈ 1.3863: assigning the correct class only 25% probability is penalised more.

Swipe long formulas, tables or code sideways to see the rest.

Start here What the network should output depends on the question. Predicting a rating (a real number) needs a different last layer and a different loss than picking one fruit out of four. This page gives the two standard pairs you will use again and again: linear + squared error and softmax + cross-entropy.

1. Case 1: predicting real numbers

Lecture example — movie ratings Input: movie features (isActorDamon, isDirectorNolan, …). Output: three ratings, y=[7.5,8.2,7.7]y = [7.5, 8.2, 7.7] (IMDb rating, critics’ rating, RT rating). Soy∈R3So y \in \mathbb{R}^{3}.

Loss: the loss should measure how fary^far \hat{y} is from y. For real-valued outputs, squared error does this:

L(θ)=(1/N)∑i=1N∑j=1k(y^ij−yij)2L(\theta ) = (1/N) \sum _{i=1}^{N} \sum _{j=1}^{k} (\hat{y}_{ij} - y_{ij})^{2}

Output function: can O be the logistic (sigmoid)? No. Sigmoid squashes everything into (0, 1), but a rating like 8.2 is outside that range. So use a linear output:

y^=O(aL)=WOaL+bO\hat{y} = O(a_L) = W_O a_L + b_O

Nowy^Now \hat{y} is not stuck between 0 and 1.

2. Case 2: choosing one of k classes

Lecture example — fruit image Classify an image as one of 4 classes: Apple, Mango, Orange, Banana. If it is an apple, the true label is y=[1,0,0,0]y = [1, 0, 0, 0].

We could use squared error, but there is a better choice.

Notice: y is a probability distribution (all values ≥0\ge 0, total =1= 1). Soy^So \hat{y} should also be a probability distribution.

Softmax: turning scores into probabilities

y^j=O(aL)j=eaL,j/∑i=1keaL,i\hat{y}_{j} = O(a_L)_{j} = e^{a_L,j} / \sum _{i=1}^{k} e^{a_L,i}

  • eanythinge^{anything} is positive → every y^j>0\hat{y}_{j} > 0.
  • Dividing by the total → they add up to 1.
Exponentiate each score, then divide by the total.
Exponentiate each score, then divide by the total.
Open image ↗
Scores 2, 1, 0.1 become probabilities 0.659, 0.242, 0.099 after softmax

Worked numbers: aL=[2.0,1.0,0.1]a_L = [2.0, 1.0, 0.1] for [apple, mango, orange].

  1. e2=7.389,e1=2.718,e0.1=1.105e^{2} = 7.389, e^{1} = 2.718, e^0.1 = 1.105
  2. Sum=7.389+2.718+1.105=11.212Sum = 7.389 + 2.718 + 1.105 = 11.212
  3. y^=[7.389/11.212,2.718/11.212,1.105/11.212]=\hat{y} = [7.389/11.212, 2.718/11.212, 1.105/11.212] = [0.659, 0.242, 0.099]
  4. Check: 0.659+0.242+0.099=1.0000.659 + 0.242 + 0.099 = 1.000 ✔

Cross-entropy: comparing two distributions

L(θ)=−∑c=1kyclog⁡y^cL(\theta ) = - \sum _{c=1}^{k} y_c \log \hat{y}_c

Since yc=1y_c = 1 only for the true class ℓ and 0 for all others, every term except one vanishes:

L(θ)=−log⁡y^ℓL(\theta ) = - \log \hat{y}_\ell

So for classification we:

minimise L(θ)=−log⁡y^L(\theta ) = - \log \hat{y}_ℓ or equivalently maximise log⁡y^\log \hat{y}_ℓ

Give the correct class a high probability → small loss. Give it a low probability → big loss.
Give the correct class a high probability → small loss. Give it a low probability → big loss.
Open image ↗
Graph of minus log p: near zero when p is near 1, very large when p is near 0
y^\hat{y}_ℓ (probability of true class) Loss −log⁡y^-\log \hat{y}_ℓ
0.9 0.105
0.659 0.417
0.5 0.693
0.1 2.303

(log here is the natural log, ln.)

Isy^Is \hat{y}_ℓ a function of θ\theta ? Yes:

y^ℓ=[O(W3g(W2g(W1x+b1)+b2)+b3)]ℓ\hat{y}_\ell = [O(W_{3} g(W_{2} g(W_{1}x + b_{1}) + b_{2}) + b_{3})]_\ell

It is the probability that x belongs to class ℓ. We want to push it close to 1. log⁡y^\log \hat{y}_ℓ is called the log-likelihood of the data.

3. The summary table

Pick the pair by what the output means.
Pick the pair by what the output means.
Open image ↗
Real values: linear output with squared error; probabilities: softmax output with cross-entropy
Output type Output activation Loss function
Real values Linear Squared error
Probabilities Softmax Cross-entropy

Other loss functions exist for other problems, but these two are by far the most common. For the rest of the backpropagation derivation we use softmax output + cross-entropy loss.

(The lecture also links a separate video on Information and Entropy for the background of cross-entropy.)

In one line: Use a linear output with squared error for real-valued targets, and a softmax output with cross-entropy (−log of the true class’s probability) for classification.

Exam words: linear output, squared error loss, softmax, probability distribution, one-hot label, cross-entropy, −log⁡y^-\log \hat{y}_ℓ, log-likelihood.

Quick check

  1. Why not use a sigmoid output to predict a rating of 8.2?
The sigmoid output is always between 0 and 1, so it can never produce 8.2. A linear output is needed.
  1. Softmax of a=[0,0,0]a = [0, 0, 0]?
Each e0=1e^{0} = 1, sum =3= 3, so y^=[1/3,1/3,1/3]\hat{y} = [1/3, 1/3, 1/3].
  1. True class is mango, y^=[0.659,0.242,0.099]\hat{y} = [0.659, 0.242, 0.099]. Cross-entropy loss?
−log⁡(0.242)=1.419. -\log (0.242) = 1.419.
  1. Does adding the same constant to every score change the softmax?
No. ea+c=ee^{a + c} = eᶜ · eᵃ, and eᶜ cancels between the top and the bottom.
  1. Minimising −log⁡y^-\log \hat{y}_ℓ is the same as maximising what?
log⁡y^\log \hat{y}_ℓ, the log-likelihood (the probability given to the true class).
YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑