Goal-Conditioned Transition Modeling with Energy-Based Models

Goal-Conditioned Transition Modeling with Energy-Based Models

This note describes a goal-conditioned transition model with two prediction modes:

  1. Action-conditioned prediction: an action is known or sampled, so the model predicts a specific next latent state.
  2. Action-free prediction: no action is given, so the model represents a distribution over possible next latent states with an energy function.

The main idea is that the action-free model should not directly output a probability vector over arbitrary future states. Instead, it should assign an energy to any candidate next latent state. Lower energy means the candidate is more plausible.


1. Setup and Notation

Given a current observation $x$ and goal $g$, define a goal-conditioned latent state:

\[s = F_\psi(x,g),\]

where:

For the next observation $x’$, define the target next latent state:

\[s'^+ = F_{\bar{\psi}}(x',g).\]

$F_{\bar{\psi}}$ can be:

In practice, a detached or EMA target encoder is often safer because it can reduce representation collapse.


2. Model Components

2.1 Action-Conditioned Predictor

If an action is available or sampled, define:

\[a = H_\eta(s,\epsilon),\]

where $\epsilon$ is noise if $H_\eta$ is stochastic, for example when $H_\eta$ is a diffusion model or another generative action sampler.

The action-conditioned predictor outputs one specific next latent state:

