arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2204.10200cs.SEcs.AIcs.CLcs.PL
收藏

BERT中代码注意的探索性研究

An Exploratory Study on Code Attention in BERT

University of British Columbia(不列颠哥伦比亚大学);Singapore Management University(新加坡管理大学)

Rishab Sharma, Fuxiang Chen, Fatemeh Fard, David Lo

摘要:

软件工程中的许多最新模型都引入了基于Transformer架构的深度神经模型,或者使用基于transformer的预训练语言模型(PLM)。虽然这些模型实现了许多下游任务(如代码摘要和错误检测)的最新技术水平,但它们是基于Transformer和PLM的,这些模型主要在自然语言处理(NLP)领域进行研究。尽管自然语言和程序设计语言之间存在差异,但目前的研究依赖于NLP对代码中这些模型的推理和实践。关于解释代码如何建模的文献也很有限。在这里,我们调查的注意力行为的PLM的代码,并比较它与自然语言。我们对BERT(一个基于Transformer的PLM)进行了代码预训练,并探索了它学习的信息类型,包括语义和语法。我们运行了几个实验来分析代码构造对彼此的注意力值以及BERT在每一层中学习到的内容。我们的分析表明,BERT更关注语法实体,特别是标识符和分隔符,而不是NLP中最受关注的标记[CLS]。这一观察促使我们在用于代码克隆检测时利用标识符来表示代码序列,而不是[CLS]标记。我们的研究结果表明,采用嵌入标识符的BERT的性能提高了605%和4%的F1分数在其较低的层和上层,分别。在基于代码的PLM CodeBERT中使用标识符的嵌入,克隆检测的F1得分提高了21-24%。这些发现可以通过使用特定于代码的表示而不是应用NLP中使用的常见嵌入来使研究社区受益,并为开发具有类似性能的较小模型开辟新的方向。

英文摘要:

Many recent models in software engineering introduced deep neural models based on the Transformer architecture or use transformer-based Pre-trained Language Models (PLM) trained on code. Although these models achieve the state of the arts results in many downstream tasks such as code summarization and bug detection, they are based on Transformer and PLM, which are mainly studied in the Natural Language Processing (NLP) field. The current studies rely on the reasoning and practices from NLP for these models in code, despite the differences between natural languages and programming languages. There is also limited literature on explaining how code is modeled. Here, we investigate the attention behavior of PLM on code and compare it with natural language. We pre-trained BERT, a Transformer based PLM, on code and explored what kind of information it learns, both semantic and syntactic. We run several experiments to analyze the attention values of code constructs on each other and what BERT learns in each layer. Our analyses show that BERT pays more attention to syntactic entities, specifically identifiers and separators, in contrast to the most attended token [CLS] in NLP. This observation motivated us to leverage identifiers to represent the code sequence instead of the [CLS] token when used for code clone detection. Our results show that employing embeddings from identifiers increases the performance of BERT by 605% and 4% F1-score in its lower layers and the upper layers, respectively. When identifiers' embeddings are used in CodeBERT, a code-based PLM, the performance is improved by 21-24% in the F1-score of clone detection. The findings can benefit the research community by using code-specific representations instead of applying the common embeddings used in NLP, and open new directions for developing smaller models with similar performance.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