Skip to main content

Chapter 11.1 - Cross Entropy Loss

Cross Entropy Loss

Cross Entropy Loss measures how much probability the model assigned to the actual (correct) next token.

It does not care which token was predicted. It only looks at the probability of the correct target token.


Formula (Single Training Set)

For one training set (one prediction):

Cross Entropy Loss=ln(P(Target))\boxed{ \text{Cross Entropy Loss} = -\ln\left(P(\text{Target})\right) }

where

  • P(Target)P(\text{Target}) = probability assigned to the actual next token.
  • ln\ln = natural logarithm.

Interpretation

Probability of Correct TargetLoss
1.000.000
0.900.105
0.500.693
0.201.609
0.102.303
0.014.605

Batch Predictions (taking B1 batch)

Context SeenOutputProbability of OutputTargetProbability of TargetFull Probability Matrix Strip [hi, my, name, is, harshu, who, really, loves, family]
hihi0.24my0.13[0.24, 0.13, 0.11, 0.09, 0.12, 0.10, 0.08, 0.07, 0.06]
hi myreally0.18name0.10[0.09, 0.12, 0.10, 0.11, 0.13, 0.14, 0.18, 0.07, 0.06]
hi my namemy0.21is0.12[0.08, 0.21, 0.09, 0.12, 0.13, 0.11, 0.10, 0.09, 0.07]
hi my name isharshu0.29harshu0.29[0.05, 0.06, 0.07, 0.09, 0.29, 0.16, 0.12, 0.09, 0.07]
mymy0.26name0.12[0.08, 0.26, 0.12, 0.10, 0.08, 0.14, 0.09, 0.07, 0.06]
my namereally0.20is0.09[0.07, 0.11, 0.15, 0.09, 0.14, 0.13, 0.20, 0.06, 0.05]
my name iswho0.22harshu0.11[0.06, 0.09, 0.10, 0.13, 0.11, 0.22, 0.12, 0.10, 0.07]
my name is harshuwho0.31who0.31[0.04, 0.06, 0.08, 0.07, 0.09, 0.31, 0.14, 0.13, 0.08]

Step 1 — Calculate Loss for Every Training Set

Using

Loss=ln(P(Target))\text{Loss}=-\ln(P(\text{Target}))
#Probability of TargetCalculationLoss
10.13ln(0.13)-\ln(0.13)2.040
20.10ln(0.10)-\ln(0.10)2.303
30.12ln(0.12)-\ln(0.12)2.120
40.29ln(0.29)-\ln(0.29)1.238
50.12ln(0.12)-\ln(0.12)2.120
60.09ln(0.09)-\ln(0.09)2.408
70.11ln(0.11)-\ln(0.11)2.207
80.31ln(0.31)-\ln(0.31)1.171

Step 2 — Average All Losses

Batch Loss=2.040+2.303+2.120+1.238+2.120+2.408+2.207+1.1718=15.6078=1.951\begin{aligned} \text{Batch Loss} &= \frac{ 2.040 + 2.303 + 2.120 + 1.238 + 2.120 + 2.408 + 2.207 + 1.171 }{8} \\[4pt] &= \frac{15.607}{8} \\[4pt] &= \boxed{1.951} \end{aligned}

Batch feeding Input target explained

Here the entire logits is passed as first input so first tensor is inputs logits tensor so logits are like :

[0.24, 0.13, 0.11, 0.09, 0.12, 0.10, 0.08, 0.07, 0.06]
[0.09, 0.12, 0.10, 0.11, 0.13, 0.14, 0.18, 0.07, 0.06]
[0.08, 0.21, 0.09, 0.12, 0.13, 0.11, 0.10, 0.09, 0.07]
[0.05, 0.06, 0.07, 0.09, 0.29, 0.16, 0.12, 0.09, 0.07]
[0.08, 0.26, 0.12, 0.10, 0.08, 0.14, 0.09, 0.07, 0.06]
[0.07, 0.11, 0.15, 0.09, 0.14, 0.13, 0.20, 0.06, 0.05]
[0.06, 0.09, 0.10, 0.13, 0.11, 0.22, 0.12, 0.10, 0.07]
[0.04, 0.06, 0.08, 0.07, 0.09, 0.31, 0.14, 0.13, 0.08]
while on other hand targets are like this :
[ [4, 3, 1, 7], [2, 5, 6, 8] ]
after flattening it looks like this :
[4, 3, 1, 7, 2, 5, 6, 8]

or if we say better :

Logits / Probability Tensor (Simplified)

Shape:

(8,  9)(8,\;9)
RowProbability Distribution [hi, my, name, is, harshu, who, really, loves, family]
1[0.24, 0.13, 0.11, 0.09, 0.12, 0.10, 0.08, 0.07, 0.06]
2[0.09, 0.12, 0.10, 0.11, 0.13, 0.14, 0.18, 0.07, 0.06]
3[0.08, 0.21, 0.09, 0.12, 0.13, 0.11, 0.10, 0.09, 0.07]
4[0.05, 0.06, 0.07, 0.09, 0.29, 0.16, 0.12, 0.09, 0.07]
5[0.08, 0.26, 0.12, 0.10, 0.08, 0.14, 0.09, 0.07, 0.06]
6[0.07, 0.11, 0.15, 0.09, 0.14, 0.13, 0.20, 0.06, 0.05]
7[0.06, 0.09, 0.10, 0.13, 0.11, 0.22, 0.12, 0.10, 0.07]
8[0.04, 0.06, 0.08, 0.07, 0.09, 0.31, 0.14, 0.13, 0.08]
and targets tensor is like this initially:
targets=
[
[1,2,3,4]
[2,3,4,5]
]
after flattening it becoems [1,2,3,4,2,3,4,5]
this is simply index no. of the logit that should have the most probability

Both Tensors Together

RowLogits / Probability Tensor (8\times9)Target Tensor (8)
1[0.24, 0.13, 0.11, 0.09, 0.12, 0.10, 0.08, 0.07, 0.06]1
2[0.09, 0.12, 0.10, 0.11, 0.13, 0.14, 0.18, 0.07, 0.06]2
3[0.08, 0.21, 0.09, 0.12, 0.13, 0.11, 0.10, 0.09, 0.07]3
4[0.05, 0.06, 0.07, 0.09, 0.29, 0.16, 0.12, 0.09, 0.07]4
5[0.08, 0.26, 0.12, 0.10, 0.08, 0.14, 0.09, 0.07, 0.06]2
6[0.07, 0.11, 0.15, 0.09, 0.14, 0.13, 0.20, 0.06, 0.05]3
7[0.06, 0.09, 0.10, 0.13, 0.11, 0.22, 0.12, 0.10, 0.07]4
8[0.04, 0.06, 0.08, 0.07, 0.09, 0.31, 0.14, 0.13, 0.08]5
targets tensor is simply the index of the logit vector which should had the maximum value in probability list.

Important Observation

Cross entropy does not care whether the predicted token matches the target directly. Instead, it looks at how much probability the model assigned to the correct target token.

For example:

ContextPredictedCorrect TargetProbability of TargetLoss
hi my name isharshu ✅harshu0.291.238
my name is harshuwho ✅who0.311.171

Although the predictions are correct, the loss is not zero because the model is only 29% and 31% confident.

Likewise,

ContextPredictedCorrect TargetProbability of TargetLoss
hi myreally ❌name0.102.303

The model predicted the wrong token and assigned only 10% probability to the correct target, resulting in a much higher loss.