Skip to main content
QUICK REVIEW

[论文解读] Pre-trained Contextual Embedding of Source Code

Aditya Kanade, Petros Maniatis|arXiv (Cornell University)|Sep 25, 2019
Software Engineering Research参考文献 28被引用 41
一句话总结

本文提出了代码理解 BERT(CuBERT),一种针对源代码的预训练上下文嵌入模型,基于 GitHub 上大规模 Python 程序语料库进行训练。通过将 BERT 的自注意力机制适配到代码上,CuBERT 在五个下游分类任务中实现了最先进性能,相比基线模型提升 2.9%–22%,尤其在有限微调数据和训练轮次下表现更优。

ABSTRACT

The source code of a program not only serves as a formal description of an executable task, but it also serves to communicate developer intent in a human-readable form. To facilitate this, developers use meaningful identifier names and natural-language documentation. This makes it possible to successfully apply sequence-modeling approaches, shown to be effective in natural-language processing, to source code. A major advancement in natural-language understanding has been the use of pre-trained token embeddings; BERT and other works have further shown that pre-trained contextual embeddings can be extremely powerful and can be fine-tuned effectively for a variety of downstream supervised tasks. Inspired by these developments, we present the first attempt to replicate this success on source code. We curate a massive corpus of Python programs from GitHub to pre-train a BERT model, which we call Code Understanding BERT (CuBERT). We also pre-train Word2Vec embeddings on the same dataset. We create a benchmark of five classification tasks and compare fine-tuned CuBERT against sequence models trained with and without the Word2Vec embeddings. Our results show that CuBERT outperforms the baseline methods by a margin of 2.9-22%. We also show its superiority when fine-tuned with smaller datasets, and over fewer epochs. We further evaluate CuBERT's effectiveness on a joint classification, localization and repair task involving prediction of two pointers.

研究动机与目标

  • 探索预训练上下文嵌入(在自然语言处理中表现成功)是否可有效应用于源代码。
  • 解决在下游自然语言处理风格任务中捕捉代码语法和语义信息的挑战。
  • 开发一种模型,利用代码中的自然语言模式(如有意义的标识符和文档注释)以提升表征学习效果。
  • 评估模型在多样化代码理解任务(包括分类、定位和修复)中的性能。
  • 证明在大规模代码语料库上进行预训练,对代码智能应用中的迁移学习具有显著效果。

提出的方法

  • 从 GitHub 收集并整理了 160 万个 Python 文件的大型语料库,用于在真实世界代码上预训练模型。
  • 基于 BERT 架构进行微调,采用适配于代码的掩码语言建模和下一句预测目标。
  • 在同一数据集上预训练 Word2Vec 嵌入作为对比基线。
  • 设计了一个包含五个不同代码分类任务的基准,用于评估模型性能。
  • 通过在下游任务中使用少量标注数据和较少训练轮次对 CuBERT 进行微调,应用迁移学习。
  • 在联合任务上评估 CuBERT,该任务包含分类、代码定位的指针预测和修复预测。

实验结果

研究问题

  • RQ1像 BERT 这类预训练上下文嵌入是否可有效适配于源代码表征学习?
  • RQ2在代码分类任务中,CuBERT 与使用 Word2Vec 嵌入及其他序列模型的基线模型相比表现如何?
  • RQ3在有限标注数据下,大规模代码预训练在下游任务中能将性能提升到何种程度?
  • RQ4CuBERT 在涉及分类、定位和修复的多任务学习场景中是否具备良好泛化能力?
  • RQ5当使用更少训练轮次或更小数据集进行微调时,CuBERT 是否仍保持性能优势?

主要发现

  • CuBERT 在五个分类任务中均优于所有基线模型,F1 分数提升范围为 2.9% 至 22%。
  • 即使在微调较小数据集时,模型仍表现出卓越性能,表明预训练带来了强大的泛化能力。
  • CuBERT 在更少训练轮次下取得更优结果,凸显其在微调阶段的高效性。
  • 在联合分类、定位和修复任务中,模型表现出色,成功预测出两个指针。
  • 在大规模代码语料库上进行预训练显著提升了表征质量,优于未进行此类预训练的模型。
  • 代码中自然语言模式(如有意义的标识符和文档注释)的整合对方法的成功至关重要。

更好的研究,从现在开始

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

无需绑定信用卡

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