Skip to main content
QUICK REVIEW

[论文解读] BERT2Code: Can Pretrained Language Models be Leveraged for Code Search?

Abdullah Al Ishtiaq, Masum Hasan|arXiv (Cornell University)|Apr 16, 2021
Software Engineering Research参考文献 36被引用 6
一句话总结

BERT2Code 提出了一种轻量级的两层神经网络,利用预训练的句子嵌入和代码嵌入模型——SBERT、Code2Vec 和 CodeBERT——进行语义代码搜索。它通过学习跨嵌入空间关系,在代码搜索任务上实现了 15.478% 的 MRR,但发现代码嵌入质量是性能的主要瓶颈。

ABSTRACT

Millions of repetitive code snippets are submitted to code repositories every day. To search from these large codebases using simple natural language queries would allow programmers to ideate, prototype, and develop easier and faster. Although the existing methods have shown good performance in searching codes when the natural language description contains keywords from the code, they are still far behind in searching codes based on the semantic meaning of the natural language query and semantic structure of the code. In recent years, both natural language and programming language research communities have created techniques to embed them in vector spaces. In this work, we leverage the efficacy of these embedding models using a simple, lightweight 2-layer neural network in the task of semantic code search. We show that our model learns the inherent relationship between the embedding spaces and further probes into the scope of improvement by empirically analyzing the embedding methods. In this analysis, we show that the quality of the code embedding model is the bottleneck for our model's performance, and discuss future directions of study in this area.

研究动机与目标

  • 探究预训练语言模型是否可被有效用于语义代码搜索。
  • 评估轻量级神经网络在使用预训练嵌入将自然语言查询映射到源代码方面的性能。
  • 识别当前代码嵌入模型在实现高精度语义代码搜索方面的局限性。
  • 实证分析句子和代码嵌入在捕捉语义相似性方面的质量。
  • 强调未来研究中需要更通用且质量更高的代码嵌入模型。

提出的方法

  • 微调 Sentence-BERT (SBERT) 以生成自然语言查询的稠密向量表示。
  • 使用 Code2Vec 和 CodeBERT 从源代码片段生成代码嵌入。
  • 采用两层前馈神经网络,在共享潜在空间中将句子嵌入映射到代码嵌入。
  • 使用对比学习和三元组损失进行模型训练,以最大化匹配查询-代码对之间的语义相似度。
  • 在 DeepCom 数据集上使用均 reciprocally rank (MRR) 评估模型性能。
  • 对 300 对查询-代码对进行人工语义相似度评分,并将其与嵌入空间中的 L2 距离进行相关性分析。

实验结果

研究问题

  • RQ1像 SBERT 和 Code2Vec 这样的预训练语言模型能否被有效结合用于语义代码搜索?
  • RQ2轻量级神经网络在将自然语言查询嵌入映射到代码嵌入方面表现如何?
  • RQ3人工标注的语义相似度与嵌入空间距离之间的相关性如何?
  • RQ4代码嵌入的质量和泛化能力在多大程度上限制了语义代码搜索模型的性能?
  • RQ5当前的代码嵌入模型是否足以充分发挥跨嵌入映射架构的潜力?

主要发现

  • BERT2Code 在 DeepCom 数据集上实现了 15.478% 的均 reciprocally rank (MRR),证明了使用预训练嵌入进行语义代码搜索的可行性。
  • 人工语义相似度评分与自然语言嵌入空间中 L2 距离的相关系数为 -0.772(p < 10^-6),表明人类判断与嵌入相似度之间具有强烈一致性。
  • 人工语义相似度与代码嵌入距离之间的相关系数仅为 -0.444(p < 10^-6),表明代码嵌入在捕捉语义含义方面不如句子嵌入可靠。
  • 统计分析确认,代码嵌入相关性低并非偶然所致,原假设在高置信度下被拒绝。
  • 该研究将代码嵌入模型的质量确定为性能的主要瓶颈,表明未来改进必须依赖更优质的代码嵌入。
  • 实证分析表明,当前模型如 Code2Vec 和 CodeBERT 在语义代码搜索任务中泛化能力不足,尤其是在语义含义至关重要的情况下。

更好的研究,从现在开始

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

无需绑定信用卡

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