Abstract
In-context learning (ICL) for text classification, which uses a few input-label demonstrations to describe a task, has demonstrated impressive performance on large language models (LLMs). However, the selection of in-context demonstrations plays a crucial role and can significantly affect LLMs' performance. Most existing demonstration selection methods primarily focus on semantic similarity between test inputs and demonstrations, often overlooking the importance of label distribution alignment. To address this limitation, we propose a two-stage demonstration selection method, TopK + Label Distribution Divergence (L2D), which leverages a fine-tuned BERT-like small language model (SLM) to generate label distributions and calculate their divergence for both test inputs and candidate demonstrations. This enables the selection of demonstrations that are not only semantically similar but also aligned in label distribution with the test input. Extensive experiments across seven text classification benchmarks show that our method consistently outperforms previous demonstration selection strategies. Further analysis reveals a positive correlation between the performance of LLMs and the accuracy of the underlying SLMs used for label distribution estimation.
Introduction
In-Context Learning (ICL) enables Large Language Models (LLMs) to perform tasks by observing a few demonstrations provided in the prompt. Despite its success, ICL performance is highly sensitive to the choice and ordering of these examples. Traditional selection strategies predominantly rely on semantic similarity—retrieving examples that share similar vocabulary or topics with the test input. However, semantic similarity alone is often insufficient. A test sentence may be semantically similar to a demonstration but possess a different logical structure or label distribution (e.g., a "mixed" sentiment vs. a "pure" one). If demonstrations do not reflect the underlying label uncertainty of the test input, the LLM may produce incorrect predictions.
Methods
We propose a two-stage framework, TopK + Label Distribution Divergence (L2D), to address the limitations of purely semantic selection:
- Semantic Retrieval (TopK): Standard embedding-based retrieval identifies a pool of candidate demonstrations semantically related to the test input.
- Label Distribution Estimation (L2D): We fine-tune a Small Language Model (SLM), such as RoBERTa or DeBERTa, on the task's training data. Unlike standard classification, the SLM generates a probability distribution across all possible labels for both the test input and the candidate demonstrations.
- Divergence Alignment: The system calculates the Jensen-Shannon (JS) divergence between the SLM’s predicted distribution for the test input and each candidate. A low divergence indicates that the example shares the same "ambiguity profile" as the test input.
- Hybrid Scoring: Candidates are ranked using a weighted combination of their semantic similarity and their label distribution alignment.
Results
The L2D method was evaluated across seven diverse text classification benchmarks, including sentiment analysis (SST-2, SST-5, CR), subjectivity analysis (Subj), topic classification (AgNews), and natural language inference (MNLI, QNLI). We tested these against a suite of LLMs including Qwen2.5, Gemma2, Phi3-mini, and LLaMA3-8B.
Key Findings
- Superior Performance: TopK + L2D consistently outperformed existing state-of-the-art methods (e.g., TopK+MDL and TopK+ConE) across six out of seven tasks, achieving an average accuracy improvement of 2.11% over previous best methods.
- SLM-LLM Correlation: A positive correlation was observed between the accuracy of the SLM and the eventual performance of the LLM. Higher-quality label distribution estimation directly translates to better ICL performance.
- Robustness and Generalization: The method remains effective even when labels are intentionally flipped or replaced with arbitrary symbols, suggesting it captures deep structural relationships. Furthermore, it demonstrates strong performance in out-of-domain scenarios where examples come from a different dataset than the test input.
- Efficiency: Using base-sized SLMs introduces minimal overhead compared to the inference costs of 7B+ parameter LLMs, making the two-stage approach computationally feasible.
Conclusion
The TopK + L2D framework highlights the critical importance of label distribution alignment in demonstration selection for ICL. By ensuring that demonstrations reflect the label characteristics of the test input, we can significantly enhance LLM performance and robustness. Future work will explore the applicability of this method to complex generative tasks and aim to improve the initial retrieval stage to further boost performance.
Summary updated