Mathematical Foundations

Information Theory for Artificial Intelligence

Information Theory for Artificial Intelligence | Lesson 4 — Free Guide
Lesson 4 · AI Engineering Foundations

Information Theory for Artificial Intelligence

Cross-entropy loss, Softmax, KL in VAEs, and perplexity in LLMs all come from one idea: how surprised should you be by an event? This intermediate lesson builds entropy, divergences, and mutual information into the language of modern neural networks.

14 topics
Intermediate
Equations + diagrams
LLMs & VAEs
00 Overview

Why Information Theory Matters in AI

Information theory, founded by Claude Shannon (1948), quantifies uncertainty and information content. In AI, almost every probabilistic objective is an information-theoretic quantity in disguise: classification uses cross-entropy, generative models minimize KL terms, transformers are scored with perplexity, and representation learning often maximizes mutual information.

Entropy — How unpredictable is this distribution?
Cross-entropy — How well does model \(q\) match true \(p\)?
KL divergence — Extra surprise from using \(q\) instead of \(p\)
Mutual information — How much does \(X\) tell me about \(Y\)?
True p(y|x) labels / data Model q_θ(y|x) Softmax logits H(p,q) / KL(p‖q) training objective Training = make q close to p in an information-theoretic sense
Supervised learning as matching distributions: minimize cross-entropy (or KL) between labels and Softmax outputs
Prerequisites

You should know discrete probability, logarithms, expectations, and the idea of a loss from Lesson 3. Linear algebra from Lesson 2 helps when Softmax acts on logit vectors.

Where you already use it

  • Training classifiersCrossEntropyLoss is \(H(p,q)\) with one-hot \(p\)
  • Language models — next-token NLL and perplexity are entropy rates
  • VAEs / diffusion — KL terms keep latents near a prior
  • Distillation & RLHF — teacher–student or policy–reference KL
Concept (this lesson)Typical AI artifact
Self-informationPer-token / per-class \(-\log q\)
Entropy \(H(p)\)Label uncertainty; irreducible CE floor
Cross-entropyClassification & LM training loss
KL divergenceVAE ELBO, distillation, RLHF penalty
Mutual informationContrastive SSL, IB-style reps
PerplexityLLM leaderboard / eval metric
01 Foundations

Self-Information (Surprise)

The self-information (or surprisal) of an outcome \(x\) with probability \(p(x)\) answers: how surprising was this event?

Definition

\[I(x) = -\log p(x)\]

Rare events (small \(p\)) → large \(I\). Certain events (\(p=1\)) → \(I=0\).

Why the negative log?

  • Independent events multiply in probability → their informations add (logs turn products into sums)
  • Monotone: less probable ⇒ more information
  • Matches coding length: optimal codeword length for \(x\) is about \(-\log_2 p(x)\) bits
p(x) I(x) rare → high surprise common → low
Self-information falls as probability rises — the curve is \(-\log p\)
Worked example

Fair coin: \(p(H)=0.5\), \(I(H)=1\) bit. Biased coin \(p(H)=0.9\): \(I(H)\approx 0.15\) bits — heads is almost expected. A tokenizer emitting a rare token with \(p=2^{-10}\) contributes 10 bits of surprise to the sequence score.

AI connection

Negative log-likelihood (NLL) of a dataset is the sum of self-informations under the model. Training a classifier or LM is literally minimizing total surprise on the training data.

02 Foundations

Entropy

Entropy is the expected self-information — the average surprise of drawing from distribution \(p\).

Shannon entropy (discrete)

\[H(p) = H(X) = -\sum_x p(x)\,\log p(x) = \mathbb{E}_{x\sim p}\bigl[-\log p(x)\bigr]\]

Continuous analogue (differential entropy): \(h(X)=-\int p(x)\log p(x)\,dx\).

Key properties

  • \(H(X)\ge 0\) for discrete \(X\); \(H=0\) iff deterministic
  • Maximum entropy for a fixed alphabet: uniform distribution
  • For binary: \(H(p)=-p\log p-(1-p)\log(1-p)\) (binary entropy), max at \(p=0.5\)
p (Bernoulli) H(p) max at 0.5
Binary entropy peaks at a fair coin — maximum uncertainty
Worked example

