Skip to main content

Why Your AI Model is Always Surprised: Entropy

Why Your AI Model is Always Surprised: Entropy Explained

Topic Overview

What is entropy? In information theory, entropy is the mathematical measure of uncertainty, unpredictability, or “average surprise” contained in a probability distribution. A distribution where one outcome is guaranteed has zero entropy. A distribution where all outcomes are equally likely has maximum entropy.

Why does it exist? Existing mathematics could measure the value of a signal, but not its information content. Engineers needed a way to quantify how much “data” a message actually contained, independent of its physical representation. Entropy was invented to measure the absolute limit of how much a signal could be compressed without losing information.

What problem does it solve? It solves the problem of quantifying uncertainty. In machine learning, we are constantly trying to reduce uncertainty. Without a metric for uncertainty, we cannot define what “learning” is. Entropy allows us to measure the uncertainty of our model’s predictions, the uncertainty of our data, and the difference between the two.

Why should engineers care? Every time you train a classification model, you are minimizing entropy. The “Softmax” function in your neural network is literally derived from the Maximum Entropy Principle. In Reinforcement Learning, entropy is the mathematical dial you turn to force an agent to explore rather than exploit. If you do not understand entropy, you do not understand why your model overfits, why your RL agent gets stuck, or how your loss function actually works.


Prerequisites

  • Probability Distributions: You must understand discrete probability mass functions (\(p(x)\)) and the fact that \(\sum p(x) = 1\). This is needed because entropy is a weighted average of the probabilities of all possible outcomes.
  • Logarithms: You must understand the \(\log\) function. This is needed because the mathematical definition of “surprise” relies entirely on the logarithmic scale.

Historical Motivation

In 1948, Claude Bell Labs engineer Claude Shannon was trying to solve a purely engineering problem: how to transmit messages over a noisy telegraph or telephone wire with the absolute minimum number of bits.

Existing methods sent fixed-length codes. If you have 4 possible messages, you send 2 bits, regardless of how likely each message is. Shannon realized this was wasteful. If a message is 99% likely to be “Hello”, you shouldn’t waste 2 bits sending it every time.

Shannon needed a mathematical formula that would measure the “information content” of a source. He realized that information is inversely related to probability. Rare events carry more information (“The stock market crashed!” is highly informative because it is rare). He needed a function that was large for small probabilities, zero for absolute certainty, and additive (the information of two independent events should equal the sum of their individual informations). The only function that satisfies these constraints is the negative logarithm of probability. He combined this into an average, calling the resulting quantity “Entropy” (borrowing the name from statistical mechanics, where it measures disorder).

In ML, we use Shannon’s entropy for the exact same reason: to measure the “disorder” or uncertainty of our model’s predictions, allowing us to compress it into confident, actionable decisions.


Intuitive Explanation

Imagine I have a trick coin that lands on Heads 100% of the time. If I flip it and tell you the result, you gain exactly zero information. You already knew it would be Heads. There is no surprise. Entropy = 0.

Now imagine a fair coin. It’s 50/50. Before I flip it, you have maximum uncertainty. When I tell you the result, you are completely surprised. Entropy is high.

Let’s scale this up. I have a 1000-sided die. If I roll it, the result could be any of 1000 equally likely numbers. The surprise is massive. Entropy is very high.

But what if the die is loaded, and 99% of the time it lands on 42? Most of the time, when I tell you the result, you aren’t surprised. The average surprise is low.

Entropy is simply the expected (average) surprise of a distribution. A peaked distribution (99% chance of one outcome) has low average surprise. A flat, uniform distribution (equal chance of all outcomes) has maximum average surprise because you can never confidently predict the outcome.


Visual Understanding

Draw a bar chart with \(K\) bars representing the probabilities of \(K\) different outcomes.

  1. Low Entropy (Peaked): One bar reaches up to 1.0. All other bars are at 0. The chart looks like a single spike. There is no spread.
  2. High Entropy (Flat): All \(K\) bars are exactly at \(1/K\). The chart looks like a perfectly flat rectangle. You cannot predict the outcome.
  3. Medium Entropy: A lumpy distribution where a few bars are moderately high, and the rest taper off.

Visually, entropy measures the “flatness” of the probability bar chart. It calculates the area under a specific curve derived from those bars: the curve of \(-p \log p\).


Mathematical Foundations

