[论文解读] Learning to Represent Programs with Heterogeneous Graphs
本文提出了一种异构程序图(HPG),显式建模源代码中的节点类型和边类型,从而实现更精确的代码表示。通过在HPG上应用异构图变换器(HGT),该方法在方法名预测和代码分类任务上优于SOTA基线模型,显著提升了异构类型信息和手动构建边的性能表现。
Program source code contains complex structure information, which can be represented in structured data forms like trees or graphs. To acquire the structural information in source code, most existing researches use abstract syntax trees (AST). A group of works add additional edges to ASTs to convert source code into graphs and use graph neural networks to learn representations for program graphs. Although these works provide additional control or data flow information to ASTs for downstream tasks, they neglect an important aspect of structure information in AST itself: the different types of nodes and edges. In ASTs, different nodes contain different kinds of information like variables or control flow, and the relation between a node and all its children can also be different. To address the information of node and edge types, we bring the idea of heterogeneous graphs to learning on source code and present a new formula of building heterogeneous program graphs from ASTs with additional type information for nodes and edges. We use the ASDL grammar of programming language to define the node and edge types of program graphs. Then we use heterogeneous graph neural networks to learn on these graphs. We evaluate our approach on two tasks: code comment generation and method naming. Both tasks require reasoning on the semantics of complete code snippets. Experiment results show that our approach outperforms baseline models, including homogeneous graph-based models, showing that leveraging the type information of nodes and edges in program graphs can help in learning program semantics.
研究动机与目标
- 为解决同质图在代码表示中的局限性,即丢弃节点和边的语义类型信息。
- 通过显式建模程序结构中的节点类型和边类型,改进代码表示学习。
- 构建一种框架,利用异构图建模捕捉源代码中复杂的结构与语义关系。
- 证明异构类型信息以及手动构建的边在提升代码处理任务模型性能方面的有效性。
提出的方法
- 利用ASDL语法定义,从抽象语法树(ASTs)中编码节点类型和边类型,构建异构程序图(HPG)。
- 引入手动构建的边,如NextSib和NextToken,以保留代码中的结构与语法顺序。
- 采用异构图变换器(HGT)架构处理HPG,实现跨不同节点类型与边类型的类型感知消息传递。
- 在解码策略中仅对子词节点进行操作,以提升方法名预测等生成任务的性能。
- 使用节点类型与边类型的嵌入向量引导HGT中的注意力机制,使模型能够区分代码中不同语义角色。
- 在标准基准数据集上,端到端训练HPG+HGT模型用于代码表示任务。
实验结果
研究问题
- RQ1在程序图中引入异构节点与边类型,如何影响代码表示的质量?
- RQ2手动构建的边(如NextSib、NextToken)对模型性能的贡献如何?
- RQ3与同质图模型相比,HGT架构如何从异构类型信息中获益?
- RQ4在子词节点上进行解码的策略对生成任务有何影响?
- RQ5若移除节点类型或边类型,模型性能会下降到何种程度?
主要发现
- HPG+HGT模型在四个基准数据集上的代码分类与方法名预测任务中,均优于SOTA基线模型。
- 从HPG中移除节点类型或边类型,F1得分均下降约7%,表明二者在表示学习中具有关键作用。
- 当同时移除节点类型与边类型,使HPG变为同质图时,F1得分下降8%,证实异质性的重要性。
- 移除手动构建的NextSib与NextToken边,分别导致准确率下降1.4%与1.0%,且p值均小于0.0002,表明具有强统计显著性。
- 若在所有节点而非仅子词节点上进行解码,F1得分下降约4.6%,验证了仅对子词节点解码的设计选择的合理性。
- GGNN模型也受益于边类型信息,当使用异构边时,F1得分提升3%,表明即使基线GNN模型也能从类型感知图中获益。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。