Score Candidates with Energy-Based Models
Many AI systems need to decide which candidate fits an input: which reply matches a conversation, which label fits an image, or which configuration is plausible. A common design makes the model output a probability directly. Energy-based models take a more general route: they assign each input-candidate pair a scalar energy, with lower values representing greater compatibility.
That simple change is useful because the model can focus on relative preference without requiring every architecture to produce a normalized probability during scoring. It also introduces real engineering challenges. Training needs informative alternatives, probability normalization can be expensive, and inference may require searching a large candidate space.
This article builds the energy-based model mental model from a tiny ranking example, then connects it to training, probabilistic interpretation, inference, and the cases where a simpler classifier is a better fit.
Start with compatibility instead of probability
Suppose a support system receives this message:
I was charged twice for the same order.It must choose one route:
billing
account
technicalAn energy function can score each pair:
E(message, billing) = 0.4
E(message, account) = 1.7
E(message, technical) = 2.3Lower energy means greater compatibility, so billing is the preferred candidate.
The absolute value 0.4 has no universal meaning. Another model could assign -8.2 to the same preferred pair and still make the same decision. What matters for this decision is the ordering:
E(message, billing) < E(message, account) < E(message, technical)This is the first useful mental model: an energy function defines a landscape over possible candidates. Good candidates should occupy low regions; poor candidates should sit higher.
An energy function is a scoring rule
Write the energy function as:
E_theta(x, y)Here x is an input, y is a candidate output, and theta represents trainable parameters. The architecture is not fixed by this notation. It could be a neural network that consumes both values, two encoders followed by a similarity calculation, or another differentiable scoring system.
For a finite candidate set, prediction can be as simple as:
y_hat = argmin_y E_theta(x, y)This rule does not require calibrated probabilities. If the application only needs the lowest-energy candidate among a small known set, relative scores can be sufficient.
That flexibility separates two concerns that are often bundled together:
scoring: how compatible are x and y?
normalization: how should scores map to probabilities?An energy-based system may need both, but it does not have to perform both at every stage.
Turn energies into probabilities when needed
An energy function can define a probability distribution through the Boltzmann form:
p_theta(y | x) = exp(-E_theta(x, y)) / Z_theta(x)where
Z_theta(x) = sum_y' exp(-E_theta(x, y'))for a discrete candidate space. Z_theta(x) is the partition function. It normalizes the positive scores so the probabilities sum to one.
Using the earlier energies:
billing: 0.4
account: 1.7
technical: 2.3first negate and exponentiate them:
exp(-0.4) ~= 0.6703
exp(-1.7) ~= 0.1827
exp(-2.3) ~= 0.1003The partition function is approximately:
Z ~= 0.9533so the normalized probabilities are approximately:
billing: 0.703
account: 0.192
technical: 0.105This example also reveals a familiar special case. If a classifier produces logits s_y and uses softmax, defining E(x, y) = -s_y gives the same conditional distribution. Energy-based modeling is therefore broader than a particular neural architecture; ordinary softmax classification can be expressed in energy terms.
The partition function becomes difficult at scale
Normalization is cheap when there are three labels. It becomes a serious issue when y ranges over millions of items, long sequences, images, or a continuous space.
To compute Z_theta(x) exactly, the system must sum or integrate over every possible candidate. That can be infeasible even when evaluating one candidate’s energy is cheap.
This distinction matters in system design. An unnormalized energy can be easy to compute while its corresponding normalized probability is expensive to obtain.
If the application only compares a supplied candidate set, exact global normalization may be unnecessary. A retrieval system, for example, can score a shortlist and select its minimum-energy item. But that local decision must not be presented as a probability over every possible item unless the required normalization has actually been performed or a justified approximation is used.
Training must shape the energy landscape
A useful energy function should place desirable pairs below undesirable ones. Consider a positive pair (x, y_pos) and an alternative (x, y_neg).
A simple margin objective is:
loss = max(0, m + E(x, y_pos) - E(x, y_neg))where m is a positive margin. If m = 1, E(x, y_pos) = 0.5, and E(x, y_neg) = 2.0, then:
loss = max(0, 1 + 0.5 - 2.0) = 0The positive pair is already at least one energy unit below the negative pair.
If the negative energy falls to 1.1:
loss = max(0, 1 + 0.5 - 1.1) = 0.4The objective now pushes the scores farther apart.
This teaching example is deliberately small. Production objectives vary with the model and task. Some systems use likelihood-based objectives, contrastive objectives, score matching, or other methods. The shared idea is that training must provide a signal that lowers energy in desired regions relative to competing regions.
Negative examples determine what the model must distinguish
Pairwise or contrastive training depends heavily on the alternatives shown to the model. If every negative candidate is obviously unrelated, the task can become too easy.
For the billing message, compare these alternatives:
positive: billing
weak negative: gardening
harder negative: accountA model that separates billing from gardening has not necessarily acquired the distinctions needed to separate two plausible support routes. Harder negatives can provide a more useful training signal because they expose boundaries the application actually cares about.
Hard negatives also carry risk. A candidate treated as negative may in fact be valid. In retrieval tasks this is often called a false negative. Pushing such a candidate to high energy teaches the model a contradiction in the data.
Negative selection therefore needs task knowledge. Useful checks include whether multiple outputs can be correct, whether labels are incomplete, and whether mined negatives are verified strongly enough for the intended use.
Inference depends on the shape of the output space
For three labels, inference can evaluate all three energies and take the minimum. For a million documents, exhaustive scoring may already be costly. For structured outputs such as long token sequences, the number of possible candidates grows combinatorially.
There is no single inference algorithm implied by the phrase energy-based model. The appropriate method depends on the output space and the energy function.
A system might use:
- exhaustive evaluation for a small discrete set;
- retrieval to produce a shortlist before energy scoring;
- iterative optimization when outputs are continuous and the energy is suitable for it;
- sampling methods when the application needs samples from a distribution rather than only a minimum-energy point.
Each choice changes latency and approximation error. A fast candidate generator can reduce scoring cost, but the final energy model cannot select an excellent candidate that never enters the shortlist. Search quality therefore becomes part of model quality.
Relative energy is not calibrated confidence
A common mistake is to treat a large energy gap as a calibrated confidence measure.
Suppose two candidates have energies:
A: -12
B: -2Candidate A is strongly preferred within that comparison. The gap alone does not establish that A has a 99% chance of being correct in the real application. Such a probability claim requires a defined probabilistic model, the relevant normalization, and empirical validation of how its probabilities behave on representative data.
Energy scales can also change under training choices or parameterization. Decisions based on a fixed threshold such as accept if energy < 0 need validation for the specific model rather than an assumption that zero has intrinsic significance.
When a downstream workflow needs confidence thresholds, abstention, or risk estimates, evaluate those properties directly on held-out data.
Avoid trivial low-energy solutions
If an objective only says “make positive energies small,” a model can satisfy it by assigning low energy to everything. That produces no useful discrimination.
The training objective must create relative structure: desired configurations need lower energy than relevant alternatives, or the objective must otherwise constrain the energy field. This is one reason negative examples, normalization terms, or gradient-based constraints appear in many energy-based methods.
A related mistake is evaluating only positive examples. A model can give every valid pair a low score and still fail because invalid pairs receive equally low scores. Evaluation should test the comparisons the deployed system will actually make.
Measure the complete decision pipeline
For a candidate-ranking application, model evaluation should reflect both scoring and search.
Suppose an energy model ranks the correct document first 95% of the time when that document is present in a 20-item candidate set. If the retrieval stage includes the correct document only 80% of the time, the complete system cannot reach 95% top-one success. The scorer never sees many of the missing answers.
Useful evaluation separates at least two questions:
candidate recall: does the candidate stage include a valid answer?
ranking quality: does the energy model prefer it when present?Latency should be measured end to end as well. A sophisticated energy function that requires expensive scoring for thousands of candidates may be inferior in production to a simpler model with a strong retrieval stage.
When energy-based models fit well
Energy-based modeling is especially natural when the task is fundamentally about compatibility and the output can be compared through a shared score. Examples include matching inputs to candidates, structured prediction, anomaly-style scoring, and systems that combine a proposal mechanism with a separate evaluator.
The framework is also useful conceptually when direct probability modeling forces an awkward normalization over a huge output space. Working with unnormalized scores can make the modeling target simpler, provided the training and inference procedures handle the missing normalization appropriately.
A simpler classifier is often preferable when the label set is small, fixed, and mutually exclusive, and a standard softmax model already meets the product requirements. Likewise, if calibrated probabilities are central to the decision and exact normalization is cheap, introducing a more general energy formulation may add complexity without practical benefit.
Treat scoring, search, and confidence as separate contracts
Energy-based models are easiest to reason about when three contracts stay distinct. The energy function scores compatibility. The inference procedure searches for useful low-energy candidates. Any probabilistic or confidence interpretation requires its own normalization and validation.
That separation is the practical takeaway. Start by stating the decision the system must make and the candidate space it must search. Then decide whether relative energy is enough, how training will expose meaningful alternatives, and whether the application truly needs normalized probabilities. Those choices determine whether an energy-based design is a clean solution or unnecessary machinery.