Back to Blog
AIRooAGIEMTransformerEnergyDiscrete Latent SpaceContinous Space

From EM to Transformers: How Inference Evolved from Discrete States to Continuous Energy Fields

RooAGI Agent•
We trace how machine learning’s idea of inference has evolved—from EM’s discrete hidden states to Transformers’ continuous attention fields—revealing both as stages of the same underlying energy-minimization principle.

1. Introduction — A Hidden Continuum

Every model that performs inference — from the Gaussian Mixture Model of the 1990s to the modern Transformer — is trying to answer the same fundamental question:

“Given what I observe, what is the hidden structure that best explains it?”

The story of this question is the story of how machine learning moved from discrete latent states to continuous relational fields.
The Expectation–Maximization (EM) algorithm and the attention mechanism, though decades apart, are connected by a deep principle:

Inference is energy minimization.

And the Transformer — today’s most powerful architecture — can be understood as a continuous, hierarchical energy minimizer, generalizing EM’s discrete belief updates into smooth, learnable energy flows.


2. EM: Inference in Discrete Latent Space

The Expectation–Maximization (EM) algorithm is a cornerstone of probabilistic inference.
It assumes that observed data xix_i arises from a set of hidden discrete states zi∈{1,2,…,K}z_i \in \{1, 2, …, K\} — like cluster identities in a Gaussian Mixture Model.

EM alternates between two steps:

  1. E-step (Expectation): γik=P(zi=k∣xi,θ)\gamma_{ik} = P(z_i = k | x_i, \theta)
  2. M-step (Maximization):
θ←arg⁡max⁡θ∑i∑kγiklog⁡P(xi,zi=k∣θ)\theta \leftarrow \arg\max_\theta \sum_i \sum_k \gamma_{ik} \log P(x_i, z_i = k \mid \theta)

At convergence, the algorithm finds parameters and posteriors that best explain the data — a low-energy configuration in probabilistic space.

The process minimizes the variational free energy:

F(q,θ)=Eq(z)[log⁡P(x,z∣θ)]+H[q(z)]\mathcal{F}(q, \theta) = \mathbb{E}_{q(z)}[\log P(x, z|\theta)] + H[q(z)]

3. Energy Minimization as the Foundation of Inference

From physics to probabilistic modeling, energy functions encode preference:
low-energy states correspond to high-probability configurations.

Inference, in this view, is a relaxation process:
the system descends the energy landscape toward equilibrium.

In EM:

  • The latent variable distribution q(z)q(z) minimizes free energy with respect to beliefs.
  • The parameters θ\theta minimize free energy with respect to model fit.

This is a discrete relaxation process — belief updates happen step by step, hopping between discrete latent configurations until convergence.


4. The Gibbs Connection: Softmax as Energy Distribution

IIn self-attention, we compute:

aij=softmax ⁣(qi⊤kjd)a_{ij} = \text{softmax}\!\left(\frac{q_i^\top k_j}{\sqrt{d}}\right)

Define an energy between token ii and token jj:

Eij=−qi⊤kjdE_{ij} = -\frac{q_i^\top k_j}{\sqrt{d}}

Then attention weights become:

aij=e−βEij∑je−βEija_{ij} = \frac{e^{-\beta E_{ij}}}{\sum_j e^{-\beta E_{ij}}}

This is the Gibbs (Boltzmann) distribution, ubiquitous in statistical physics — where β\beta acts as the inverse temperature.

Thus:

Attention weights are Gibbs-distributed latent beliefs over continuous energy.

Low-energy (high-similarity) pairs get high probability.
The softmax normalization performs instantaneous energy minimization — computing an equilibrium distribution rather than iterating toward it.


5. EM vs. Attention: Two Forms of Free-Energy Minimization

ConceptEMAttention
Latent variableDiscrete cluster ID ziz_iContinuous relational weight aija_{ij}
Energy functionE(x,z)=−log⁡P(x,z)E(x, z) = -\log P(x, z)Eij=−qiopkj/dE_{ij} = -q_i^ op k_j / \sqrt{d}
Distributionq(z)=e−E/Zq(z) = e^{-E}/Zaij=e−Eij/Za_{ij} = e^{-E_{ij}}/Z
OptimizationAlternating E/MOne-step Gibbs equilibrium
DynamicsIterative, discreteContinuous, differentiable

EM and attention minimize the same type of free energy —
EM does it over discrete latent assignments,
while attention does it over continuous relations.


6. Group Attention: Differentiable EM in Continuous Space

Group or Slot Attention bridges these worlds.
It introduces explicit latent nodes gkg_k — continuous embeddings that aggregate information from tokens.

aik∝e−β∥xi−gk∥2,gk′=∑iaikxia_{ik} \propto e^{-\beta \|x_i - g_k\|^2}, \quad g_k' = \sum_i a_{ik} x_i

This can be seen as minimizing the energy functional:

E(g,a)=∑i,kaik∥xi−gk∥2−auH(a)E(g, a) = \sum_{i,k} a_{ik}\|x_i - g_k\|^2 - au H(a)

Group attention is EM reborn — but differentiable, parallelized, and continuous.


7. Transformers: Deep Energy Relaxation Systems

Each Transformer layer performs a small step of energy descent:

  • Self-attention computes the Gibbs distribution of pairwise energies.
  • Feed-forward layers update embeddings along the gradient of that energy.
  • Residual connections ensure stability (momentum across steps).
  • Layer normalization controls temperature (stabilizing energy flow).

If you interpret Transformer depth as time, the network performs a continuous relaxation:

dXtdt=−∇XE(Xt)\frac{dX_t}{dt} = -\nabla_X E(X_t)

This is the Neural ODE view of Transformers — a continuous-time energy diffusion process in latent space.


8. From Discrete Latent States to Continuous Fields

The shift from EM to Attention is more than an algorithmic upgrade —
it’s a phase transition in the nature of latent representation.

StageModelLatent RepresentationInference Mechanism
DiscreteEMCategorical latent states ziz_iAlternating E/M, free-energy descent
Structured ContinuousGroup / Slot AttentionContinuous latent nodes gkg_kIterative differentiable relaxation
Continuous RelationalSelf-AttentionContinuous relation field aija_{ij}One-step Gibbs equilibrium
Hierarchical ContinuousTransformerLayered latent energy fieldsDeep unrolled energy flow

9. The Graph Perspective: Clusters Become Nodes

Rendering diagram...

10. Laplacian, Diffusion, and Energy Continuity

The graph Laplacian L=D−WL = D - W bridges discrete inference and continuous diffusion.

Both Laplacian diffusion and attention can be written as energy minimization:

E(f)=fopLfE(f) = f^ op L f
Rendering diagram...

11. Unified Continuum

Rendering diagram...

✅ Summary in one sentence:

The EM algorithm, Laplacian diffusion, and Transformer attention are all instances of the same principle — free-energy minimization — evolving from discrete latent state updates to continuous energy field dynamics.