Cross-Entropy Loss
The loss that trains almost every classifier and language model. It measures how surprised the model was by the right answer: if it put high probability on the true class, the loss is near 0; if it was confidently wrong, the loss explodes. No numpy — just a logarithm.
The problem
Implement cross-entropy loss for a single example. You are given a predicted probability distribution p (a list that sums to ~1) and a target. The target is either an integer class index or a one-hot list. Return the negative log-probability the model assigned to the true class: -log(p[target]) for an index, or -Σ tᵢ·log(pᵢ) for a one-hot target. Guard the logarithm with a tiny eps so a probability of 0 gives a large finite loss instead of -inf.
p=[0.0, 1.0, 0.0], target=10.0p=[0.25, 0.25, 0.25, 0.25], target=0≈1.3863p=[0.999, 0.0005, 0.0005], target=1≈7.6- For an integer
target, the loss is-log(p[target]). - For a one-hot list
target, the loss is-Σ tᵢ·log(pᵢ). - Clamp each probability to at least
eps = 1e-12before taking its log, solog(0)never happens. - Pyodide has no numpy — use the
mathstdlib and plain lists only.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement cross_entropy(p, target, eps=1e-12). Return the negative log-probability of the true class — accepting either an integer index or a one-hot list as the target.
- The whole loss for an integer target is one line: -log(p[target]).
- Protect the log with max(p[target], eps) so a zero probability gives a big finite number, not -inf.
- If the target is a list (one-hot), sum -tᵢ·log(pᵢ) over all classes — only the tᵢ=1 term survives.
- Perfect confidence (p on the true class = 1) gives log(1) = 0; a uniform guess over K classes gives log(K).
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.