AnyLearn
All lessons
AIadvanced

Inside a JEPA: Encoders, Predictors, and Collapse

Predicting a learned representation invites a trivial cheat: represent nothing. This lesson opens the JEPA machine, the context encoder, target encoder, and predictor, shows the training loop in code, explains why representation collapse is the central danger, and how asymmetry, stop-gradient, and an EMA target defeat it without negatives.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 8

The machine in three parts

Lesson 1 made the case for predicting representations. Now open the machine. A JEPA has three components, and their asymmetry is the whole design.

  • The context encoder. Takes the visible part of the input (the unmasked patches) and produces embeddings. This is the network you actually care about, the one you keep and use for downstream tasks.
  • The target encoder. Takes the full input and produces embeddings for the masked regions. These are the prediction targets. Crucially, it is not trained by gradient descent.
  • The predictor. A smaller network that takes the context embedding plus a specification of where the target sits (positional information about the masked region) and outputs a guess at the target's embedding.

The loss is simple: the distance (typically L2) between the predictor's output and the target encoder's output, computed in embedding space, not pixel space. There is no decoder anywhere.

That positional conditioning matters. The predictor is not asked "what is somewhere in this image?" but "what is at this specific location?" Without it, the task would be ill-posed, and the model could not learn spatial structure. It is what turns the objective into a genuine prediction problem about a particular region rather than vague summarization.

Full lesson text

All 8 steps on one page, for reading, reference, and search.

Show

1. The machine in three parts

Lesson 1 made the case for predicting representations. Now open the machine. A JEPA has three components, and their asymmetry is the whole design.

  • The context encoder. Takes the visible part of the input (the unmasked patches) and produces embeddings. This is the network you actually care about, the one you keep and use for downstream tasks.
  • The target encoder. Takes the full input and produces embeddings for the masked regions. These are the prediction targets. Crucially, it is not trained by gradient descent.
  • The predictor. A smaller network that takes the context embedding plus a specification of where the target sits (positional information about the masked region) and outputs a guess at the target's embedding.

The loss is simple: the distance (typically L2) between the predictor's output and the target encoder's output, computed in embedding space, not pixel space. There is no decoder anywhere.

That positional conditioning matters. The predictor is not asked "what is somewhere in this image?" but "what is at this specific location?" Without it, the task would be ill-posed, and the model could not learn spatial structure. It is what turns the objective into a genuine prediction problem about a particular region rather than vague summarization.

2. The training loop, in code

The mechanism is clearer as pseudocode. One training step:

# x: a batch of images (or video clips)
context_patches, target_patches, target_pos = mask(x)

# 1. Encode the visible context (this network learns)
z_context = context_encoder(context_patches)

# 2. Encode the FULL input to get targets, no gradients
with torch.no_grad():
    z_target = target_encoder(x)[target_pos]
    z_target = layer_norm(z_target)   # stabilizes scale

# 3. Predict target embeddings from context + position
z_pred = predictor(z_context, target_pos)

# 4. Loss lives in embedding space; no pixels, no decoder
loss = mse(z_pred, z_target)
loss.backward()                        # updates context_encoder + predictor ONLY
opt.step()

# 5. Target encoder follows by EMA, it never gets a gradient
for p_t, p_c in zip(target_encoder.params(), context_encoder.params()):
    p_t.data = m * p_t.data + (1 - m) * p_c.data   # m ~ 0.996 -> 1.0

Every structural choice from the last step appears here. torch.no_grad() on the target branch is the stop-gradient. The EMA update in step 5 is why the target encoder has no optimizer. The loss compares vectors, never pixels.

After training you throw away the predictor and usually the target encoder. The context encoder is the product: the thing that turns an image into a useful representation. Everything else was scaffolding to train it.

3. The trivial cheat

Now the danger. Look again at the loss: minimize the distance between the predicted embedding and the target embedding, where both come from networks being trained. Ask the adversarial question: what is the easiest way to make that loss zero?

Not to understand images. The easiest solution is for the encoder to output the same constant vector for every input. Then the target is always, say, the zero vector, the predictor learns to always output the zero vector, and the loss is exactly zero, forever. Perfect score, and the representation carries no information whatsoever.

This is representation collapse, and it is the defining hazard of joint-embedding methods. It is not a rare pathology; it is the global optimum of the naive objective. A gradient descent process handed this objective with two trainable branches will happily find it, because it is the simplest possible way to satisfy the loss.

