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 about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Your agent calls
Luneget_paper_fulltext
Free to start. No credit card required.
Terminal
Install the CLIlune papers fulltext 93c16853-827d-47ee-8885-86bd464110f1Cited by top-tier papers6
- Rewriting the Code: A Simple Method for Large Language Model Augmented Code SearchHaochen Li, Xin Zhou, Zhiqi ShenACL 2024 · 10 citations
- 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
- AlignCoder: Aligning Retrieval with Target Intent for Repository-Level Code CompletionTianyue Jiang, Yanlin Wang, Yanli Wang, Daya Guo et al.ASE 2025 · 2 citations
- Uncertainty-Aware Contrastive Learning with Hard Negative Sampling for Code Search TasksHan Liu, Jiaqing Zhan, Qin ZhangAAAI 2025 · 1 citation
- MGS3: A Multi-Granularity Self-Supervised Code Search FrameworkRui Li, Junfeng Kang, Qi Liu, Liyang He et al.KDD 2025
Builds on9
- Understanding Contrastive Representation Learning through Alignment and Uniformity on the HypersphereTongzhou Wang, Phillip IsolaICML 2020 · 2,360 citations
- GraphCodeBERT: Pre-training Code Representations with Data FlowDaya Guo, Shuo Ren, Shuai Lu, Zhangyin Feng et al.ICLR 2021 · 1,644 citations
- On the Sentence Embeddings from Pre-trained Language ModelsBohan Li, Hao Zhou, Junxian He, Mingxuan Wang et al.EMNLP 2020 · 538 citations
- Debiased Contrastive Learning of Unsupervised Sentence RepresentationsKun Zhou, Beichen Zhang, Wayne Xin Zhao, Ji-Rong WenACL 2022 · 128 citations
- Self-Supervised Contrastive Learning for Code Retrieval and Summarization via Semantic-Preserving TransformationsNghi D. Q. Bui, Yijun Yu, Lingxiao JiangSIGIR 2021 · 98 citations
Related papers
- 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
- Empowering Collaborative Filtering with Principled Adversarial Contrastive LossAn Zhang, Leheng Sheng, Zhibo Cai, Xiang Wang et al.NeurIPS 2023 · 56 citations
- Contrastive Predictive Coding Done Right for Mutual Information EstimationJongha Ryu, Pavan Yeddanapudi, Xiangxiang Xu, Gregory W. WornellICLR 2026 · 1 citation
- 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
- A New Mechanism for Eliminating Implicit Conflict in Graph Contrastive LearningDongxiao He, Jitao Zhao, Cuiying Huo, Yongqi Huang et al.AAAI 2024 · 21 citations
