Skip to main content
QUICK REVIEW

[论文解读] Code Attention: Translating Code to Comments by Exploiting Domain Features

Wenhao Zheng, Hong-Yu Zhou|arXiv (Cornell University)|Sep 22, 2017
Software Engineering Research参考文献 31被引用 12
一句话总结

本文提出 Code Attention,一种新颖的注意力机制,通过显式建模标识符、符号和程序结构等特定领域特征,提升了代码到注释的生成效果。通过将这些特征整合到序列到序列的 RNN 框架中,该方法在 BLEU、METEOR 和人工评估上均达到最先进性能,在一个新的大规模数据集 C2CGit 上优于现有方法。

ABSTRACT

Appropriate comments of code snippets provide insight for code functionality, which are helpful for program comprehension. However, due to the great cost of authoring with the comments, many code projects do not contain adequate comments. Automatic comment generation techniques have been proposed to generate comments from pieces of code in order to alleviate the human efforts in annotating the code. Most existing approaches attempt to exploit certain correlations (usually manually given) between code and generated comments, which could be easily violated if the coding patterns change and hence the performance of comment generation declines. In this paper, we first build C2CGit, a large dataset from open projects in GitHub, which is more than 20$ imes$ larger than existing datasets. Then we propose a new attention module called Code Attention to translate code to comments, which is able to utilize the domain features of code snippets, such as symbols and identifiers. We make ablation studies to determine effects of different parts in Code Attention. Experimental results demonstrate that the proposed module has better performance over existing approaches in both BLEU and METEOR.

研究动机与目标

  • 为解决开源代码中注释不足的问题,该问题会阻碍程序理解与维护。
  • 克服现有注释生成方法依赖僵化、手动定义的代码-注释关联性的问题,这些关联性在编码模式变化时容易失效。
  • 通过利用代码的内在领域特征,开发一种更鲁棒且可泛化的代码到注释转换方法。
  • 构建一个大规模、真实世界的基准数据集,以支持并验证未来在代码注释生成方面的研究。
  • 证明显式建模代码结构和语义特征可生成更准确且功能正确的注释。

提出的方法

  • 提出一种名为 Code Attention 的新型注意力机制,将领域特定特征(如标识符、符号和控制结构)整合到注意力计算中。
  • 设计三阶段流程:标识符排序以优先处理有意义的标记,标记编码以嵌入代码元素,全局注意力以对齐代码特征与自然语言输出。
  • 采用三层 RNN 编码器-解码器架构,结合束搜索推理,使用随机梯度下降在 C2CGit 数据集上进行训练。
  • 使用分桶机制处理可变长度的代码与注释对,通过 10 折交叉验证选择最优桶大小:(40,15)、(55,20)、(70,40)、(220,60)。
  • 应用自适应学习率衰减(初始值 0.5,每 3000 次无下降步长衰减 0.99),以提升训练稳定性和收敛性。
  • 利用预训练词嵌入(512 维)和 1024 维隐藏单元,以捕捉代码与注释中的语义和句法模式。

实验结果

研究问题

  • RQ1是否一种显式建模代码特定特征(如标识符和符号)的神经注意力机制能够提升代码到注释的生成效果?
  • RQ2将程序结构(如循环、条件语句)整合到注意力机制中,是否能生成更功能准确的注释?
  • RQ3所提方法在自动指标和人工评估上与现有基线相比表现如何?
  • RQ4像 C2CGit 这样更大规模的真实世界数据集在多大程度上提升了代码注释生成模型的泛化能力和可靠性?
  • RQ5Code Attention 模块中每个组件对整体性能的贡献是什么?

主要发现

  • 所提出的 Code Attention 模型在自动指标上均达到最先进性能,BLEU 和 METEOR 分数全面优于所有基线模型。
  • 人工评估证实,Code Attention 生成的注释比现有方法更准确,且与代码功能在语义上更一致。
  • 消融实验表明,Code Attention 的每个组件——标识符排序、标记编码和全局注意力——均对性能提升有显著贡献。
  • C2CGit 数据集包含超过 1000 个开源项目,比以往基准大 20 多倍,使模型评估更加稳健。
  • 该模型能正确反映结构语义(例如区分 'swap' 与 'shift'),而简单模型则会遗漏这些细节。
  • 在单张 NVIDIA K80 GPU 上训练模型约耗时 3 天,共 90,000 次迭代,表明该架构具有可行的训练效率。

更好的研究,从现在开始

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

无需绑定信用卡

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