Three-class label prior \(p=(0.7,0.2,0.1)\): \(H(p)\approx 1.16\) bits (base 2). Uniform \(p=(1/3,1/3,1/3)\): \(H=\log_2 3\approx 1.58\) bits — harder to predict a priori.

AI connection

Label entropy sets a lower bound on cross-entropy loss: even a perfect model cannot beat \(H(p)\) on average. Class imbalance (low entropy) makes accuracy look easy while minority classes remain hard.

Entropy of Softmax outputs

During training, the entropy of \(q_\theta(y|x)\) often starts near \(\log K\) (almost uniform) and falls as the model becomes confident. Monitoring prediction entropy is a cheap diagnostic for overconfidence and calibration.

High \(H(q)\) — uncertain / underfit predictions
Low \(H(q)\) — peaked Softmax (may be wrong but sure)
Label smoothing — prevents \(H(q)\to 0\) collapse
Temperature — rescales entropy without changing ranking
03 Foundations

Joint & Conditional Entropy

For two random variables, we ask how much uncertainty remains in one after observing the other.

Definitions

Joint entropy: \(H(X,Y)=-\sum_{x,y}p(x,y)\log p(x,y)\)

Conditional entropy:

\[H(Y\mid X)=\sum_x p(x)\,H(Y\mid X=x)=-\sum_{x,y}p(x,y)\log p(y\mid x)\]

Chain rule: \(H(X,Y)=H(X)+H(Y\mid X)=H(Y)+H(X\mid Y)\)

H(Y|X)=0 — Y is a deterministic function of X
H(Y|X)=H(Y) — X and Y are independent
H(X,Y) ≤ H(X)+H(Y) — equality under independence
H(Y|X) ≤ H(Y) — conditioning never increases entropy
H(X|Y) H(Y|X) I(X;Y) H(X,Y) = H(X)+H(Y|X)
Venn picture: joint entropy covers both circles; overlap is mutual information
AI connection

A supervised model aims to drive \(H(Y\mid X)\) down — after seeing features \(X\), labels \(Y\) should be predictable. Conditional entropy of next-token given context is exactly what language models minimize.

04 Divergences

Cross-Entropy

Cross-entropy \(H(p,q)\) is the expected surprise when events come from \(p\) but you score them with \(q\).

Definition

\[H(p,q) = -\sum_x p(x)\,\log q(x) = \mathbb{E}_{x\sim p}\bigl[-\log q(x)\bigr]\]

Fundamental identity:

\[H(p,q) = H(p) + D_{\mathrm{KL}}(p\,\|\,q)\]

Why it dominates ML

When \(p\) is a one-hot label \(y\), cross-entropy collapses to \(-\log q(y)\) — the familiar Softmax loss. Minimizing \(H(p,q)\) over model parameters is equivalent to minimizing KL when \(p\) is fixed (because \(H(p)\) does not depend on \(\theta\)).

QuantityDepends onRole in training
\(H(p)\)data onlyConstant — ignore for optimization
\(H(p,q_\theta)\)data + modelStandard loss to minimize
\(D_{\mathrm{KL}}(p\|q_\theta)\)data + modelSame optimum as CE
Worked example

True class = cat. Model Softmax: \(q(\text{cat})=0.6\), \(q(\text{dog})=0.3\), \(q(\text{bird})=0.1\). CE loss \(=-\log 0.6\approx 0.511\) nats. If \(q(\text{cat})=0.95\), loss \(\approx 0.051\) — much less surprise.

AI connection

nn.CrossEntropyLoss in PyTorch combines LogSoftmax + NLL. Label smoothing replaces hard \(p\) with a softer target, which changes \(H(p)\) and can improve calibration.

05 Divergences

KL Divergence

The Kullback–Leibler divergence measures how one distribution diverges from another — the expected extra surprise from using \(q\) when the truth is \(p\).

Definition

\[D_{\mathrm{KL}}(p\,\|\,q) = \sum_x p(x)\log\dfrac{p(x)}{q(x)} = H(p,q)-H(p)\]

Properties: \(D_{\mathrm{KL}}\ge 0\), equals 0 iff \(p=q\) almost everywhere; not symmetric and does not satisfy the triangle inequality (not a metric).

