When a Transformer processes a sentence, it doesn't produce a single answer. Instead, it generates a sequence of high-dimensional vectors—one for every token in the input. For tasks like sentiment analysis, document retrieval, or semantic similarity, we face a "bottleneck" problem: how do we collapse this variable-length sequence of vectors into a single, fixed-length "textual fingerprint"?
While early models like BERT popularized the use of a dedicated classification token ([CLS]), a growing body of research suggests that mean pooling—simply averaging the embeddings of all tokens in a sequence—is a more robust and semantically rich way to represent text.
The Distributed Nature of Meaning
The intuition behind mean pooling stems from the fact that semantic information in Large Language Models (LLMs) is not concentrated in a single point. As Zhiquan Tan and colleagues demonstrate in The Information of Large Language Model Geometry, a sentence's meaning is distributed across all its constituent tokens.
Relying on a single specialized token like [CLS] creates a representational bottleneck. In contrast, mean pooling acts as a holistic aggregator. This is particularly vital in morphologically rich languages like Latin. Anna Mambelli and Fabio Tutrone found that averaging token embeddings captures the grammatical and semantic information distributed across inflected word forms far more effectively than a summary token (Benchmarking BERT-based Models for Latin).
Why Mean Pooling Outperforms the CLS Token
In the early days of BERT, the [CLS] token was the default choice for sentence-level tasks. However, Xiaofei Ma and others noted in Universal Text Representation from BERT that raw BERT embeddings are surprisingly mediocre at capturing semantic similarity. They discovered that switching to mean pooling across layers transforms the model into a "powerhouse" for embeddings.
This was further solidified by Nils Reimers and Iryna Gurevych in their seminal work on Sentence-BERT. By applying a pooling layer to the final hidden states, they showed that the mean of all output vectors provides a more effective semantic representation for clustering and retrieval than the default start-of-sequence token.
The reason for this performance gap lies in how relevance is distributed. As explored in Explaining Text Similarity in Transformer Models by Alexandros Vasileiou, mean pooling encourages a balanced distribution of importance across various parts of speech. While [CLS] pooling might fixate on specific token interactions, mean pooling captures the "global gist" of the input.
Adapting to the Decoder-Only Era
The shift toward decoder-only models (like Llama and Mistral) has made pooling strategies even more critical. Unlike BERT, these models are trained for next-token prediction and often lack a natural "summary" token at the start of the sequence.
In Sentence-T5, Jianmo Ni demonstrated that for architectures lacking a dedicated classification token, averaging all hidden states is significantly superior to using the first or last token. This is because the final token in a decoder-only model is often specialized for predicting the next word, which may not contain the full semantic summary of the preceding text.
Furthermore, Albert Q. Jiang and Wenda Li identify mean pooling as a key component of the "compute-optimal recipe" for turning generative models into embedding models (Repurposing Language Models into Embedding Models). By averaging across the entire sequence, the resulting vector captures information from every state, ensuring no part of the context is discarded.
Robustness Across Domains
The versatility of mean pooling is evident across diverse applications:
- Psychological Profiling: Nikita Soni and H. Andrew Schwartz found that averaging hidden states is the most effective way to capture psychological signals distributed throughout a text (Evaluation of LLMs-based Hidden States as Author Representations).
- Code Retrieval: In LoRACode, Aman Chadha utilizes a padding-aware mean pooling strategy to ensure that variable-length code snippets are represented by their actual content, not the zeros used to pad the sequence.
- Knowledge Graphs: The GilBERT framework uses mean pooling to generate entity and relation representations, finding it more robust for similarity-based reasoning than standard sequence tokens.
By treating the Transformer's output as a collective pool of information rather than a single-point summary, mean pooling allows models to maintain a high-fidelity "semantic map" of the input text.