TensorTau PathToAGI Probability and Uncertainty Level 03

Level 03 · Probability and Uncertainty · Chapter 037

Cross-Entropy

Why is cross-entropy the natural loss when a model predicts probabilities?

1. The Classification Problem

A 3-class model outputs probabilities

Q=[0.7,0.2,0.1].Q=[0.7,0.2,0.1].

If the true class is class 1, target distribution is

P=[1,0,0].P=[1,0,0].

We need a loss that strongly rewards assigning probability to what actually happened and strongly punishes assigning it tiny probability.

Cross-entropy is

H(P,Q)=−∑iPilog⁡Qi.\boxed{H(P,Q)=-\sum_i P_i\log Q_i}.

For one-hot PP:

H(P,Q)=−log⁡Qtrue.H(P,Q)=-\log Q_{true}.

The entire loss becomes the negative log probability assigned to the correct class.


2. Why the Log?

If the model gives the true class probability:

0.9   → small loss
0.5   → moderate loss
0.01  → huge loss

Because

−log⁡q-\log q

rises sharply as q→0q\to0.

The logarithm also turns products of probabilities into sums, which connects classification loss to maximum likelihood.


3. Cross-Entropy = Negative Log-Likelihood

For labeled examples (xi,yi)(x_i,y_i), a classifier models

Pθ(yi∣xi).P_\theta(y_i\mid x_i).

Likelihood of independent observations:

∏iPθ(yi∣xi).\prod_i P_\theta(y_i\mid x_i).

Log likelihood:

∑ilog⁡Pθ(yi∣xi).\sum_i\log P_\theta(y_i\mid x_i).

Maximizing it equals minimizing

−∑ilog⁡Pθ(yi∣xi),-\sum_i\log P_\theta(y_i\mid x_i),

which is categorical cross-entropy.

So cross-entropy is not an arbitrary neural-network convention. It is maximum likelihood for categorical outcomes.


4. Softmax + Cross-Entropy

Logits ziz_i are unrestricted real numbers. Softmax converts them to probabilities:

qi=ezi∑jezj.q_i=\frac{e^{z_i}}{\sum_j e^{z_j}}.

For true class kk:

L=−log⁡qk.L=-\log q_k.

A beautiful derivative appears:

∂L∂zi=qi−yi.\boxed{\frac{\partial L}{\partial z_i}=q_i-y_i}.

The gradient is simply predicted probability minus target probability.

This clean gradient is one reason softmax and cross-entropy fit together so naturally.


5. Numerical Stability

Never implement large-logit softmax naively. e1000e^{1000} overflows.

Subtract the maximum logit:

softmax(z)=softmax(z−max⁡z).softmax(z)=softmax(z-\max z).

Libraries combine log_softmax and negative log-likelihood using the log-sum-exp trick.

In PyTorch:

import torch
import torch.nn.functional as F

logits = torch.tensor([[2.0, 1.0, -1.0]])
target = torch.tensor([0])
loss = F.cross_entropy(logits, target)

Pass logits, not already-softmaxed probabilities, to F.cross_entropy.


6. Binary Cross-Entropy

For binary target y∈{0,1}y\in\{0,1\} and predicted probability pp:

L=−[ylog⁡p+(1−y)log⁡(1−p)].L=-[y\log p+(1-y)\log(1-p)].

With a sigmoid logit, PyTorch’s numerically stable choice is binary_cross_entropy_with_logits.


7. Cross-Entropy vs Squared Error

Squared error can be used on probabilities, but it corresponds to a different statistical story and often gives less convenient classification gradients.

Cross-entropy directly asks:

How much probability did you assign to the observed class?

and follows from categorical maximum likelihood.

Choose losses from the probabilistic problem, not from habit.


8. Shape Check

Multiclass classification:

logits: [B, C]
target: [B]      # integer class indices
loss:   []       # default mean reduction

For B=32B=32, C=10C=10:

[32,10] → cross_entropy(target [32]) → scalar

9. Break It

Softmax twice

Applying softmax before F.cross_entropy changes the computation and harms numerical behavior.

Wrong axis

Softmax must normalize across classes for each example, not across the batch.

log(0)

Naive probability implementations can produce infinities. Work in logits/log-space when possible.

Confidence ≠ calibration

Low cross-entropy encourages probability on correct labels, but a trained model can still be miscalibrated.


10. Real-World Connection

Cross-entropy trains image classifiers, token predictors, language models and many discriminative systems. Next-token language-model loss is simply cross-entropy repeated across sequence positions.

For logits [B,T,V] and token targets [B,T], the model is solving a VV-class classification problem at every token position.


Mastery Check

  1. Derive one-hot cross-entropy from the general formula.
  2. Explain its connection to maximum likelihood.
  3. Why does tiny true-class probability receive huge loss?
  4. What is the gradient of softmax-cross-entropy with respect to logits?
  5. Why should PyTorch cross-entropy receive logits?
  6. How is next-token prediction a classification problem?

Level 3 Complete

We started with uncertainty and ended with the loss used to train modern classifiers and language models:

probability
→ conditional probability
→ Bayes
→ random variables
→ expectation
→ covariance
→ Gaussian
→ likelihood
→ MAP
→ entropy
→ KL divergence
→ cross-entropy

Next → Level 4: Classical Machine Learning. We now have enough mathematics to derive learning algorithms rather than merely call them.

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

Explain

Explain cross-entropy as a penalty for the probability assigned to what actually happened.

Calculate

For true class 0, calculate loss for predictions [0.9,0.05,0.05], [0.5,0.25,0.25], [0.01,0.49,0.50].

Predict

Before calculating, order those predictions from smallest to largest loss.

Derive

Starting from softmax and L=−log⁡qkL=-\log q_k, derive ∂L/∂zi=qi−yi\partial L/\partial z_i=q_i-y_i.

Break

Demonstrate numerical overflow with naive softmax on logits [1000,999,998], then fix it by subtracting the maximum.

Build

Implement stable log_softmax using log-sum-exp and use it to implement cross-entropy from logits.

Investigate

Compare squared error and cross-entropy on a binary classification toy problem. Plot loss and gradient as confidence becomes increasingly wrong.

Shape Check

For language-model logits [8,128,50000] and targets [8,128], explain what each axis means and how many classification decisions contribute to the loss.

Ready to Continue?

Level 3 is mastered when you can connect categorical probability → likelihood → negative log-likelihood → cross-entropy → softmax gradient.