p q KL(p‖q) ≠ KL(q‖p) Mode-seeking vs mass-covering depends on direction
KL is asymmetric: forward and reverse KL behave differently in variational inference

Forward vs reverse KL (intuition)

  • \(D_{\mathrm{KL}}(p\|q)\) — zero-forcing / mass-covering when fitting \(q\) to \(p\)
  • \(D_{\mathrm{KL}}(q\|p)\) — mode-seeking (typical in mean-field VI)
AI connection

VAE training maximizes the ELBO, which includes \(-D_{\mathrm{KL}}(q_\phi(z|x)\|p(z))\). Knowledge distillation uses KL between teacher and student Softmax. RLHF / preference models often use KL penalties against a reference policy.

Worked example

Let \(p=(0.5,0.5)\) and \(q=(0.9,0.1)\). Then \(D_{\mathrm{KL}}(p\|q)=0.5\log(0.5/0.9)+0.5\log(0.5/0.1)\approx 0.511\) nats, while \(D_{\mathrm{KL}}(q\|p)=0.9\log(0.9/0.5)+0.1\log(0.1/0.5)\approx 0.369\) nats — same pair, different numbers.

Continuous KL (Gaussian sketch)

For \(q=\mathcal{N}(\mu,\sigma^2)\) and \(p=\mathcal{N}(0,1)\):

\[D_{\mathrm{KL}}(q\|p)=\tfrac{1}{2}\bigl(\mu^2+\sigma^2-1-\log\sigma^2\bigr)\]

This closed form is why diagonal-Gaussian VAEs are popular — the KL term is cheap and exact.

06 Dependence

Mutual Information

Mutual information \(I(X;Y)\) measures how much knowing \(X\) reduces uncertainty about \(Y\) (and vice versa).

Equivalent forms

\[ \begin{aligned} I(X;Y) &= H(X)-H(X\mid Y) = H(Y)-H(Y\mid X)\\ &= H(X)+H(Y)-H(X,Y)\\ &= D_{\mathrm{KL}}\bigl(p(x,y)\,\|\,p(x)p(y)\bigr) \end{aligned} \]

I(X;Y) ≥ 0 — zero iff X ⊥ Y
I(X;X)=H(X) — self-information equals entropy
Symmetric — I(X;Y)=I(Y;X)
Data processing — I(X;Z) ≤ I(X;Y) for Markov X→Y→Z
X (input) Z = encoder representation Y (label) max I(Z;Y) min I(Z;X)
Information bottleneck sketch: keep predictive information about Y, discard irrelevant bits of X
AI connection

Contrastive learning (InfoNCE) lower-bounds mutual information between views. Feature selection and IB theories phrase "good representations" as high \(I(Z;Y)\) with constrained \(I(Z;X)\).

07 Softmax

Softmax & the Maximum-Entropy Principle

Given logits \(z\in\mathbb{R}^K\), Softmax produces a distribution over classes. It is not arbitrary: it is the maximum-entropy distribution consistent with those scores as expected energy constraints.

Softmax

\[q_i = \dfrac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}}\qquad i=1,\ldots,K\]

Equivalently, \(q = \arg\max_{r\in\Delta} \bigl\{ H(r) + \langle z, r\rangle \bigr\}\) over the probability simplex.

Temperature

With temperature \(T>0\): \(q_i \propto \exp(z_i/T)\). As \(T\to 0\), \(q\) approaches one-hot (argmax). As \(T\to\infty\), \(q\) approaches uniform. Distillation and sampling both exploit \(T\).

Worked example

Logits \(z=(2.0, 1.0, 0.1)\). Softmax ≈ \((0.659, 0.242, 0.099)\). With \(T=2\): softer ≈ \((0.506, 0.307, 0.187)\).

AI connection

Attention weights are Softmax over query–key scores. Gumbel-Softmax / concrete distributions extend this idea for discrete latent variables. Numerically, subtract \(\max z_i\) before exp for stability.

ComponentRole of Softmax
Classifier headMap logits → class probabilities
Transformer attentionMap scores → mixing weights over keys
LM headMap vocab logits → next-token distribution
Mixture / gatingSoft assignment over experts or components
Invariant to shift — Softmax\((z+c)=\mathrm{Softmax}(z)\)
Not invariant to scale — Softmax\((\alpha z)\) changes sharpness
Max-ent — least assumptions given mean energy \(\langle z,q\rangle\)
LogSumExp — \(\log\sum_j e^{z_j}\) is the soft-max potential
08 Language models