To formalize intuition, we first define the “surprise” (or self-information) of a single outcome \(x\): \[I(x) = -\log p(x)\] Why the negative log? * If \(p(x) = 1\) (certain), \(\log(1) = 0 \implies I(x) = 0\). (No surprise). * If \(p(x) \to 0\) (impossible), \(\log(p) \to -\infty \implies I(x) \to \infty\). (Infinite surprise). * Log base: In ML, we almost always use the natural log (\(\ln\), base \(e\)). The unit of information is then called a “nat”. In computer science, base 2 is used, and the unit is a “bit”.

Entropy is the expected value (average) of this surprise over all possible outcomes: \[H(X) = \mathbb{E}_{x \sim P}[I(x)] = -\sum_{x} p(x) \log p(x)\]

Symbol Breakdown: * \(H(X)\): The entropy of the random variable \(X\). * \(p(x)\): The probability of outcome \(x\). * \(-\): The negative sign is there because \(\log(p)\) for \(0 < p < 1\) is always negative. We want entropy to be a positive number measuring uncertainty, so we flip the sign. * \(\sum\): We sum over all possible discrete outcomes.

Properties & Proofs: 1. Non-negativity: \(H(X) \geq 0\). Since \(0 \leq p(x) \leq 1\), \(\log p(x)\) is \(\leq 0\). Multiplying by \(-p(x)\) makes it \(\geq 0\). Summing non-negative numbers yields a non-negative number. 2. Maximum Entropy: The maximum possible entropy for \(K\) categories is \(\log K\). This happens when the distribution is perfectly uniform: \(p(x) = 1/K\) for all \(x\). * Proof: If \(p(x) = 1/K\), then \(H = -\sum (1/K) \log(1/K) = -K \cdot (1/K) \log(1/K) = -\log(1/K) = \log(K)\). 3. Minimum Entropy: \(H(X) = 0\). This happens when one outcome has probability 1 and all others have 0.


Worked Numerical Example

Let’s calculate the entropy of a biased coin. \(P(\text{Heads}) = 0.8\), \(P(\text{Tails}) = 0.2\).

\[H(X) = - [ (0.8 \log 0.8) + (0.2 \log 0.2) ]\]

Using natural log (\(\ln\)): 1. \(\ln(0.8) \approx -0.2231\) 2. \(\ln(0.2) \approx -1.6094\)

\[H(X) = - [ (0.8 \cdot -0.2231) + (0.2 \cdot -1.6094) ]\] \[H(X) = - [ -0.17848 - 0.32188 ]\] \[H(X) = 0.50036 \text{ nats}\]

Comparison: * A perfectly fair coin (0.5, 0.5): \(H = \ln(2) \approx 0.693\) nats. * Our biased coin (0.8, 0.2): \(H \approx 0.500\) nats. * A deterministic coin (1.0, 0.0): \(H = 0\) nats.

The biased coin has lower entropy than the fair coin because we are less uncertain about the outcome.


Computational Interpretation

What is the computer actually doing?

  1. Input: An array of probabilities [0.8, 0.2].
  2. Log Step: The CPU/GPU computes the element-wise natural log: [-0.2231, -1.6094].
  3. Multiply Step: Element-wise multiplication of the original probabilities by the logs: [0.8 * -0.2231, 0.2 * -1.6094] = [-0.1785, -0.3219].
  4. Sum Step: Sum the array: -0.5004.
  5. Negate Step: Multiply by -1: 0.5004.

Complexity & Bottlenecks: * Time Complexity: \(O(K)\) where \(K\) is the number of categories. The bottleneck is the log operation, which is mathematically expensive on hardware (often requiring polynomial approximation or lookup tables). * Memory Cost: \(O(K)\) for the input and intermediate buffers. For a language model with a vocabulary of 50,000 tokens, calculating the entropy of the output distribution requires allocating arrays of size 50,000.


Implementation Perspective

NumPy (From Scratch)

import numpy as np

# Probabilities of 3 outcomes
p = np.array([0.5, 0.3, 0.2])

# 1. Filter out zeros to avoid log(0) = NaN (or use built-in handling)
# 2. Compute p * log(p)
# 3. Sum and negate
entropy = -np.sum(p * np.log(p)) 

print(f"Entropy: {entropy:.4f} nats")
# Max entropy for 3 classes is ln(3) ~ 1.098. 
# Our result will be less than that.

