Generalization through Memorization: Nearest Neighbor Language Models

Urvashi Khandelwal, Omer Levy, Dan Jurafsky, Luke Zettlemoyer, Mike Lewis

Introduction

Neural Language Models (LMs) are typically tasked with two distinct objectives: learning a meaningful mathematical representation of text and predicting the next word in a sequence. While modern architectures like Transformers are excellent at learning representations, the prediction problem remains incredibly challenging. Standard LMs attempt to "cram" all world knowledge and linguistic patterns into a fixed set of parameters. This approach struggles with the "long tail" of language—rare words, factual knowledge, and specific names that do not appear often enough in the training data to be reliably stored in the model's weights.

To address this, researchers introduce the $k$NN-LM, an approach that improves language models by allowing them to explicitly "look up" similar examples from a massive datastore at inference time. By interpolating a standard pre-trained Transformer with a $k$-nearest neighbors search, the authors achieved state-of-the-art results without any additional training, demonstrating that explicit memorization is a powerful shortcut to better generalization.

The $k$NN-LM Approach

The core hypothesis of the $k$NN-LM is that it is easier for a model to recognize that two contexts are similar than it is to calculate the exact probability distribution of the next word from scratch. The method augments a pre-trained Transformer LM with a retrieval mechanism in two phases:

  1. Datastore Creation: The model performs a single forward pass over a text collection (such as the original training set). For every token, it saves the LM's internal representation (the "key") and the actual word that followed it (the "value").
  2. Inference via Lookup: When predicting a word for a new sentence, the model computes the representation of the current context. It then queries the datastore for the $k$ most similar contexts using squared $L^2$ distance.
The kNN-LM architecture interpolates a standard language model distribution with a k-nearest neighbors retrieval distribution.

As shown above, the model creates a probability distribution based on the targets associated with those nearest neighbors. Finally, it linearly combines this "retrieval-based" distribution with the standard "softmax-based" distribution from the neural network using a tuned interpolation parameter, $\lambda$. To make searching through billions of examples computationally feasible, the authors utilize FAISS, a library optimized for fast similarity search in high-dimensional spaces.

Key Insights and Performance

The researchers evaluated their approach on several English corpora, including WIKITEXT-103, the Toronto Books Corpus, and subsets of English Wikipedia (WIKI-3B & WIKI-100M). The results yielded several compelling insights:

State-of-the-Art Performance: On the WIKITEXT-103 benchmark, the $k$NN-LM achieved a perplexity of 15.79, a massive 2.9-point improvement over the previous best model. Remarkably, this was achieved using the exact same underlying architecture and zero extra training. Increasing the number of neighbors ($k$) consistently improved performance, with $k=1024$ providing a strong balance.

Scaling without Training: One of the most profound findings is that a model's performance can be drastically improved simply by providing it with a larger datastore.

Perplexity of a 100M-token model augmented with a kNN datastore compared to a model trained on 3B tokens.

The authors demonstrated that a model trained on only 100 million tokens, when paired with a 3-billion-token datastore during inference, could actually outperform a model explicitly trained on the full 3 billion tokens. This suggests that explicit memory (the datastore) is far more efficient for capturing long-tail factual knowledge than implicit memory (model weights).

Zero-Shot Domain Adaptation: The $k$NN-LM architecture allows for seamless domain adaptation. A model trained on Wikipedia can become an expert on legal, medical, or fictional text simply by swapping the datastore for one containing those specific documents—no retraining or fine-tuning required.

Limitations and Future Directions

While highly effective, the $k$NN-LM approach introduces new computational challenges. Although FAISS is fast, performing a $k$-nearest neighbor search for every single token at inference time is significantly slower than a standard forward pass, making real-time deployment challenging. Additionally, storing high-dimensional vectors for every token in a multi-billion-word corpus requires massive disk space and RAM.

Currently, the model uses a "frozen" LM to build the datastore. Future work could involve training the LM specifically to be better at retrieval, learning a similarity function that is explicitly optimized for $k$NN lookup, further bridging the gap between representation learning and explicit memorization.

Summary updated

BibTeX
@inproceedings{khandelwal2020generalization,
  author = {Urvashi Khandelwal and Omer Levy and Dan Jurafsky and Luke Zettlemoyer and Mike Lewis},
  title = {{Generalization through Memorization: Nearest Neighbor Language Models}},
  booktitle = {International Conference on Learning Representations},
  year = {2020},
  url = {https://openreview.net/forum?id=jATUHWLmSBH},
}
Figure