Notice why the other families in Lesson 1 do not face this. Generative methods cannot collapse: the pixel target is fixed by the data, so a constant prediction is heavily penalized, you cannot change the ground truth to make yourself right. Contrastive methods can collapse in principle, which is exactly why they use negatives: pushing different images apart makes a constant representation maximally bad, so the repulsion term explicitly forbids the cheat.

JEPA has neither defense. It has no fixed target and no negatives. So it needs another answer.

4. Asymmetry as the defense

The answer, inherited from the BYOL and SimSiam line of work, is architectural asymmetry. The two branches are deliberately made different, and that difference alone is enough to prevent collapse without a single negative sample. Three ingredients combine.

  • The stop-gradient. The target branch receives no gradient (the no_grad() above). This is essential: if gradients flowed to the target encoder, the optimizer could directly optimize the target to be easier to predict, which is precisely the road to a constant. Blocking that path means the model can only improve by making better predictions, never by making the answer easier. It breaks the symmetry that would otherwise let both sides collude on a trivial solution.
  • The predictor. An extra network on the context side only, so the two branches are not interchangeable. SimSiam's ablations (Chen and He) showed both the predictor and the stop-gradient are necessary, remove either and the representation collapses.
  • The EMA target encoder. The target's weights are a slow exponential moving average of the context encoder's. This makes the target a lagged, smoothed version of the online network: a moving but stable goalpost. Because it changes slowly, the online network is always chasing a target it cannot instantly corrupt.

BYOL and SimSiam are nearly identical except for the momentum encoder, which shows the mechanism is not one trick but a family of ways to break symmetry.

5. Why the asymmetry works

It is worth being honest: exactly why stop-gradient plus a predictor prevents collapse is subtle, and was not obvious even to the researchers who found it empirically. Several complementary explanations exist.

The cleanest intuition is about the direction of optimization. With a stop-gradient, the system is no longer doing plain gradient descent on a single energy that both branches share. Only one branch moves in response to the loss; the other is dragged along slowly by the EMA. The dynamics become non-conservative, they do not simply descend the naive objective, so the collapsed solution, though it is a minimum of that objective, is not what the actual training dynamics converge to. The optimizer is following a modified vector field in which the trivial solution is not the attractor.

A second framing notes that the predictor plus stop-gradient produces an implicit repulsion similar to what contrastive negatives supply explicitly. The asymmetric structure induces contrastive-like effects without a contrastive loss, so the model gets the benefit of "spread the representations out" without paying the price of choosing negatives.

The practical upshot for a practitioner is concrete: collapse is an engineering reality to monitor, not a solved theorem. Teams track the variance or rank of the embeddings during training; variance heading toward zero, or the representation's effective rank falling, is the diagnostic that collapse has begun. Momentum schedules and layer normalization on targets are stabilizers tuned for exactly this reason.

6. Latent variables and honest uncertainty

One piece of LeCun's framing completes the architecture. In his 2022 position paper, and developed formally in the latent-variable energy-based model lecture notes by Anna Dawid and Yann LeCun (2023), a JEPA is an energy-based model: it does not output a probability distribution, it assigns a scalar energy, low for compatible context-target pairs, high for incompatible ones. Training shapes that energy landscape, and collapse is precisely the degenerate landscape where energy is low everywhere.

This framing motivates the last component: a latent variable fed to the predictor. The world is genuinely ambiguous. Given the left half of an image, or a video of a hand approaching a cup, several outcomes are compatible with the context, and no deterministic predictor can output all of them. Forced to produce one answer, it outputs the average, which is often a meaningless blur in representation space.

A latent variable gives the predictor a place to put that irreducible uncertainty: as the latent varies, the prediction sweeps the set of plausible outcomes. The predictor is then no longer asked "what is the answer?" but "what answers are compatible?", which is the honest question.

LeCun extends this to H-JEPA, a hierarchical stack where higher levels predict over longer horizons at greater abstraction, because coarse predictions stay valid further into the future. That hierarchy is the proposed route from a representation learner to a world model that can plan, which is where Lesson 3 goes.

7. The design choices, and what they buy

Assemble the mechanism into the choices an implementer actually makes.

