Skip to content

Language models, retrieval, prompting, and language adaptation · Long-context and retrieval-augmented language · LNG050 · Model

REALM

Learns a retrieval-augmented language model where selecting relevant documents is part of pretraining. It connects a neural retriever to masked prediction, enabling external documents to contribute information absent…

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Learns a retrieval-augmented language model where selecting relevant documents is part of pretraining. It connects a neural retriever to masked prediction, enabling external documents to contribute information absent from the immediate input.

Question / objective

Can masked-token supervision teach which external documents are useful evidence?

Architecture / computational structure

A learned retriever supplies latent documents to a reader; their joint probability is approximated over retrieved candidates. Learned retriever/reader weights are parameters; candidate depth and index-refresh policy are hyperparameters.

Assumptions

Approximate retrieval can omit useful documents and stale indexes can mismatch evolving embeddings.

Formula / computational definition

p(y∣x)=∑zpθ(z∣x)pϕ(y∣x,z).p(y\mid x)=\sum_zp_\theta(z\mid x)p_\phi(y\mid x,z).

Marginalize a latent retrieved document z: p(y|x)=sum_z p_theta(z|x)p_phi(y|x,z). A practical top-k approximation limits candidates, while retriever and reader learning encourage evidence useful for predicting masked targets.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Candidate depth, retrieval embedding dimension, corpus index refresh rate, masking strategy, retriever initialization, and reader capacity.

Fitting, fine-tuning, and adaptation

Pretrain with the specified latent-document objective, then adapt reader and optionally retriever on the target task. Freeze the retriever for a stable baseline; if updating it, refresh indexed embeddings. Validate retrieval recall, reader performance, leakage, and whether changes degrade general-domain retrieval.

How to use and calibrate

Report retrieval recall, downstream open-domain question answering, masked-token likelihood, index-refresh cost, and corpus-contamination controls. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Answer factual questions using evidence learned through retrieval-aware pretraining.

Interpretation / pitfalls

Watch out

Weak evidence supervision may learn shortcuts or retrieve answer leakage rather than reliable generalizable knowledge. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: Use the authors' REALM code or a faithful documented reproduction; standard RAG inference does not reproduce REALM pretraining. R bridges Python using reticulate. The native example evaluates a two-document probability marginalization and exposes the model's latent-document interpretation.

Example scope: REALM latent-document marginal probability for one masked target.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — REALM latent-document marginal probability for one masked target.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
p_document, p_target_given_document = [0.7,0.3], [0.8,0.2]
p_target = dot(p_document,p_target_given_document)
print(p_target,-math.log(p_target))
R

Component demo, not a trained model — REALM latent-document marginal probability for one masked target.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
p_document <- c(0.7,0.3); p_target_given_document <- c(0.8,0.2)
p_target <- sum(p_document*p_target_given_document)
print(c(probability=p_target,loss=-log(p_target)))

Sources

From the CodeSplash reference, entry LNG050.