Skip to content

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

SPLADE sparse lexical retrieval

Learns sparse vocabulary-level query and document representations, including expansion terms, so neural retrieval can exploit inverted-index infrastructure.

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 sparse vocabulary-level query and document representations, including expansion terms, so neural retrieval can exploit inverted-index infrastructure. It combines lexical interpretability with learned contextual expansion.

Question / objective

Can learned lexical expansion combine neural relevance with sparse inverted indexes?

Architecture / computational structure

A contextual encoder projects token states to vocabulary weights and pools nonnegative activations. Learned encoder/projection weights are parameters; sparsity penalties, pooling variant, thresholds, and posting-list settings are hyperparameters.

Assumptions

Expansion terms may introduce semantic drift, and aggressive pruning harms recall.

Formula / computational definition

wj=max⁡ilog⁡(1+ReLU⁡(zij)),S(q,d)=wq⊤wd(max-pooling variant).w_j=\max_i\log(1+\operatorname{ReLU}(z_{ij})),\qquad S(q,d)=w_q^\top w_d\qquad\text{(max-pooling variant).}

Vocabulary weight w_j can aggregate log(1+ReLU(z_ij)) across token positions i using a model-specific pooling operation. Ranking uses a sparse dot product; training combines relevance loss with sparsity or computational-cost regularization.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Vocabulary projection, pooling variant, sparsity penalties, query/document asymmetry, pruning thresholds, negatives, and index implementation.

Fitting, fine-tuning, and adaptation

Fit ranking/contrastive loss plus documented sparsity regularization. Freeze selected encoder layers or use verified adapters while preserving output-head training as required. Rebuild sparse indexes after changes and tune regularization on a quality-versus-query-cost curve; inspect expansion drift and domain forgetting.

How to use and calibrate

Report ranking metrics, posting-list/index size, query latency, sparsity-quality curves, and lexical versus semantic failure cases. 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

Search a large document corpus using sparse learned term expansions and existing index infrastructure.

Interpretation / pitfalls

Watch out

Sparse vector weights are learned retrieval features, not calibrated probabilities that a term is true or present in a document. 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' SPLADE implementation and documented sparse-index backends. R normally bridges model encoding via reticulate, while sparse-vector scoring can be native. The example demonstrates nonnegative log-saturated weights and a sparse dot product, not full relevance training.

Example scope: SPLADE-style log-saturated nonnegative vocabulary activations with max pooling.

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 — SPLADE-style log-saturated nonnegative vocabulary activations with max pooling.

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)
logits = [[1,-1,0.5],[0.2,0.8,-0.4]]
weights = [max(math.log1p(max(row[j],0)) for row in logits) for j in range(3)]
query_weights = [0.4,0.1,0.0]
print(weights,dot(weights,query_weights))
R

Component demo, not a trained model — SPLADE-style log-saturated nonnegative vocabulary activations with max pooling.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
logits <- rbind(c(1,-1,0.5),c(0.2,0.8,-0.4))
weights <- apply(log1p(pmax(logits,0)),2,max)
query_weights <- c(0.4,0.1,0)
print(list(weights=weights,score=sum(weights*query_weights)))

Sources

From the CodeSplash reference, entry LNG056.