一句话:提出 code2vec,将代码片段表示为定长向量,并通过预测方法名验证其语义表示能力。
本文研究如何为代码片段学习连续分布式表示,即 code embeddings,以支持预测代码语义属性。方法上,code2vec 首先将代码分解为抽象语法树中的一组路径,神经网络在学习每条路径原子表示的同时,学习如何聚合这些路径,最终得到单个定长代码向量。作者以从方法体预测方法名为任务,在包含 1200 万个方法的数据集上训练模型。实验表明,模型能从训练中未出现过的文件预测方法名,并且学习到能捕捉语义相似性、组合和类比关系的方法名向量。与先前技术在同一数据集上相比,该方法提升超过 75%,成为首个能基于大规模跨项目语料成功预测方法名的工作。
We present a neural model for representing snippets of code as continuous distributed vectors (``code embeddings''). The main idea is to represent a code snippet as a single fixed-length code vector, which can be used to predict semantic properties of the snippet. To this end, code is first decomposed to a collection of paths in its abstract syntax tree. Then, the network learns the atomic representation of each path while simultaneously learning how to aggregate a set of them. We demonstrate the effectiveness of our approach by using it to predict a method's name from the vector representation of its body. We evaluate our approach by training a model on a dataset of 12M methods. We show that code vectors trained on this dataset can predict method names from files that were unobserved during training. Furthermore, we show that our model learns useful method name vectors that capture semantic similarities, combinations, and analogies. A comparison of our approach to previous techniques over the same dataset shows an improvement of more than 75%, making it the first to successfully predict method names based on a large, cross-project corpus. Our trained model, visualizations and vector similarities are available as an interactive online demo at http://code2vec.org. The code, data and trained models are available at https://github.com/tech-srl/code2vec.
@article{paperbot634,
title = {code2vec: learning distributed representations of code},
author = {Uri Alon and Meital Zilberstein and Omer Levy and Eran Yahav},
journal = {Proceedings of the ACM on Programming Languages},
volume = {3},
number = {POPL},
year = {2019},
doi = {10.1145/3290353}
}