PyTorch (Production ML)

In PyTorch, we usually deal with batches. A model outputs a batch of probability distributions.

import torch
import torch.nn.functional as F

# Batch of 2 samples, 4 classes
logits = torch.tensor([[2.0, 1.0, 0.1, -0.5], 
                       [1.0, 1.0, 1.0, 1.0]])

# Convert to probabilities using Softmax
probs = F.softmax(logits, dim=-1) # Shape: [2, 4]

# Compute log probabilities (PyTorch handles zeros safely here)
log_probs = torch.log(probs)

# Entropy = -sum(p * log_p) over the class dimension (dim=1)
entropy = -torch.sum(probs * log_probs, dim=1) # Shape: [2]

print(f"Entropy per sample: {entropy}")
# Sample 1 has lower entropy (peaked). Sample 2 has max entropy (ln(4) ~ 1.386).

Where It Appears In AI

AI Component Exact Usage of Entropy
Reinforcement Learning (SAC) Entropy is added to the reward: \(R_{total} = R_{env} + \alpha H(\pi)\). The agent is explicitly paid to keep its policy distribution uncertain, forcing exploration.
Classification Networks The Softmax output layer represents the model’s confidence. A good model outputs low-entropy (peaked) distributions for known inputs and high-entropy (flat) for unknown inputs.
Decision Trees (C4.5) Splits are chosen based on Information Gain: \(IG = H(\text{Parent}) - H(\text{Children})\). The algorithm seeks to reduce entropy the most.
Transformers (LLMs) The Maximum Entropy Principle proves that if you constrain a model to only output expected values (logits), the most unbiased estimate of the actual distribution is the Softmax function.
Variational Autoencoders The latent space prior is set to a standard Gaussian \(\mathcal{N}(0, 1)\), which is the maximum entropy distribution for a given variance.

Deep Dive Into Real Models

1. Transformers & LLMs (Softmax & Temperature) The logits output by an LLM are unbounded. Why do we use Softmax (\(e^{z_i} / \sum e^{z_j}\)) to turn them into probabilities? Because of the Maximum Entropy Principle. If we assume the model’s logit represents the log-odds of a class, Softmax is the mathematically correct, maximum-entropy distribution that satisfies those constraints without making any other assumptions. When we apply “Temperature” (\(T\)) during sampling, we are directly manipulating entropy: \(p_i = \text{softmax}(z_i / T)\). High \(T\) flattens the distribution (increases entropy, more creative). Low \(T\) sharpens it (decreases entropy, more deterministic).

2. Reinforcement Learning (Soft Actor-Critic) Standard RL (like DQN) suffers from premature convergence; the agent finds a decent action and repeats it forever, never finding the optimal action. SAC solves this by maximizing an entropy-regularized objective: \[J(\theta) = \sum_{t} \mathbb{E}_{(s_t, a_t) \sim \rho_\pi} \left[ r(s_t, a_t) + \alpha H(\pi(\cdot|s_t)) \right]\] The term \(\alpha H(\pi)\) means the agent is penalized for being too certain. It forces the policy to maintain a spread of probabilities over actions, ensuring it keeps exploring. \(\alpha\) is the entropy trade-off dial.

3. Decision Trees (Information Gain) When building a tree to classify spam vs. not-spam, you look for a feature (e.g., “contains ‘Viagra’”). You calculate the entropy of the dataset before the split. Then you calculate the weighted entropy of the datasets after the split. The difference is Information Gain. You greedily split on the feature that maximizes entropy reduction.


Failure Modes

1. The \(\log(0)\) NaN Bomb * Cause: If a neural network outputs a probability of exactly \(0.0\) for an outcome, and you compute \(-0 \cdot \log(0)\), the code evaluates to \(0 \cdot (-\infty)\), which mathematically evaluates to NaN in floating-point arithmetic. * Symptoms: The training loss becomes NaN and all weights turn to NaN. The model dies instantly. * Solution: Always add a tiny epsilon: p * np.log(p + 1e-12). Or use PyTorch’s F.log_softmax which handles this safely under the hood via fused kernels.

