HNCSE: Advancing Sentence Embeddings via Hybrid Contrastive Learning with Hard Negatives

Wenxiao Liu, Zihong Yang, Chaozhuo Li, Zijin Hong, Jianfeng Ma, Zhiquan Liu, Litian Zhang, Feiran HuangarXiv 2024

Abstract: Unsupervised sentence representation learning remains a critical challenge in modern natural language processing (NLP) research. Recently, contrastive learning techniques have achieved significant success in addressing this issue by effectively capturing textual semantics. Many such approaches prioritize the optimization using negative samples. In fields such as computer vision, hard negative samples (samples that are close to the decision boundary and thus more difficult to distinguish) have been shown to enhance representation learning. However, adapting hard negatives to contrastive sentence learning is complex due to the intricate syntactic and semantic details of text. To address this problem, we propose HNCSE, a novel contrastive learning framework that extends the leading SimCSE approach. The hallmark of HNCSE is its innovative use of hard negative samples to enhance the learning of both positive and negative samples, thereby achieving a deeper semantic understanding. Empirical tests on semantic textual similarity and transfer task datasets validate the superiority of HNCSE.

TL;DR

HNCSE advances unsupervised sentence embeddings by using "hard negative" samples to refine the boundaries of the semantic space. It introduces two novel mixing strategies—Positive Mixing and Hard Negative Mixing—to challenge the model during training, ultimately outperforming standard baselines and even some large language models (LLMs) on semantic similarity tasks.

What Problem Does This Paper Address?

In Natural Language Processing (NLP), "sentence embedding" is the process of turning a sentence into a mathematical vector that represents its meaning. Ideally, sentences with similar meanings should have vectors that are close together in the embedding space.

The current gold standard for doing this without labels is Contrastive Learning, specifically the SimCSE model. SimCSE takes a sentence, applies "dropout" to create two slightly different "views" of it, and trains the model to pull these views together (positives) while pushing all other sentences in the batch away (negatives).

However, this approach has significant flaws:

  1. Confusing Positives: Sometimes the dropout versions are too different, or a "negative" sentence in the batch is actually semantically very similar to the query.
  2. Over-Easy Negatives: Most sentences in a random batch are totally unrelated (e.g., "I like apples" vs. "The stock market crashed"). The model learns very little from these easy comparisons.
  3. Missing Hard Negatives: The model needs "hard negatives"—sentences that look similar but mean something different (e.g., "Two dogs are running" vs. "Two dogs are walking")—to truly learn the nuances of language. As shown in Figure 1, standard batches often lack these critical boundary cases.
The challenge of identifying and utilizing hard negatives in the semantic space.

Methods: The HNCSE Framework

The authors propose two main extensions to the SimCSE framework to address the scarcity and utility of hard negatives:

1. HNCSE-PM (Positive Mixing)

Instead of just pulling the two dropout "views" of a sentence together, HNCSE identifies the hardest negative in the batch and mixes a portion of it into the positive sample. This "pulls" the positive sample slightly toward the decision boundary, forcing the model to work harder to keep the positive pair together despite the confusing influence. This effectively sharpens the model's ability to distinguish true positives from near-misses.

2. HNCSE-HNM (Hard Negative Mixing)

To solve the "easy negative" problem, the model maintains a pool of hard negatives from previous batches. It then uses a technique called Mixup (linear interpolation) to combine these hard negatives, creating new, synthetic "super-hard" negatives. This fills the gaps in the embedding space and provides a more continuous and rigorous challenge for the model during training.

3. Adaptive Scaling

The mixing isn't random; the authors use an adaptive coefficient based on similarity scores to ensure the synthetic samples are challenging but not impossible to learn from, preventing the model from collapsing during training.

Key Results

The model was trained on 1 million unlabeled sentences from Wikipedia and evaluated on the Semantic Textual Similarity (STS) benchmarks and various transfer tasks.

Performance comparison of HNCSE against state-of-the-art models on STS benchmarks.

As detailed in the results table above, HNCSE demonstrates clear superiority:

  • Superior Accuracy: HNCSE-PM (Base) achieved an average Spearman’s correlation of 78.38% on STS tasks, a significant jump over the original SimCSE's 76.25%.
  • Beating Large Language Models: Remarkably, the BERT-sized HNCSE models outperform massive LLMs like LLaMA2-7B (even with prompt engineering or knowledge enhancement) on these specific similarity tasks.
  • Large Model Gains: When scaled to a BERT-Large architecture, HNCSE reached 79.20%, setting a new high bar for unsupervised methods.
  • Transfer Task Success: The model showed a "remarkable enhancement" in the MRPC (Microsoft Research Paraphrase Corpus) dataset, indicating a better grasp of whether two sentences are paraphrases of each other.

Conclusion and Future Work

HNCSE proves that using negative information to improve positive samples is a powerful tool for representation learning. By "hallucinating" difficult edge cases through Mixup, the authors effectively bridge the gap in the semantic space that standard contrastive learning ignores.

Future work will focus on mitigating sensitivity to noise in unannotated data and incorporating broader document context to help the model understand more complex linguistic relationships beyond the sentence level.

Summary updated

BibTeX
@misc{liu2024hncse,
  author = {Wenxiao Liu and Zihong Yang and Chaozhuo Li and Zijin Hong and Jianfeng Ma and Zhiquan Liu and Litian Zhang and Feiran Huang},
  title = {{HNCSE: Advancing Sentence Embeddings via Hybrid Contrastive Learning with Hard Negatives}},
  year = {2024},
  eprint = {2411.12156},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2411.12156},
}
Figure