Perplexity for LLMs

Perplexity turns average NLL into an interpretable "effective branching factor" — how many equally likely choices the model feels it has at each step.

Definitions

For a sequence \(w_1,\ldots,w_N\) under model \(q\):

\[\mathrm{PPL} = \exp\Bigl(\tfrac{1}{N}\sum_{t=1}^{N}-\log q(w_t\mid w_{<t})\Bigr) = e^{H(p,q)}\]

(Using natural log → PPL in "nats-as-exp"; with \(\log_2\), report \(2^{H}\) for bit-based perplexity.)

Lower perplexity → sharper next-token predictions PPL ≈ vocab size uniform / weak PPL tens–hundreds typical LM range PPL → 1 near-deterministic
Perplexity as geometric mean of inverse predictive probabilities
AI connection

Compare PPL only under the same tokenizer and evaluation set. Bits-per-byte (BPB) is preferred when vocabularies differ. PPL correlates with likelihood but not always with human preference — hence RLHF and win-rate metrics.

09 Units

Bits vs Nats

The base of the logarithm sets the unit of information.

BaseUnitCommon in
\(\log_2\)bit (shannon)Coding theory, compression
\(\ln = \log_e\)natML libraries, continuous math
\(\log_{10}\)hartley / ditRare in AI
Conversion

\[1\ \mathrm{nat} = \log_2 e \approx 1.4427\ \mathrm{bits}\qquad 1\ \mathrm{bit} = \ln 2 \approx 0.6931\ \mathrm{nats}\]

Worked example

PyTorch CE loss reports nats. A loss of \(0.693\) nats ≈ \(1\) bit ≈ random binary guess. Multiply NLL by \(\log_2 e\) to convert to bits before computing bit-based perplexity \(2^{\mathrm{NLL}_{\mathrm{bits}}}\).

AI connection

Papers sometimes write \(\log\) without a base — in information theory that usually means \(\log_2\); in ML it usually means \(\ln\). Always check the surrounding equations.

10 Derivation

Deriving Cross-Entropy Loss from MLE

Assume i.i.d. samples \((x^{(i)}, y^{(i)})\) and a categorical model \(q_\theta(y|x)=\mathrm{Softmax}(f_\theta(x))\). Maximum likelihood maximization is equivalent to minimizing cross-entropy.

Step-by-step

Likelihood: \(\mathcal{L}(\theta)=\prod_i q_\theta(y^{(i)}|x^{(i)})\)

Log-likelihood: \(\ell(\theta)=\sum_i \log q_\theta(y^{(i)}|x^{(i)})\)

Minimize NLL / empirical CE:

\[J(\theta)= -\dfrac{1}{m}\sum_{i=1}^{m}\log q_\theta(y^{(i)}|x^{(i)}) = \widehat{H}(p_{\mathrm{data}}, q_\theta)\]

For one-hot \(y\): \(J=-\log q_{y}\). Softmax+CE gradient w.r.t. logits is simply \(q - y\) (derived fully in Lesson 6).

Binary case (sigmoid)

For two classes with \(q=\sigma(z)=1/(1+e^{-z})\) and label \(y\in\{0,1\}\), CE becomes binary cross-entropy:

BCE

\[L = -\bigl(y\log q + (1-y)\log(1-q)\bigr)\]

This is still \(H(p,q)\) with \(p=\mathrm{Bernoulli}(y)\). Multiclass Softmax CE is the natural generalization.

Worked example

Batch of 2: labels \([1,0]\), model probs \([0.8, 0.3]\). Mean CE \(= \tfrac{1}{2}\bigl(-\log 0.8 - \log(1-0.3)\bigr)\approx 0.290\) nats. Gradient on the probability vector pulls mass toward the true class.

AI connection

This is why "training with CE" and "doing MLE under a Softmax model" are the same statement. Changing the output family (Gaussian, Bernoulli) changes the matching NLL — MSE for Gaussian mean estimation is also an MLE story.

11 Representations

Information Bottleneck Intuition