2. Confusing Discrete and Differential Entropy * Cause: Applying the discrete formula \(-\sum p \log p\) to continuous densities (PDFs). Differential entropy \(h(X) = -\int p(x) \log p(x) dx\) does not share the same properties. A continuous distribution can have negative differential entropy, whereas discrete entropy is always \(\geq 0\). * Symptoms: Baffled debugging when an entropy calculation returns a negative number for a Gaussian distribution. * Solution: Always verify if your probabilities are discrete mass (sums to 1) or continuous density (integrates to 1). For continuous data, bin it first, or use KL divergence instead.

3. Entropy of the Wrong Dimension * Cause: In PyTorch, passing a batch of predictions [Batch, Classes] to an entropy function without specifying the dimension. The code computes the entropy of the entire flattened array, rather than the entropy of each prediction. * Symptoms: You get a single scalar instead of a vector of [Batch] entropies. Your RL loss is completely wrong. * Solution: Explicitly specify dim=-1 when summing over class probabilities.


Engineering Insights

Fused Kernels for Log-Softmax: Computing probs = softmax(logits) and then log_probs = log(probs) is numerically unstable and slow. If logits are large, exp(logits) overflows. Modern frameworks use LogSoftmax, a fused mathematical operation that computes the log of the sum in a stable way without ever materializing the intermediate exp array. If you are computing entropy manually, always start from log_softmax.

Precision Limitations: Calculating entropy in FP16 (half precision) is risky. The log of a very small probability (e.g., \(10^{-6}\)) is around \(-13.8\). Multiplying this by \(10^{-6}\) yields a very small number that can underflow to zero in FP16. Entropy calculations in mixed-precision training are often kept in FP32 to prevent underestimating the true entropy.

Information Bottleneck on GPU: If you are calculating the entropy of a massive distribution (e.g., over a 100,000-token vocabulary for an LLM), the operation is entirely memory-bandwidth bound. You are loading huge arrays just to do element-wise math. Grouping entropy calculations into larger batched matrix operations is critical for GPU utilization.


Interview Questions

Beginner: Q: What is entropy in the context of machine learning, and what is its maximum value for a 5-class classification problem? A: Entropy measures the uncertainty of a model’s prediction distribution. For a 5-class problem, the maximum entropy is \(\ln(5) \approx 1.609\) nats, which occurs when the model predicts an equal 20% probability for all 5 classes.

Intermediate: Q: Why do we use the logarithm in the entropy formula instead of, say, \(1/p(x)\) to measure surprise? A: Because information needs to be additive. If two independent events happen, the probability is \(p_1 \cdot p_2\). The surprise should be the sum of individual surprises. \(\log(p_1 \cdot p_2) = \log(p_1) + \log(p_2)\). The function \(1/p\) does not satisfy this additive property, meaning it cannot be used to build an expectation of information over independent events.

Advanced: Q: In Soft Actor-Critic (SAC), how does the entropy temperature parameter \(\alpha\) affect the optimal policy, and what happens at the extremes \(\alpha \to 0\) and \(\alpha \to \infty\)? A: \(\alpha\) controls the trade-off between exploitation (reward) and exploration (entropy). If \(\alpha \to 0\), the objective becomes purely reward-seeking, and SAC degenerates into standard deterministic Q-learning, risking premature convergence to a local optimum. If \(\alpha \to \infty\), the reward is ignored entirely, and the optimal policy becomes the uniform random distribution (maximum entropy), meaning the agent explores forever and never accomplishes the task.

Research-level: Q: The Maximum Entropy Principle states we should choose the distribution with the highest entropy consistent with our constraints. How does this principle mathematically justify the Softmax function in neural networks? A: If we use a linear layer to output expected values (logits) \(z_i\), and we constrain our distribution \(p\) such that the expected value of certain features matches the logits, the principle says we should assume nothing else. Using Lagrange multipliers to maximize \(-\sum p_i \log p_i\) subject to \(\sum p_i = 1\) and feature constraints yields the exponential family. The solution is \(p_i \propto e^{z_i}\), which is exactly the Softmax function.


Research Perspective

Current Limitations: Shannon entropy measures the syntactic uncertainty of a distribution, but not the semantic uncertainty. An LLM might output a distribution with low entropy over the next token, but the chosen token might be factually wrong. Entropy cannot distinguish between a confident truth and a confident hallucination.

Open Problems: How do we measure the entropy of a generative model’s output space? If a diffusion model generates a face, what is the entropy of that image? Calculating the differential entropy of high-dimensional continuous spaces is an open, ill-posed problem (curse of dimensionality).

