Deep Continuous Prompt for Contrastive Learning of Sentence Embeddings

Yuxin Jiang, Wei Wang

Abstract

Contrastive learning has been demonstrated to be effective in enhancing pre-trained language models (PLMs) to derive superior universal sentence embeddings. However, existing contrastive methods still have two limitations. Firstly, previous works may acquire poor performance under domain shift settings, thus hindering the application of sentence representations in practice. We attribute this low performance to the over-parameterization of PLMs with millions of parameters. To alleviate it, we propose PromCSE (Prompt-based Contrastive Learning for Sentence Embeddings), which only trains small-scale Soft Prompt (i.e., a set of trainable vectors) while keeping PLMs fixed. Secondly, the commonly used NT-Xent loss function of contrastive learning does not fully exploit hard negatives in supervised learning settings. To this end, we propose to integrate an Energy-based Hinge loss to enhance the pairwise discriminative power, inspired by the connection between the NT-Xent loss and the Energy-based Learning paradigm. Empirical results on seven standard semantic textual similarity (STS) tasks and a domain-shifted STS task both show the effectiveness of our method compared with the current state-of-the-art sentence embedding models. Our code is publicly available at https://github.com/YJiangcm/PromCSE.

Introduction

State-of-the-art models for sentence embeddings, such as SimCSE, typically rely on contrastive learning to "pull" similar sentences together and "push" different ones apart in vector space. Despite their success, these models face two major hurdles:

  1. Domain Shift: Large Pre-trained Language Models (PLMs) with millions of parameters are prone to overfitting on training data (e.g., Wikipedia). Consequently, their performance often degrades significantly when applied to different domains, such as image captions.
  2. Inefficient Loss Functions: The standard NT-Xent loss function used in contrastive learning often fails to sufficiently separate "hard negatives"—sentences that are syntactically similar but semantically distinct (e.g., "A man is sleeping" vs. "A man is running").

Methodology

To address these issues, we introduce PromCSE, which incorporates two primary innovations:

Prompt-based Architecture

Instead of fine-tuning all weights in a model like BERT or RoBERTa, PromCSE freezes the entire PLM. We prepend a small set of trainable vectors, known as Soft Prompts, to the input of every layer in the Transformer. This multi-layer approach allows the model to adapt to sentence embedding tasks while preserving the general linguistic knowledge of the frozen weights, significantly improving robustness to new domains.

Energy-based Hinge (EH) Loss

We establish a theoretical connection between contrastive learning and Energy-Based Models (EBMs). Based on this, we propose a supplemental Energy-based Hinge (EH) loss. This loss explicitly enforces a mathematical margin between a positive pair and the most challenging negative pair. By creating a clear gap in the embedding space, the model better distinguishes between semantically different sentences.

Experimental Setup

We evaluated PromCSE using several benchmarks:

  • Training: 1 million unlabeled Wikipedia sentences (unsupervised) and the SNLI/MNLI datasets (supervised).
  • Evaluation: Seven standard Semantic Textual Similarity (STS) tasks (STS 2012–2016, STS Benchmark, and SICK-Relatedness).
  • Domain Shift: The Crisscrossed Captions (CxC) dataset, featuring image captions to test out-of-domain generalization.

Results and Analysis

PromCSE demonstrates superior performance and robustness across all evaluated tasks.

Comparison of PromCSE and SimCSE on standard and domain-shifted STS tasks.

As shown in the figure above, unsupervised PromCSE-BERT achieves an average score of 78.49% on standard STS tasks, outperforming SimCSE (76.25%). More importantly, on the domain-shifted CxC task, PromCSE improves performance by 3.7 absolute points (from 67.5% to 71.2%), highlighting its resilience to data distribution changes.

In supervised settings, the integration of EH loss pushes the performance even further, achieving new state-of-the-art (SOTA) results.

Detailed performance of supervised models on STS tasks.

As detailed in the table, PromCSE-BERT with EH loss reaches 82.13%, while the RoBERTa-large variant achieves 85.10%. Analysis indicates that while unsupervised PromCSE excels at "alignment" (keeping similar items close), the supervised version with EH loss significantly improves "uniformity" (distributing different items evenly across the space).

Conclusion

PromCSE offers a robust and efficient framework for sentence embeddings by leveraging frozen PLMs and multi-layer soft prompts. While it achieves SOTA results on similarity tasks, future work will explore improving performance on supervised transfer tasks (e.g., sentiment classification) and developing methods to automatically sample hard negatives for unsupervised learning.

Summary updated

Abstract

Contrastive learning has been demonstrated to be effective in enhancing pre-trained language models (PLMs) to derive superior universal sentence embeddings. However, existing contrastive methods still have two limitations. Firstly, previous works may acquire poor performance under domain shift settings, thus hindering the application of sentence representations in practice. We attribute this low performance to the over-parameterization of PLMs with millions of parameters. To alleviate it, we propose PromCSE (Prompt-based Contrastive Learning for Sentence Embeddings), which only trains small-scale \emph{Soft Prompt} (i.e., a set of trainable vectors) while keeping PLMs fixed. Secondly, the commonly used NT-Xent loss function of contrastive learning does not fully exploit hard negatives in supervised learning settings. To this end, we propose to integrate an Energy-based Hinge loss to enhance the pairwise discriminative power, inspired by the connection between the NT-Xent loss and the Energy-based Learning paradigm. Empirical results on seven standard semantic textual similarity (STS) tasks and a domain-shifted STS task both show the effectiveness of our method compared with the current state-of-the-art sentence embedding models. Our code is publicly avaliable at https://github.com/YJiangcm/PromCSE
BibTeX
@inproceedings{jiang2022deep,
  author = {Yuxin Jiang and Wei Wang},
  title = {{Deep Continuous Prompt for Contrastive Learning of Sentence Embeddings}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2022},
  url = {https://openreview.net/forum?id=7S3-PiMD6j},
}
Figure