ComponentWhy it existsRemove it and...
Loss in embedding spacelets the model discard unpredictable detailyou are back to a pixel autoencoder
Stop-gradient on targetstops the model making the answer easierit optimizes the target to a constant, collapse
Predictor (context side only)breaks branch symmetrycollapse (per SimSiam ablations)
EMA target encodera slow, stable, uncorruptible goalposttraining destabilizes or collapses
Positional conditioningmakes it a real prediction about a placethe task is ill-posed
Latent variableabsorbs genuine ambiguitypredictions average over outcomes, blur
Layer-norm on targetskeeps target scale stablescale drift, degenerate solutions

The unifying reading: JEPA buys the freedom to abstract by taking on the burden of collapse, then pays that burden with asymmetry rather than with negatives or a fixed pixel target. Every row is either exercising that freedom or defending it.

That is the trade in full. Lesson 1 showed why predicting representations is the right target; this lesson showed the machinery that makes it trainable, and the failure mode that machinery exists to prevent. What remains is the payoff: what happens when this architecture is actually built and scaled, on images, then on a million hours of video, and finally used to plan a robot's actions. That is Lesson 3.

8. The JEPA training step

The context encoder embeds visible patches and the predictor guesses the masked region's embedding at a given position, while the target encoder embeds the full input under a stop-gradient; the loss compares embeddings, gradients update only the context side, and the target encoder follows by EMA.

flowchart TD
  A["Input, masked"] --> B["Context encoder (learns)"]
  A --> C["Target encoder (stop-gradient)"]
  B --> D["Predictor + target position"]
  C --> E["Target embeddings"]
  D --> F["Loss in embedding space"]
  E --> F
  F --> G["Gradients update context side only"]
  G --> B
  G --> H["EMA copies weights to target encoder"]
  H --> C

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. What is representation collapse, and why is it the central danger for JEPA?
    • The model overfits to the training set
    • The encoder outputs a constant vector for every input, making the loss exactly zero while carrying no information, it is the global optimum of the naive objective
    • The embeddings grow unboundedly large
    • The predictor becomes larger than the encoder
  2. Why must the target branch have a stop-gradient?
    • To save memory during backpropagation
    • Because otherwise the optimizer could improve the loss by making the TARGET easier to predict, driving it to a constant, the stop-gradient means the model can only win by predicting better
    • Because the target encoder has no parameters
    • To allow the use of negative samples
  3. How is the target encoder updated if it receives no gradients?
    • It is frozen at random initialization forever
    • It is trained on a separate labeled dataset
    • By an exponential moving average (EMA) of the context encoder's weights, making it a slow, lagged, stable goalpost
    • It is re-initialized every epoch
  4. What role does a latent variable play in the predictor?
    • It compresses the model to run faster
    • It absorbs genuine ambiguity, as the latent varies the prediction sweeps the plausible outcomes, instead of a deterministic predictor averaging them into a blur
    • It replaces the need for positional conditioning
    • It stores the training labels
  5. After JEPA pre-training, which component is kept for downstream use?
    • The predictor
    • The pixel decoder
    • The target encoder's optimizer state
    • The context encoder, the predictor and target encoder were scaffolding to train it

Related lessons

AI
advanced

EBMs as a Unifying Lens

Why LeCun treats energy as the common language of machine learning. This lesson shows how classification, generative models, self-supervised learning, JEPA, and diffusion all read as energy-based models, ties the contrastive-versus-regularized split back to self-supervised learning, and gives an honest account of where explicit EBMs help and where they do not.

8 steps·~12 min
AI
advanced

The Memory Wall: Why Training Needs More Than One GPU

Training memory is dominated by things that are not the model. Mixed-precision Adam costs 16 bytes per parameter, so a 70B model needs 1120 GB of state before one activation is stored. This lesson works through where every byte goes, why data parallelism helps throughput but not memory, how accumulation and recomputation trade compute for space, and what each axis of parallelism addresses.

10 steps·~15 min
Robotics
advanced

Modelling Interaction: From Social Forces to Social Pooling

How the field learned to represent people influencing each other: the physics-inspired force model, the Social LSTM pooling layer that replaced hand-designed rules with learned ones, and the attention and graph architectures that followed.

8 steps·~12 min
AI
advanced

What Phase Transitions Mean for Machine Learning

Taking the framework beyond solvable toy models: sharp transitions in real learning, why the loss landscape of a neural network is not the fractured one theory warns about, and what the physics lens genuinely explains.

8 steps·~12 min