Modern Improvements: Approximate Entropy (ApEn) and Sample Entropy are used in time-series analysis to measure complexity without needing a defined probability distribution. In LLM research, “Semantic Entropy” is a new frontier: researchers sample multiple sentences from an LLM, use a separate NLP model to cluster them by meaning, and then calculate the entropy of the meanings rather than the tokens.


Connections

  • Prerequisites: Probability Distributions (defines \(p(x)\)).
  • Enables: Cross-Entropy Loss (\(H(p, q) = -\sum p \log q\)), KL Divergence (\(D_{KL}(p||q) = H(p,q) - H(p)\)), Mutual Information (\(I(X;Y) = H(X) - H(X|Y)\)).
  • Used by: Softmax (derived via Max Entropy), Decision Trees (Information Gain), RL (SAC entropy regularization).
  • Extended by: Rényi Entropy (generalizes Shannon entropy, used in adversarial training), Fisher Information (curvature of entropy).
  • Related concepts: Thermodynamic Entropy (physics origin), Gini Impurity (a non-logarithmic measure of disorder used in CART trees).

Final Mental Model

Think of entropy as the “Flatness Meter.”

Imagine your model’s output probabilities as a pile of sand on a table. * If the sand is piled into a single, towering spike, the flatness is zero. You are certain. Entropy = 0. * If the sand is scraped perfectly flat across the whole table, the flatness is maximum. You have no idea where the answer lies. Entropy = Max.

When you train a classifier, your goal is to take a flat pile of sand and shape it into a spike. When you train an RL agent to explore, you are actively forbidding the pile from turning into a spike, forcing it to stay somewhat flat.

Comments

Popular posts from this blog

Why Every AI Model is Actually Bayesian

Why Every AI Model is Actually Bayesian Topic Overview Standard probability asks: “What is the chance of this event happening in the entire universe?” Conditional probability asks: “Given that I already know this to be true, what is the chance of that happening?” Bayes’ Theorem is the engineering tool that flips this condition around. It answers the most critical question in machine learning: “Given the data I just observed, what is the probability that my hypothesis is true?” Why does it exist? Because in the real world, we usually know \(P(\text{Data}|\text{Hypothesis})\) —how likely a certain observation is under a specific model—but what we actually want is \(P(\text{Hypothesis}|\text{Data})\) —how likely our model is to be correct given the observation. Bayes’ Theorem is the mathematical bridge between the data we generate and the truths we want to infer. What problem does it solve? It provides a rigorous, mathematically sound mechani...

Why Everything in AI is a Vector ?

Why Everything in AI is a Vector ? Topic Overview In the previous lesson, you learned about the scalar: a single number representing magnitude. But reality is rarely one-dimensional. A single pixel has three color channels (red, green, blue). A word’s meaning has dozens of grammatical and semantic features. A model’s parameters number in the millions. To represent entities that possess multiple attributes simultaneously, we need a mathematical object that holds multiple scalars in a specific order. That object is a vector. A vector \(\mathbf{v} \in \mathbb{R}^n\) is an ordered list of \(n\) scalars. It represents a point, or an arrow from the origin, in \(n\) -dimensional space. Why does it exist? Because we need a mathematical language for things that have more than one degree of freedom. A scalar tells you “how hot.” A vector tells you “where you are” and “which way you’re going.” What problem does it solve? It provides the fundamental d...

Why Your PyTorch Code Keeps Breaking (It’s the Scalars)

Why Your PyTorch Code Keeps Breaking (It’s the Scalars) Topic Overview You are about to learn about the most fundamental object in all of mathematics and machine learning: the scalar. A scalar is a single number. That is the entire definition. But do not confuse simplicity with insignificance. Every neural network, no matter how many billions of parameters it contains, is ultimately optimized by reading and writing scalars. The loss value is a scalar. The learning rate is a scalar. The temperature parameter in softmax is a scalar. The scale factor in attention is a scalar. Every gradient clipping decision is based on a scalar. The final prediction of a classification model is a scalar probability. Why does the scalar exist as a named concept? Because not all numbers are created equal. Some numbers exist as components of larger structures—a coordinate in a vector, an entry in a matrix, a pixel in an image. A scalar is a number that stands alone...