The information bottleneck (IB) formalizes compression of \(X\) into representation \(Z\) that remains predictive of \(Y\).

IB objective

\[\min_{p(z|x)} \; I(X;Z) - \beta\, I(Z;Y)\]

Small \(I(X;Z)\): discard irrelevant detail. Large \(I(Z;Y)\): keep what predicts the target. \(\beta\) trades off compression vs prediction.

Deep learning reading

  • Early layers may retain more \(I(X;Z)\); later layers become more task-specific
  • Dropout, bottlenecks, and sparse codes act as soft information constraints
  • IB is a lens, not always a literal training loss — estimating MI is hard in high dimensions
AI connection

Variational IB replaces MI terms with tractable bounds (similar spirit to VAEs). In transformers, the residual stream carries high-capacity information; attention and MLPs selectively route task-relevant bits.

12 Practice

Applications: Classification, LMs, and VAEs

SettingInformation quantityWhat you optimize / report
ClassificationCross-entropy \(H(p,q)\)CE / NLL loss
Language modelsToken NLL → perplexityPPL, BPB
VAEsELBO = recon − KLReconstruction + \(D_{\mathrm{KL}}(q\|p)\)
DistillationKL(teacher ‖ student)Soft targets at temperature \(T\)
Contrastive SSLMI lower bound (InfoNCE)Contrastive loss
RLHFKL to reference policyReward − \(\beta\) KL
Classifier min H(y, Softmax) = MLE LLM min Σ −log q(w_t|·) report PPL VAE max ELBO recon − KL
Three workhorses of modern AI — all information-theoretic at the core
VAE ELBO sketch

\(\log p(x) \ge \mathbb{E}_{q(z|x)}[\log p(x|z)] - D_{\mathrm{KL}}(q(z|x)\|p(z))\). The KL term is an information penalty keeping the approximate posterior near the prior.

Reading a training log through information theory

CE falling — model \(q\) matches labels better
Train CE ≪ val CE — overfitting (memorizing \(p_{\mathrm{train}}\))
KL rising in VAE — posterior drifting from prior (check \(\beta\))
PPL stuck high — weak context model or data mismatch
Transformers note

A decoder-only LM is a huge conditional distribution \(q(w_t|w_{<t})\). Causal masking enforces the chain rule of probability — exactly the expansion behind sequence cross-entropy and perplexity.

13 Wrap-up

Summary

Information theory gives a precise language for uncertainty and prediction error in AI systems.

Self-information — \(-\log p(x)\)
Entropy — average surprise \(H(p)\)
Cross-entropy — training loss \(H(p,q)\)
KL — \(H(p,q)-H(p)\), VAE / distill / RLHF
Mutual information — shared information \(I(X;Y)\)
Perplexity — exp(average NLL) for LMs
Next lesson

You now know what we minimize (information-theoretic losses). Lesson 5 covers how we minimize it efficiently: SGD, momentum, Adam, schedules, and regularization. Then Lesson 6 derives the gradients themselves.

Frequently Asked Questions

Why is information theory important for AI?
Information theory defines how we measure uncertainty and surprise. Cross-entropy is the standard classification loss, KL divergence appears in VAEs and distillation, mutual information guides representation learning, and perplexity is the headline metric for language models.
What is the difference between entropy and cross-entropy?
Entropy H(p) measures the uncertainty of a true distribution p. Cross-entropy H(p,q) measures the expected code length when encoding samples from p using a model designed for q. Cross-entropy equals entropy plus KL(p‖q).
Do I need Lessons 1–3 before this one?
Yes. This intermediate lesson assumes probability, logarithms, and basic calculus from Lessons 1–3. It builds directly on discrete distributions and loss functions from Lesson 3.
Is this lesson free?
Yes. The full lesson is free and available in Arabic and English with no registration required.
Why do frameworks report loss in nats instead of bits?
Autograd and continuous calculus use the natural logarithm. Converting to bits is a constant factor (\(1/\ln 2\)). Report bits when comparing compression or when a paper specifies bit-based perplexity.
Is minimizing cross-entropy the same as minimizing KL?
When the data distribution \(p\) is fixed, yes — they differ by the constant \(H(p)\). If you change the target distribution (label smoothing, soft distillation targets), you are changing \(p\) and both \(H(p)\) and KL move.