Lensa ML
Lensa ML

How Entropy & Cross-Entropy Work

Step 1 of 6

Surprise

Unlikely events carry more information

ProbabilitySurpriseSURPRISE1.00
Rare (0.01)Certain (1.0)

P(event)

0.50

Surprise

1.00 bits

Likely events are not very surprising — we already expected them.

Step 1 of 6: Surprise

Unlikely events carry more information

ProbabilitySurpriseSURPRISE1.00
Rare (0.01)Certain (1.0)

P(event)

0.50

Surprise

1.00 bits

Likely events are not very surprising — we already expected them.

Step 2 of 6: Entropy

The average surprise of a distribution

P(X)Entropy0.501.00
P(X) = 0P(X) = 1

H(X) = −Σ P(X)·log₂(P(X))

= −[0.50×log₂(0.50)+0.50×log₂(0.50)]
= −[-0.500+-0.500] = 1.000 bits

P(X)

0.50

1 − P(X)

0.50

ENTROPY

H(X) = 1.000

Maximum entropy! When both outcomes are equally likely, uncertainty is highest — 1 full bit.

Step 3 of 6: Distributions & Entropy

Shape determines uncertainty

A25%B25%C25%D25%2.000 bits
A: 25
B: 25
C: 25
D: 25

Entropy H

2.000 bits

Max H

2.000 bits

Near-uniform distribution — maximum uncertainty. All outcomes are equally likely.

Step 4 of 6: Cross-Entropy

Measuring the mismatch between distributions

Cat70%40%Dog20%30%Bird10%30%True (p)Predicted (q)
Adjust predicted distribution (q):
Cat q
Dog q
Bird q

H(p)

1.157 bits

H(p,q)

1.446 bits

KL gap

0.290 bits

Decent match — there is a small KL divergence gap. Cross-entropy equals entropy plus the KL divergence penalty.

Step 5 of 6: Cross-Entropy as Loss

The ML connection — why this is the loss function

Predicted ŷ (confidence)Loss1234−log(ŷ)gradient
Wrong (0.01)Correct (1.0)

ŷ

0.50

Loss

0.693 nats

∂L/∂ŷ

-2.0

The model is fairly wrong — loss is rising and the gradient pushes strongly to fix the prediction.

Step 6 of 6: Why Minimize Cross-Entropy

Watch training converge to the true distribution

EpochH(p,q)H(p)p vs q

Epoch

0

H(p,q)

1.589 bits

Accuracy

46.0%

Before training begins, the model predicts uniformly — cross-entropy starts high. Press Play or step through epochs.