BACKPROP LAB
01TITLE

Cross-Entropy Loss

DIFFICULTY 1/38 MIN02 · Probability & Entropy
UNSEEN
02EQUATION
CRISP KATEX // NO DISTORTION

LCE(p,q)=−∑i=1Vpilog⁡qi\mathcal{L}_{\text{CE}}(p, q) = -\sum_{i=1}^{V} p_i \log q_i

When the target pp is a one-hot vector concentrated on token kk, this collapses to the negative log-likelihood LCE=−log⁡qk\mathcal{L}_{\text{CE}} = -\log q_k.

03WHAT IT MEANS

Cross-entropy measures the average number of bits (or nats, using ln⁡\ln) needed to encode events from a true distribution pp when your model predicts probabilities qq. Whenever qq puts high mass on the true next token, the loss drops toward zero; confident wrong predictions are penalized heavily.

04WHERE A MODEL USES IT

An autoregressive LLM evaluates cross-entropy at every vocabulary prediction step during pre-training and supervised fine-tuning.

05WORKED EXAMPLE

Suppose vocabulary size is V=3V = 3, the true next token is class 11 (p=[1,0,0]p = [1, 0, 0]), and softmax outputs q=[0.7,0.2,0.1]q = [0.7, 0.2, 0.1]:

LCE=−(1⋅ln⁡(0.7)+0⋅ln⁡(0.2)+0⋅ln⁡(0.1))=−ln⁡(0.7)≈0.3567 nats\mathcal{L}_{\text{CE}} = -\left(1 \cdot \ln(0.7) + 0 \cdot \ln(0.2) + 0 \cdot \ln(0.1)\right) = -\ln(0.7) \approx 0.3567 \text{ nats}

06RUN IT
PYTHON 3 + NUMPY // LAZY WEBASSEMBLY WORKER

CODE RUNS ONLY IN YOUR BROWSER VIA PYODIDE WEBASSEMBLY — NOTHING IS SENT TO ANY SERVER.

07SOURCES
08CHECK YOURSELF

QUESTION 1 / 3

0 / 3 ANSWERED

Q1 · MCQ

For a one-hot target distribution where class k has probability 1, what does multi-class cross-entropy reduce to?

09NEXT UP