\[\hat{s}' = G_\phi(s,a).\]

This corresponds to modeling:

\[p(s'|s,a).\]

2.2 Action-Free Energy Model

If no action is given, the model should represent the distribution over possible next latent states:

\[p(s'|s).\]

An energy-based model represents this distribution indirectly:

\[E_\theta(s,\tilde{s}') \in \mathbb{R},\]

where $\tilde{s}’$ is a candidate next latent state.

The conditional distribution is:

\[p_\theta(\tilde{s}'|s) = \frac{\exp(-E_\theta(s,\tilde{s}'))}{Z_\theta(s)},\]

with partition function:

\[Z_\theta(s) = \int \exp(-E_\theta(s,u))\,du.\]

The EBM is queried with both the current latent $s$ and a candidate next latent $\tilde{s}’$. It outputs a scalar energy:

\[G_{\text{EBM}}(s,\tilde{s}') = E_\theta(s,\tilde{s}').\]

Low energy means $\tilde{s}’$ is a plausible next latent state from $s$. High energy means it is unlikely.

A simple implementation is:

\[E_\theta(s,\tilde{s}') = \operatorname{MLP}_\theta([s,\tilde{s}']),\]

where $[s,\tilde{s}’]$ denotes concatenation.


3. Relationship Between the Two Modes

When the action is unknown, the true action-free transition distribution can be viewed as the action-marginal distribution:

\[p(s'|s) = \int p(s'|s,a)p(a|s)\,da.\]

This means that many actions may be possible from $s$, and each action may lead to a different next state. If the action is unobserved, the next-state distribution is a mixture over possible actions.

Explicitly computing the marginal would require modeling both:

\[p(a|s)\]

and:

\[p(s'|s,a).\]

The EBM avoids this explicit decomposition. It learns the final compatibility function directly from observed transition data:

\[(s_t,s_{t+1}).\]

At optimum, the energy can be interpreted approximately as:

\[E_\theta(s,s') \approx -\log p_{\text{data}}(s'|s) + C(s),\]

where $C(s)$ is an arbitrary function of $s$.

For a fixed $s$, $C(s)$ does not affect optimization over $s’$, so:

\[\arg\min_{\tilde{s}'} E_\theta(s,\tilde{s}')\]

corresponds to finding a likely next latent state.


4. Exact EBM Likelihood

For one positive transition pair:

\[(s,s'^+),\]

the negative log-likelihood is:

\[\mathcal{L}_{\text{NLL}} = -\log p_\theta(s'^+|s).\]

Using the EBM definition:

\[\log p_\theta(s'^+|s) = -E_\theta(s,s'^+) - \log Z_\theta(s).\]

Therefore:

\[\mathcal{L}_{\text{NLL}} = E_\theta(s,s'^+) + \log Z_\theta(s).\]

This objective has two forces:

4.1 Gradient of the Exact Likelihood

Differentiate the exact loss:

\[\nabla_\theta \mathcal{L}_{\text{NLL}} = \nabla_\theta E_\theta(s,s'^+) + \nabla_\theta \log Z_\theta(s).\]

Because:

\[Z_\theta(s)=\int \exp(-E_\theta(s,u))\,du,\]

we have:

\[\nabla_\theta \log Z_\theta(s) = - \mathbb{E}_{u\sim p_\theta(\cdot|s)} \left[ \nabla_\theta E_\theta(s,u) \right].\]

Thus:

\[\boxed{ \nabla_\theta \mathcal{L}_{\text{NLL}} = \nabla_\theta E_\theta(s,s'^+) - \mathbb{E}_{u\sim p_\theta(\cdot|s)} \left[ \nabla_\theta E_\theta(s,u) \right] }\]

This is the classical EBM learning rule:

The expectation over $u\sim p_\theta(\cdot s)$ is usually hard to compute, so practical training often uses contrastive negatives, MCMC negatives, or in-batch negatives.

5. Contrastive Training with In-Batch Negatives

Given a batch of $B$ transitions:

\[(s_i,s_i'^+), \quad i=1,\dots,B,\]

define pairwise energies:

\[e_{ij}=E_\theta(s_i,s_j'^+).\]

For each $s_i$, the positive target is $s_i’^+$. The negatives are the other batch targets:

\[s_j'^+, \quad j\neq i.\]

Define logits:

\[\ell_{ij} = -\frac{e_{ij}}{\tau},\]

where $\tau>0$ is a temperature.

The contrastive probability is:

\[q_{ij} = \frac{\exp(\ell_{ij})}{\sum_{k=1}^B \exp(\ell_{ik})} = \frac{\exp(-E_\theta(s_i,s_j'^+)/\tau)} {\sum_{k=1}^B \exp(-E_\theta(s_i,s_k'^+)/\tau)}.\]

The loss for sample $i$ is:

\[\mathcal{L}_i = -\log q_{ii}.\]

Equivalently:

\[\mathcal{L}_i = - \log \frac{ \exp(-E_\theta(s_i,s_i'^+)/\tau) }{ \sum_{j=1}^B \exp(-E_\theta(s_i,s_j'^+)/\tau) }.\]

The batch loss is:

\[\mathcal{L}_{\text{NCE}} = \frac{1}{B}\sum_{i=1}^B \mathcal{L}_i.\]

This is a practical starting point for training the conditional EBM.

5.1 Gradient of the Contrastive Loss

Recall:

\[\ell_{ij} = -\frac{e_{ij}}{\tau},\]

and:

\[q_{ij} = \operatorname{softmax}(\ell_i)_j.\]

The standard cross-entropy derivative is:

\[\frac{\partial \mathcal{L}_i}{\partial \ell_{ij}} = q_{ij} - \mathbf{1}[j=i].\]

Since:

\[\frac{\partial \ell_{ij}}{\partial e_{ij}} = -\frac{1}{\tau},\]

we obtain:

\[\boxed{ \frac{\partial \mathcal{L}_i}{\partial e_{ij}} = \frac{1}{\tau} \left( \mathbf{1}[j=i] - q_{ij} \right) }\]

For the positive pair $j=i$:

\[\frac{\partial \mathcal{L}_i}{\partial e_{ii}} = \frac{1}{\tau}(1-q_{ii}) > 0.\]

Under gradient descent, this lowers the positive energy $e_{ii}$.

For a negative pair $j\neq i$:

\[\frac{\partial \mathcal{L}_i}{\partial e_{ij}} = -\frac{1}{\tau}q_{ij} < 0.\]

Under gradient descent, this raises the negative energy $e_{ij}$.

The parameter gradient is:

\[\boxed{ \nabla_\theta \mathcal{L}_i = \sum_{j=1}^B \frac{1}{\tau} \left( \mathbf{1}[j=i] - q_{ij} \right) \nabla_\theta E_\theta(s_i,s_j'^+) }\]

This is the finite-negative version of the exact EBM gradient.


6. Inference with the Action-Free EBM

Once $E_\theta(s,\tilde{s}’)$ is trained, there are two main inference modes.

6.1 MAP Prediction

If you want one likely next latent state, solve:

\[\hat{s}' = \arg\min_{\tilde{s}'} E_\theta(s,\tilde{s}').\]

This can be optimized using gradient descent:

\[\tilde{s}'_{k+1} = \tilde{s}'_k - \alpha \nabla_{\tilde{s}'}E_\theta(s,\tilde{s}'_k).\]

This produces a likely next latent state, usually one mode of the learned distribution.

6.2 Sampling Diverse Futures

If you want diverse samples from:

\[p_\theta(s'|s) \propto \exp(-E_\theta(s,s')),\]

you can use Langevin dynamics:

\[\tilde{s}'_{k+1} = \tilde{s}'_k - \frac{\alpha}{2} \nabla_{\tilde{s}'}E_\theta(s,\tilde{s}'_k) + \sqrt{\alpha}\epsilon_k,\]

where:

\[\epsilon_k \sim \mathcal{N}(0,I).\]

Without the noise term, this becomes pure energy minimization.

6.3 Useful Derivative for Optimization and Sampling

For fixed $s$:

\[\log p_\theta(\tilde{s}'|s) = -E_\theta(s,\tilde{s}') - \log Z_\theta(s).\]

Because $Z_\theta(s)$ does not depend on $\tilde{s}’$:

\[\boxed{ \nabla_{\tilde{s}'} \log p_\theta(\tilde{s}'|s) = - \nabla_{\tilde{s}'}E_\theta(s,\tilde{s}') }\]

and:

\[\boxed{ \nabla_{\tilde{s}'} [-\log p_\theta(\tilde{s}'|s)] = \nabla_{\tilde{s}'}E_\theta(s,\tilde{s}') }\]

7. Action-Conditioned Losses

When an action is available or sampled:

\[a = H_\eta(s,\epsilon),\]

and:

\[\hat{s}' = G_\phi(s,a).\]

If a ground-truth next latent $s’^+$ is available, use a prediction loss:

\[\mathcal{L}_{\text{pred}} = \frac{1}{2} \|G_\phi(s,a)-s'^+\|^2.\]

Equivalently:

\[\mathcal{L}_{\text{pred}} = \frac{1}{2} \|\hat{s}'-s'^+\|^2.\]

The derivative with respect to $\hat{s}’$ is:

\[\boxed{ \nabla_{\hat{s}'} \mathcal{L}_{\text{pred}} = \hat{s}' - s'^+ }\]

The parameter gradient for $\phi$ is:

\[\boxed{ \nabla_\phi \mathcal{L}_{\text{pred}} = (\hat{s}'-s'^+)^\top \nabla_\phi G_\phi(s,a) }\]

where $\nabla_\phi G_\phi$ is the Jacobian of the predictor output with respect to its parameters.


8. Compatibility Between the Predictor and the EBM

Because $G_\phi(s,a)$ produces a candidate next latent state, you can encourage it to lie in a low-energy region of the action-free EBM:

\[\mathcal{L}_{\text{compat}} = E_\theta(s,G_\phi(s,a)).\]

Let:

\[\hat{s}' = G_\phi(s,a).\]

Then:

\[\mathcal{L}_{\text{compat}} = E_\theta(s,\hat{s}').\]

The derivative with respect to $\theta$ is:

\[\boxed{ \nabla_\theta \mathcal{L}_{\text{compat}} = \nabla_\theta E_\theta(s,\hat{s}') }\]

The derivative with respect to $\hat{s}’$ is:

\[\boxed{ \nabla_{\hat{s}'} \mathcal{L}_{\text{compat}} = \nabla_{\hat{s}'} E_\theta(s,\hat{s}') }\]

The derivative with respect to predictor parameters $\phi$ is:

\[\boxed{ \nabla_\phi \mathcal{L}_{\text{compat}} = \left[ \nabla_{\hat{s}'}E_\theta(s,\hat{s}') \right]^\top \nabla_\phi G_\phi(s,a) }\]

If the action is generated by:

\[a = H_\eta(s,\epsilon),\]

then the derivative with respect to $\eta$ is:

\[\boxed{ \nabla_\eta \mathcal{L}_{\text{compat}} = \left[ \nabla_{\hat{s}'}E_\theta(s,\hat{s}') \right]^\top \nabla_a G_\phi(s,a) \nabla_\eta H_\eta(s,\epsilon) }\]

This means the action sampler can be trained to generate actions whose predicted next states have low energy.


9. Full Training Objective

A reasonable full objective is:

\[\mathcal{L} = \mathcal{L}_{\text{EBM}} + \lambda_{\text{pred}}\mathcal{L}_{\text{pred}} + \lambda_{\text{compat}}\mathcal{L}_{\text{compat}} + \lambda_{\text{reg}}\mathcal{L}_{\text{reg}}.\]

where:

A practical contrastive EBM loss is:

\[\mathcal{L}_{\text{EBM}} = - \frac{1}{B} \sum_{i=1}^B \log \frac{ \exp(-E_\theta(s_i,s_i'^+)/\tau) }{ \sum_{j=1}^B \exp(-E_\theta(s_i,s_j'^+)/\tau) }.\]

10. Negative Sampling Options

For the contrastive EBM loss, useful negative candidates $\tilde{s}’^-$ include:

Source Construction Notes
In-batch negatives For $s_i$, use $s_j’^+$ with $j\neq i$ Simple and strong baseline
Same trajectory, wrong time For $s_t$, use $s_{t+k}$ with $k\neq 1$ Harder negatives because they are temporally close
Same goal, different state Use next latents from transitions with the same goal but different current states Forces the model to respect the current state, not only the goal
Same state, wrong goal Use $F(x_{t+1},g_{\text{wrong}})$ Helps preserve goal conditioning
Model-generated negatives Sample $a_k \sim H(s,\epsilon_k)$, then use $\tilde{s}’k=G\phi(s,a_k)$ Useful but may include valid alternative futures, so soft negatives may be better
MCMC negatives Sample from the current EBM using Langevin dynamics Closer to classical EBM training

11. Minimal Implementation Sketch

# x, g, x_next: batch data
# F_enc: encoder
# F_target: target encoder
# E_model: energy model
# G_act: action-conditioned predictor
# H: action sampler

import torch
import torch.nn.functional as F

s = F_enc(x, g)                         # [B, d]
s_next = F_target(x_next, g).detach()   # [B, d]

B, d = s.shape

# ------------------------------------------------------------
# EBM contrastive loss
# energies[i, j] = E_model(s_i, s_next_j)
# ------------------------------------------------------------

s_i = s[:, None, :].expand(B, B, d)
sp_j = s_next[None, :, :].expand(B, B, d)

energies = E_model(s_i, sp_j)           # [B, B]
logits = -energies / tau

labels = torch.arange(B, device=s.device)
loss_ebm = F.cross_entropy(logits, labels)

# ------------------------------------------------------------
# Action-conditioned mode
# ------------------------------------------------------------

eps = torch.randn(B, noise_dim, device=s.device)
a = H(s, eps)
s_pred = G_act(s, a)

loss_pred = 0.5 * ((s_pred - s_next) ** 2).sum(dim=-1).mean()

# ------------------------------------------------------------
# Compatibility with EBM
# ------------------------------------------------------------

loss_compat = E_model(s, s_pred).mean()

# ------------------------------------------------------------
# Full loss
# ------------------------------------------------------------

loss = loss_ebm + lambda_pred * loss_pred + lambda_compat * loss_compat
loss.backward()

Start with the simplest stable version:

  1. Encode the current latent:

    \[s = F(x,g).\]
  2. Encode the target next latent:

    \[s'^+ = F_{\bar{\psi}}(x',g).\]
  3. Train the action-free EBM:

    \[E_\theta(s,\tilde{s}').\]
  4. Use in-batch negatives:

    \[E_\theta(s_i,s_j'^+), \quad j\neq i.\]
  5. Train with a contrastive softmax loss.

  6. For no-action MAP prediction, optimize:

    \[\hat{s}' = \arg\min_{\tilde{s}'} E_\theta(s,\tilde{s}').\]
  7. For diverse no-action futures, sample with Langevin dynamics:

    \[\tilde{s}'_{k+1} = \tilde{s}'_k - \frac{\alpha}{2} \nabla_{\tilde{s}'}E_\theta(s,\tilde{s}'_k) + \sqrt{\alpha}\epsilon_k.\]

13. Key Takeaways

Do not write the action-free model as:

\[G(s) \rightarrow p(s')\]

if this suggests a direct finite probability output over arbitrary next latent states.

Instead, write it as:

\[G_{\text{EBM}}(s,\tilde{s}') \rightarrow E_\theta(s,\tilde{s}').\]

The distribution is implicit:

\[p_\theta(s'|s) \propto \exp(-E_\theta(s,s')).\]

The two prediction modes are:

Mode Input Output Meaning
Action-conditioned $(s,a)$ $\hat{s}’=G_\phi(s,a)$ One specific next latent
Action-free EBM $(s,\tilde{s}’)$ $E_\theta(s,\tilde{s}’)$ Plausibility score
Action-free MAP $s$ plus optimized $\tilde{s}’$ $\arg\min_{\tilde{s}’}E_\theta(s,\tilde{s}’)$ Most likely next latent
Action-free sampling $s$ plus noise or MCMC Samples from $\exp(-E_\theta)$ Diverse possible next latents

The action-conditioned model learns:

\[p(s'|s,a),\]

while the EBM learns the action-marginal transition:

\[p(s'|s) = \int p(s'|s,a)p(a|s)\,da.\]

The EBM does not need to compute this integral explicitly. It learns the marginal compatibility directly from transition data by assigning low energy to observed next latents and high energy to unlikely alternatives.