Lune

EMNLP2023Top-tier venue

Rethinking Negative Pairs in Code Search

Haochen Li, Xin Zhou, Anh Tuan Luu, Chunyan Miao

Abstract

Recently, contrastive learning has become a key component in fine-tuning code search models for software development efficiency and effectiveness. It pulls together positive code snippets while pushing negative samples away given search queries. Among contrastive learning, InfoNCE is the most widely used loss function due to its better performance. However, the following problems in negative samples of In-foNCE may deteriorate its representation learning: 1) The existence of false negative samples in large code corpora due to duplications. 2). The failure to explicitly differentiate between the potential relevance of negative samples. As an example, a bubble sorting algorithm example is less "negative" than a file saving function for the quick sorting algorithm query. In this paper, we tackle the above problems by proposing a simple yet effective Soft-InfoNCE loss that inserts weight terms into InfoNCE. In our proposed loss function, we apply three methods to estimate the weights of negative pairs and show that the vanilla InfoNCE loss is a special case of Soft-InfoNCE. Theoretically, we analyze the effects of Soft-InfoNCE on controlling the distribution of learnt code representations and on deducing a more precise mutual information estimation. We furthermore discuss the superiority of proposed loss functions with other design alternatives. Extensive experiments demonstrate the effectiveness of Soft-InfoNCE and weights estimation methods under state-of-the-art code search models on a large-scale public dataset consisting of six programming languages. Source code is available at https://github.com/ Alex-HaochenLi/Soft-InfoNCE .

Ask Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

6 top-tier papers have cited it since. Know what they found before your reviewers do.

Questions to start from

Lune can draw on

the full paper · the top-tier papers that cite it · related work

Get the answer

Free to start. No credit card required.

Lineage

  • MGS3: A Multi-Granularity Self-Supervised Code Search Framework cites CodeBERT: A Pre-Trained Model for Programming and Natural Languages
  • MGS3: A Multi-Granularity Self-Supervised Code Search Framework cites GraphCodeBERT: Pre-training Code Representations with Data Flow
  • Rewriting the Code: A Simple Method for Large Language Model Augmented Code Search cites CodeBERT: A Pre-Trained Model for Programming and Natural Languages
  • Rewriting the Code: A Simple Method for Large Language Model Augmented Code Search cites GraphCodeBERT: Pre-training Code Representations with Data Flow
  • Debiased Contrastive Learning of Unsupervised Sentence Representations cites On the Sentence Embeddings from Pre-trained Language Models
  • DrainCode: Stealthy Energy Consumption Attacks on Retrieval-Augmented Code Generation via Context Poisoning cites CodeBERT: A Pre-Trained Model for Programming and Natural Languages
  • DrainCode: Stealthy Energy Consumption Attacks on Retrieval-Augmented Code Generation via Context Poisoning cites GraphCodeBERT: Pre-training Code Representations with Data Flow
  • Uncertainty-Aware Contrastive Learning with Hard Negative Sampling for Code Search Tasks cites Rewriting the Code: A Simple Method for Large Language Model Augmented Code Search
  • Uncertainty-Aware Contrastive Learning with Hard Negative Sampling for Code Search Tasks cites Understanding Contrastive Representation Learning through Alignment and Uniformity on the Hypersphere
  • Uncertainty-Aware Contrastive Learning with Hard Negative Sampling for Code Search Tasks cites CodeBERT: A Pre-Trained Model for Programming and Natural Languages
  • Uncertainty-Aware Contrastive Learning with Hard Negative Sampling for Code Search Tasks cites GraphCodeBERT: Pre-training Code Representations with Data Flow
Builds onCited byLeft to right by year; the more cited, the higher.

Cited by top-tier papers6

  1. Rewriting the Code: A Simple Method for Large Language Model Augmented Code SearchHaochen Li, Xin Zhou, Zhiqi ShenACL 2024 · 10 citations
  2. DrainCode: Stealthy Energy Consumption Attacks on Retrieval-Augmented Code Generation via Context PoisoningYanli Wang, Jiadong Wu, Tianyue Jiang, Mingwei Liu et al.ASE 2025 · 3 citations
  3. AlignCoder: Aligning Retrieval with Target Intent for Repository-Level Code CompletionTianyue Jiang, Yanlin Wang, Yanli Wang, Daya Guo et al.ASE 2025 · 2 citations
  4. Uncertainty-Aware Contrastive Learning with Hard Negative Sampling for Code Search TasksHan Liu, Jiaqing Zhan, Qin ZhangAAAI 2025 · 1 citation
  5. MGS3: A Multi-Granularity Self-Supervised Code Search FrameworkRui Li, Junfeng Kang, Qi Liu, Liyang He et al.KDD 2025

Builds on11

  1. CodeBERT: A Pre-Trained Model for Programming and Natural LanguagesZhangyin Feng, Daya Guo, Duyu Tang, Nan Duan et al.EMNLP Findings 2020 · 2,828 citations
  2. Understanding Contrastive Representation Learning through Alignment and Uniformity on the HypersphereTongzhou Wang, Phillip IsolaICML 2020 · 2,360 citations
  3. GraphCodeBERT: Pre-training Code Representations with Data FlowDaya Guo, Shuo Ren, Shuai Lu, Zhangyin Feng et al.ICLR 2021 · 1,644 citations
  4. On the Sentence Embeddings from Pre-trained Language ModelsBohan Li, Hao Zhou, Junxian He, Mingxuan Wang et al.EMNLP 2020 · 538 citations
  5. Debiased Contrastive Learning of Unsupervised Sentence RepresentationsKun Zhou, Beichen Zhang, Wayne Xin Zhao, Ji-Rong WenACL 2022 · 128 citations

Related papers

  1. Vision Language Pre-training by Contrastive Learning with Cross-Modal Similarity RegulationChaoya Jiang, Wei Ye, Haiyang Xu, Songfang Huang et al.ACL 2023 · 7 citations
  2. Empowering Collaborative Filtering with Principled Adversarial Contrastive LossAn Zhang, Leheng Sheng, Zhibo Cai, Xiang Wang et al.NeurIPS 2023 · 56 citations
  3. Contrastive Predictive Coding Done Right for Mutual Information EstimationJongha Ryu, Pavan Yeddanapudi, Xiangxiang Xu, Gregory W. WornellICLR 2026 · 1 citation
  4. Does GCL Need a Large Number of Negative Samples? Enhancing Graph Contrastive Learning with Effective and Efficient Negative SamplingYongqi Huang, Jitao Zhao, Dongxiao He, Di Jin et al.AAAI 2025 · 11 citations
  5. A New Mechanism for Eliminating Implicit Conflict in Graph Contrastive LearningDongxiao He, Jitao Zhao, Cuiying Huo, Yongqi Huang et al.AAAI 2024 · 21 citations
Dusk over the sea between two cliffs drawn in fine vertical lines