TL;DR
The cross-entropy method (CEM) searches for the input that maximises a score function when you cannot compute its gradient. It keeps a probability distribution over inputs, usually a Gaussian. Each round it draws many candidates from that distribution, keeps the best few, and refits the Gaussian so it matches those elite candidates. The distribution moves toward good regions and narrows as it converges.
It needs only the ability to evaluate the score, so it works on black boxes such as simulators, games and learned models.
Where you'll see it: in planning with a learned world model, where it picks action sequences that a model predicts will do well, as in PlaNet, and in policy search and black-box tuning of controllers.
Intuition
Hunting with a net that shrinks
Suppose you are looking for the highest point of a hilly field in fog. You cannot see the slope, but you can measure the height where you stand. One strategy: throw a handful of stones across a wide area, note where the ones that landed highest are, then throw the next handful around that spot, with a smaller spread. Repeat. The throws drift toward the peak and tighten as you go.
That is CEM. The throws are samples from a Gaussian, the "keep the best few" step is the elite selection, and "refit the Gaussian" moves its centre to the average of the elites and sets its width to their spread. The method needs no derivatives, which matters when the score comes from a physics engine or a neural network whose gradient is painful to use.
A single Gaussian can only describe one bump of good answers. If the good answers sit in two separate peaks, CEM tends to settle on one of them. Keep that in mind for the tradeoffs.
flowchart LR A[Fit a Gaussian to the current belief] --> B[Sample N candidates] B --> C[Score each candidate] C --> D[Keep the top K elites] D --> E[Refit mean and variance to the elites] E --> A
- start: mean 0, sigma 2
- after one update: mean 2.25, sigma 0.25
The math
The problem
We want to maximise a score function over vectors :
We cannot differentiate , so instead we work with a family of distributions indexed by parameters . For CEM the family is the multivariate Gaussian with a diagonal covariance, so and
Here is coordinate of , is its mean, and its standard deviation. The product form says the coordinates are independent under , which is the diagonal-covariance case from the Gaussian lesson.
The ideal target distribution
Pick a threshold and let be the set of samples with score at least . Define the distribution that puts all its weight on that level set:
The indicator is 1 when its condition holds and 0 otherwise. The denominator is the probability, under , of landing above the threshold, and it normalises to total 1. If is close to the best score, concentrates near the optimum.
We would like to look like . The natural measure of mismatch is the KL divergence, and the cross-entropy is closely related:
Because , and does not depend on , minimising cross-entropy over is the same as minimising the KL divergence. That is where the name comes from.
Turning the target into samples
We cannot compute the expectation under directly, but we can draw from and keep the samples that pass the threshold. Those kept samples are distributed as . So the expectation becomes an average over the elites:
where is the number of elites. This is exactly the maximum likelihood estimate of from the elite samples. CEM is "fit the model to the best samples, then repeat".
The Gaussian update in closed form
Write out the log density for the diagonal Gaussian:
Set the derivative with respect to to zero and the derivative with respect to to zero. The solution is
So the new mean is the average of the elites and the new variance is their average squared spread. Note the divisor , not : this is the maximum likelihood version of the variance.
The algorithm
- Initialise to a guess and to a wide spread.
- Draw samples from .
- Score each sample with .
- Sort by score and keep the top samples as elites, for some elite fraction such as .
- Set and to the elite sample mean and standard deviation, as above.
- Repeat from step 2 until the spread is small or the budget runs out. Return the best sample seen.
The threshold is never chosen by hand. It is the score of the -th best sample in the current batch, so the method adapts it automatically.
Worked examples
Example 1: one update by hand
Maximise on the line, starting from and . Draw six samples. These are chosen by hand to make the arithmetic checkable; they are not random draws.
- Samples and scores: , , , , , .
- Elite fraction , so . The two best scores are (at ) and (at ). The threshold is .
- New mean: .
- New variance: , so .
- Interpretation: the Gaussian moved from centre to centre , toward the true optimum at , and its width dropped from to . With only two elites the estimate is noisy, which is why real runs use far more samples.
Example 2: a seeded run in two dimensions
Maximise , whose optimum is . Start from mean and . Use samples per round and elites. I ran a 30-line Python script with random seed 0 and recorded the first three rounds:
| round | elite threshold | best score | mean | sigma |
|---|---|---|---|---|
| 1 | -1.813 | -0.041 | (0.895, -2.011) | (0.554, 0.699) |
| 2 | -0.046 | -0.003 | (1.047, -1.978) | (0.108, 0.116) |
| 3 | -0.002 | -0.000 | (1.002, -2.006) | (0.023, 0.019) |
- The elite threshold climbs from to in three rounds. The best sample is within about of the optimum after round 1, since .
- The spread shrinks by roughly a factor of five per round in the early rounds: in the first coordinate.
- By round 5 the sigmas are at most and the mean is to three decimals. Further rounds change nothing. Running them longer buys nothing because the distribution has collapsed onto the answer.
- Interpretation: the method found the optimum with 300 score evaluations. A grid search at similar resolution would need far more.
Tradeoffs and failure modes
CEM is simple and robust when the search space is low to moderate dimensional and the score is cheap to evaluate. It fails in predictable ways.
| situation | what goes wrong | better choice |
|---|---|---|
| Score has two or more separate good regions | A single Gaussian covers both poorly and collapses onto one | Mixture of Gaussians, or several restarts |
| Spread collapses too early | Sigma reaches almost zero on a poor answer; the search stops exploring | Add a small noise floor to sigma each round, or smooth the update with the previous parameters |
| Very high dimensions | With a fixed sample budget, the elite set is poorly estimated | Gradient methods such as [[evolution-strategies |
| Score is cheap and gradients are available | CEM wastes evaluations | Ordinary [[gradient-descent |
| Score is an inaccurate model | CEM finds inputs that exploit model errors | Ensembles or uncertainty-aware scoring, as in [[model-predictive-control |
The main practical knob is the elite fraction . Small focuses hard on the top samples and risks collapse. Large averages in mediocre samples and converges slowly. Values around to are common defaults.
History
Reuven Rubinstein developed the method in the late 1990s. It began as an adaptive importance sampling procedure for estimating the probability of rare events in simulations, where the goal was to pick a sampling distribution close to the ideal one. The key observation was that this selection step could be written as a cross-entropy minimisation, and that it also worked as an optimiser. Rubinstein and Dirk Kroese collected the theory and many applications in a 2004 book.
It later spread to machine learning, including policy search in reinforcement learning. It caught on in planning with learned models because it needs nothing but the ability to score candidate plans. The PlaNet paper used it to pick action sequences in a learned latent space, which is why it appears among that paper's prerequisites.
Check yourself
QuestionWhat is the elite set in one round of CEM, and what does it contribute to the update?Show answer
The elite set is the top samples by score in the current batch. The update fits the Gaussian to them by maximum likelihood, so the new mean and variance describe where the good samples were. The rest of the batch is discarded.
QuestionScores for five samples are with . With elite fraction , what is the new mean?Show answer
, so the elites are the two largest samples, and . The new mean is . The new variance is , so the new sigma is .
QuestionWhy does minimising cross-entropy against the elite-weighted distribution give the same answer as maximum likelihood on the elites?Show answer
The expectation under is approximated by averaging over the samples that passed the threshold, because those samples are distributed as . Maximising the average log likelihood of those samples is exactly the maximum likelihood problem on the elites. Cross-entropy and KL differ only by a term that does not depend on the new parameters, so the minimiser is the same.
QuestionYou run CEM on a robot-arm plan. After four rounds the sigmas are near zero but the best score is poor. What happened, and what would you change?Show answer
The distribution collapsed onto a poor region before finding a good one. The usual cause is too small an elite fraction or too few samples per round, so early rounds were not exploratory enough. Remedies include a larger sample count, a larger elite fraction, a minimum sigma, or restarts from several initial means.
Sources
- Rubinstein and Kroese, The Cross-Entropy Method (Springer, 2004)
- Cross-entropy method (Wikipedia)
- Learning Latent Dynamics for Planning from Pixels (Hafner et al., 2018)