Improving Text Embeddings for Smaller Language Models Using Contrastive Fine-tuning

Trapoom Ukarapol, Zhicheng Lee, Amy XinarXiv 2024

Abstract

While Large Language Models show remarkable performance in natural language understanding, their resource-intensive nature makes them less accessible. In contrast, smaller language models such as MiniCPM offer more sustainable scalability, but often underperform without specialized optimization. In this paper, we explore the enhancement of smaller language models through the improvement of their text embeddings. We select three language models, MiniCPM, Phi-2, and Gemma, to conduct contrastive fine-tuning on the NLI dataset. Our results demonstrate that this fine-tuning method enhances the quality of text embeddings for all three models across various benchmarks, with MiniCPM showing the most significant improvements of an average 56.33% performance gain. The contrastive fine-tuning code is publicly available at https://github.com/trapoom555/Language-Model-STS-CFT.

Introduction

Large Language Models (LLMs) have set new standards for natural language understanding, yet their immense computational requirements limit their practical accessibility. Smaller Language Models (SLMs), typically in the 1B to 3B parameter range, offer a more sustainable and deployable alternative. However, these models often struggle to generate high-quality text embeddings—vector representations crucial for semantic similarity tasks—without specific optimization. This paper addresses this gap by investigating the efficacy of contrastive fine-tuning on three prominent SLMs: MiniCPM, Phi-2, and Gemma.

Methodology

The researchers utilized a two-pronged strategy to enhance embedding quality while maintaining computational efficiency. First, they applied contrastive fine-tuning using the InfoNCE loss function. Unlike standard supervised learning, this approach trains the model to minimize the distance between semantically similar sentence pairs (premise and entailment) while maximizing the distance between dissimilar pairs (premise and contradiction). The training utilized the Natural Language Inference (NLI) dataset, which contains approximately 275,000 sentence triplets.

Second, to ensure the process remained resource-efficient, the team employed Parameter-Efficient Fine-Tuning (PEFT) via LoRA (Low-Rank Adaptation). By training only a small set of auxiliary weights instead of the entire model, they significantly reduced the GPU memory footprint, enabling training on consumer-grade hardware like the RTX 3090. Embeddings were extracted from the final <EOS> token after appending it to the input.

Results and Analysis

The fine-tuning process yielded substantial improvements across all tested models. Evaluation was conducted on nine Semantic Textual Similarity (STS) benchmarks (STS12-17, STSBenchmark, BIOSSES, and SICK-R), measuring the Spearman correlation between model predictions and human judgments.

Performance comparison across STS benchmarks shows MiniCPM achieving the highest overall correlation after fine-tuning.

MiniCPM emerged as the top performer, achieving an overall Spearman correlation of 83.84%, a remarkable improvement from its baseline performance of 27.51%. Gemma and Phi-2 also saw gains, reaching 81.56% and 70.89%, respectively. Notably, MiniCPM demonstrated exceptional alignment with human judgment in the STS17 benchmark, reaching an 89.96% correlation.

A key observation was the speed of convergence. The models achieved significant performance gains within a very small number of training steps, typically plateauing early in the process.

The Mean Spearman Correlation improves rapidly, plateauing after approximately 200 checkpoints.

Discussion

The "Zero-to-Hero" transformation of MiniCPM—moving from the weakest baseline to the strongest fine-tuned model—suggests that certain SLM architectures possess significant latent potential for embedding tasks that can be unlocked with targeted tuning.

The study also uncovered a "Prompting Paradox." While the base models often benefited from task-specific prompts (e.g., "This sentence means..."), these same prompts frequently degraded the performance of fine-tuned models. This suggests that the fine-tuning process aligns the model so closely with the raw sentence format of the training data that additional prompting becomes counterproductive. Furthermore, the inclusion of "hard negatives" in the contrastive loss was found to be critical for helping the models distinguish between sentences that share vocabulary but differ in meaning.

Conclusion

This research demonstrates that contrastive fine-tuning is a highly effective method for elevating the performance of smaller language models to levels competitive with much larger counterparts. By achieving an average performance gain of 56.33% for MiniCPM, the authors highlight the viability of SLMs for resource-efficient semantic search and retrieval. Future work will focus on expanding these techniques to multilingual contexts and even smaller "edge-device" models.

Summary updated

BibTeX
@misc{ukarapol2024improving,
  author = {Trapoom Ukarapol and Zhicheng Lee and Amy Xin},
  title = {{Improving Text Embeddings for Smaller Language Models Using Contrastive Fine-tuning}},
  year = {2024},
  eprint = {2408.00690},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.00690},
}
Figure