Why Bert transformer uses CLS token for classification instead of average over all tokens?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The BERT (Bidirectional Encoder Representations from Transformers) model fundamentally changes how Natural Language Processing (NLP) tasks are tackled by providing deep contextualized embeddings. One of its unique aspects is the use of the special `[CLS]` token when it comes to classification tasks. Understanding why BERT relies on this specific token for classifications, instead of averaging over all tokens, involves a deeper dive into the architecture and functions of BERT and Transformers in general.
Understanding the `[CLS]` Token in BERT
BERT uses a variety of specialized tokens to manage different NLP tasks, one of which is the `[CLS]` token. This token is placed at the beginning of every input sequence and plays a critical role during the fine-tuning stages of classification tasks. But why opt for a single token embedding rather than averaging all token embeddings?
Focused Representation
The main idea behind using the `[CLS]` token is its focused representation. As BERT processes an input sequence, each layer of the Transformer updates the embeddings. The output embedding corresponding to the `[CLS]` token is designed to aggregate information from the entire sequence, representing it as a single, task-focused vector.
Centralized Processing
- Transformers Architecture: In a Transformer, the self-attention mechanism enables each position (token) in the input sequence to attend to and get information from every other position dynamically. The `[CLS]` token, being part of the sequence, gathers contextual information from the whole input throughout these attention mechanisms.
- Unified Embedding Space: As BERT is pre-trained over massive datasets, the `[CLS]` token also becomes part of this learned knowledge space. During fine-tuning, this helps individualize its output to be more task-specific, serving as a representation of the entire sentence or sequence.
Comparison with Averaging All Tokens
Taking an average over all tokens in a sequence might seem a fair approach, but it dilutes this centralized task-optimized information. Here are some reasons elucidating this:
- Information Specificity: Averaging embeddings can lead to loss of task-specific nuances as all tokens contribute equally regardless of their relevance to the classification task.
- Noise Reduction: By focusing on the `[CLS]` token, BERT can reduce the noise potentially introduced by less informative tokens that would be included in an average.
- Training Efficiency: The use of a single standardized token simplifies the training and fine-tuning process, allowing for more efficient convergence on the model's objectives.
Illustration: Sentiment Classification Example
Consider a sentiment classification task where the input is a sentence, and the goal is to classify the sentiment as positive or negative.
- With `[CLS]` Token: The output vector for `[CLS]` is fed into a classifier to predict sentiment. The tokenizer converts "BERT is fantastic!" into following tokens: `[CLS]`, `BERT`, `is`, `fantastic`, `!`, `[SEP]`. The `[CLS]` embedding is enriched by attention mechanisms capturing "fantastic" as a positive indicator, agglomerating this via the layers to output a sentiment-focused vector.
- With Averaged Tokens: If you average "BERT", "is", and "fantastic", the sentiment-indicative boost from "fantastic" may be diminished by the neutral words "BERT" and "is," potentially leading to less accurate classification results.
Summary Table
| Aspect | Using \[CLS] Token | Averaging All Tokens |
| Focus | Centralized, task-specific | Diffused, task-generic |
| Contextual Power | Aggregates full context | Equal emphasis across tokens |
| Noise Handling | Reduced noise | Increased noise |
| Computation Complexity | Simplifies tasks and objectives | Potentially more computation to manage cross-token relations |
| Output Representation | Unified, single vector | Blended vector with reduced specificity |
Conclusion
The decision for BERT to use the `[CLS]` token for classification instead of averaging stems from leveraging more precise, centralized, and refined representations of the input sequences. This strategy enhances the model's ability to fine-tune effectively for various downstream tasks by focusing on the richness of transformer-encoded contextual information. Ultimately, the `[CLS]` token aids the model in learning nuanced and distinct patterns that are critical for classification tasks.

