Skip to main content
QUICK REVIEW

[论文解读] Learning Deep Semantic Model for Code Search using CodeSearchNet Corpus

Chen Wu, Ming Yan|arXiv (Cornell University)|Jan 27, 2022
Software Engineering Research被引用 6
一句话总结

本文提出了一种用于代码搜索的深度语义模型,该模型利用多模态学习、自注意力池化和融合表示,弥合了自然语言查询与代码片段之间的语义鸿沟。该模型在CodeSearchNet语料库上采用大批次训练和困难负样本挖掘进行训练,实现了0.384的SOTA NDCG得分,在基准挑战赛中获得第一名。

ABSTRACT

Semantic code search is the task of retrieving relevant code snippet given a natural language query. Different from typical information retrieval tasks, code search requires to bridge the semantic gap between the programming language and natural language, for better describing intrinsic concepts and semantics. Recently, deep neural network for code search has been a hot research topic. Typical methods for neural code search first represent the code snippet and query text as separate embeddings, and then use vector distance (e.g. dot-product or cosine) to calculate the semantic similarity between them. There exist many different ways for aggregating the variable length of code or query tokens into a learnable embedding, including bi-encoder, cross-encoder, and poly-encoder. The goal of the query encoder and code encoder is to produce embeddings that are close with each other for a related pair of query and the corresponding desired code snippet, in which the choice and design of encoder is very significant. In this paper, we propose a novel deep semantic model which makes use of the utilities of not only the multi-modal sources, but also feature extractors such as self-attention, the aggregated vectors, combination of the intermediate representations. We apply the proposed model to tackle the CodeSearchNet challenge about semantic code search. We align cross-lingual embedding for multi-modality learning with large batches and hard example mining, and combine different learned representations for better enhancing the representation learning. Our model is trained on CodeSearchNet corpus and evaluated on the held-out data, the final model achieves 0.384 NDCG and won the first place in this benchmark. Models and code are available at https://github.com/overwindows/SemanticCodeSearch.git.

研究动机与目标

  • 解决代码搜索中自然语言查询与代码片段之间的语义鸿沟问题。
  • 通过联合学习多种编程语言之间的跨语言嵌入,提升代码搜索性能。
  • 通过中间代码和查询嵌入的特征融合与自注意力池化,增强表示学习。
  • 设计一种高效且有效的模型架构,适用于真实世界中的代码搜索应用。
  • 在多语言语义代码搜索的CodeSearchNet基准上实现SOTA性能。

提出的方法

  • 采用双编码器架构以实现高效的代码搜索,为查询和代码片段分别设置独立的编码器。
  • 应用自注意力池化,利用可学习的注意力权重将可变长度的代码和查询表示聚合为单一向量。
  • 通过可学习的、语言特定的权重,融合编码器中的多个中间表示,生成最终的融合表示。
  • 采用大批次训练和困难负样本挖掘,以提升泛化能力和表示质量。
  • 利用TreeSitter和BPE分词技术解析和分词代码与文档,减少词汇量。
  • 使用对比损失函数进行模型训练,最大化正样本对(查询,代码)之间的余弦相似度,同时最小化负样本对之间的相似度。

实验结果

研究问题

  • RQ1与标准池化方法相比,中间表示的自注意力池化是否能提升代码搜索性能?
  • RQ2通过可学习权重融合多个编码器层,是否能带来更好的语义表示学习效果?
  • RQ3采用大批次训练结合困难负样本挖掘,是否能显著提升多语言代码搜索中的模型泛化能力?
  • RQ4跨语言嵌入对齐在提升不同编程语言之间的零样本或少样本代码搜索方面有多有效?
  • RQ5结合多模态学习与融合和注意力池化等架构创新,相较于标准神经代码搜索基线模型,其性能提升程度如何?

主要发现

  • 所提出的模型在CodeSearchNet基准上实现了0.384的平均NDCG得分,优于竞赛中所有其他模型。
  • 采用自注意力池化与融合表示的模型(NBoW+Weighted)在测试集上达到最高性能,NDCG得分为0.3841。
  • 消融实验证实,与基线NBoW模型相比,自注意力池化和表示融合均对性能提升有显著贡献。
  • 该模型在六种编程语言(Go、Java、JavaScript、PHP、Python和Ruby)上均表现出强大的泛化能力。
  • 大批次训练与困难负样本挖掘的结合,显著提升了表示学习质量,并增强了对噪声或模糊查询的鲁棒性。
  • 最终模型已公开发布于GitHub,支持可复现性,并推动语义代码搜索领域的进一步研究。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。