[论文解读] code2seq: Generating Sequences from Structured Representations of Code
该论文提出 code2seq,一种新颖的 seq2seq 模型,通过其抽象语法树(AST)中的组合路径编码源代码,实现更准确的自然语言序列生成,用于代码摘要和文档生成。该模型在两种编程语言的四个数据集上均优于先前模型,通过在 AST 路径上使用注意力机制,实现了当前最优的结果。
The ability to generate natural language sequences from source code snippets has a variety of applications such as code summarization, documentation, and retrieval. Sequence-to-sequence (seq2seq) models, adopted from neural machine translation (NMT), have achieved state-of-the-art performance on these tasks by treating source code as a sequence of tokens. We present ${ m {\scriptsize CODE2SEQ}}$: an alternative approach that leverages the syntactic structure of programming languages to better encode source code. Our model represents a code snippet as the set of compositional paths in its abstract syntax tree (AST) and uses attention to select the relevant paths while decoding. We demonstrate the effectiveness of our approach for two tasks, two programming languages, and four datasets of up to $16$M examples. Our model significantly outperforms previous models that were specifically designed for programming languages, as well as state-of-the-art NMT models. An interactive online demo of our model is available at this http URL. Our code, data and trained models are available at this http URL.
研究动机与目标
- 通过捕捉源代码中的语法结构,改进代码到文本的生成,超越对词元序列的建模。
- 解决现有 seq2seq 模型将代码视为扁平词元序列的局限性,忽略程序结构。
- 通过从抽象语法树(AST)中提取组合路径,开发一种学习代码有意义表示的模型。
- 在多种编程语言和数据集上证明结构化代码表示的有效性。
提出的方法
- 将代码片段表示为从其抽象语法树(AST)中提取的一组组合路径,其中每条路径捕捉节点之间的语法关系。
- 使用深度神经网络将每条路径嵌入到稠密向量表示中,保留语义和语法信息。
- 在解码过程中应用注意力机制,选择性地关注相关 AST 路径,实现上下文感知的序列生成。
- 使用序列到序列框架和交叉熵损失端到端训练模型,用于自然语言生成。
- 利用 AST 的层次性和结构性,提升代码生成中的泛化能力和表示学习。
- 将模型扩展至最大达 1600 万条样本的数据集,实现对多样化编程任务的稳健性能。
实验结果
研究问题
- RQ1与扁平词元序列相比,将代码建模为组合 AST 路径是否能提升代码到文本的生成效果?
- RQ2对 AST 路径的注意力机制如何增强模型生成准确且相关自然语言描述的能力?
- RQ3所提出的结构化表示是否能在不同编程语言和代码生成任务中实现泛化?
- RQ4在性能和鲁棒性方面,code2seq 与当前最优的神经机器翻译(NMT)和编程专用模型相比如何?
主要发现
- code2seq 在代码摘要任务中显著优于先前为编程语言专门设计的模型。
- 该模型在涵盖两种编程语言的四个数据集上均达到当前最优性能,展现出强大的泛化能力。
- 通过利用 AST 路径和注意力机制,code2seq 学习到的代码表示比仅依赖词元序列的模型更具语义意义。
- 该模型在大规模数据上表现稳健,即使在最大达 1600 万条样本的数据集上也能实现高精度。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。