LEARNING TO REPRESENT PROGRAMS WITH HETEROGENEOUS GRAPHS 学会用异构图表示程序(从AST中构建异构图)

大多数已有的研究是以抽象语法树来表示源码信息,还有一些研究是向AST中添加额外的边把源码转换成图的形式,然后利用神经网络学习程序图的表示。尽管这些工作提供了额外的控制或数据流信息向AST发送任务,但他们忽略了AST本身结构信息的重要方面:节点和边的不同类型

为了解决节点和边类型的信息,考虑使用异构图学习源码,提出一个新的构建公式,从带有额外节点和边的类型信息的AST中建立异构图使用程序语言的ASDL语法定义程序图的节点和边类型,然后使用异构图神经网络训练这些图。在两项任务上评估我们的方法:代码注释生成和方法命名。(两个不同领域的Python数据集)这两个任务都需要理解完整代码段的语义

很多研究使用AST表明代码结构信息,但AST的一个问题是,它们不能明确反映语法依赖之外的结构信息,如控制和数据流。一个可行的解决方案是在AST上添加不同类型的控制和数据流的边以生成程序图,并在程序图上应用图神经网络(GNN)来学习它们的表示。然而,这些方法不考虑除了控制或数据流的边之外,原始AST的节点和边的类型也不同。

我们的向AST中添加节点和边的类型的想法与异构图的概念是一致的。使用抽象语法描述语言获取AST上的节点和类型信息。

在获取到代码段的异构图之后,需要找到一种图神经网络(GNN)去表示这些图。我们把目光转向图嵌入领域。异构图神经网络被广泛使用在图嵌入中,不像传统的图神经网络,异构图神经网络能够在消息传递节点集成节点和边的类型信息并把节点的不同类型映射到不同的特征空间。在异构图上使用异构图转换器(HGT)计算程序表示。

贡献如下:1)使用异构图表示程序,并在源代码段上使用异构图神经网络

                  2)使用ASDL语法从程序的AST上建立异构程序图

                  3)在两个不同的任务上评估此方法,这两个任务涉及对源代码片段的图形级预测。我们的方法在注释生成和方法命名任务这两方面都优于其他GNN模型

方法

从源码中创建异构程序图(HPG),如何在异构图上应用异构图神经网络

异构程序图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Alright, I understand your requirements. Here's a Python program that satisfies your specifications: ```python import queue # Define the 5x5 boolean two-dimensional array to represent the connectivity of the graphs graph = [ [False, True, False, True, False], [True, False, True, False, True], [False, True, False, True, False], [True, False, True, False, True], [False, True, False, True, False], ] # Define the depth first traversal function def depth_first_traversal(graph, start): visited = [False] * len(graph) stack = [start] while stack: vertex = stack.pop() if not visited[vertex]: print(vertex, end=" ") visited[vertex] = True for i in range(len(graph)): if graph[vertex][i] and not visited[i]: stack.append(i) # Define the breadth first traversal function def breadth_first_traversal(graph, start): visited = [False] * len(graph) q = queue.Queue() q.put(start) while not q.empty(): vertex = q.get() if not visited[vertex]: print(vertex, end=" ") visited[vertex] = True for i in range(len(graph)): if graph[vertex][i] and not visited[i]: q.put(i) # Choose a reasonable starting position for the traversal based on the connectivity of the graph start = 0 for i in range(len(graph)): if sum(graph[i]) > sum(graph[start]): start = i # Perform depth first traversal print("Depth First Traversal:") depth_first_traversal(graph, start) # Perform breadth first traversal print("\nBreadth First Traversal:") breadth_first_traversal(graph, start) ``` In this program, we first define the 5x5 boolean two-dimensional array `graph` to represent the connectivity of the graphs that need to be traversed. Then, we define the `depth_first_traversal` function and the `breadth_first_traversal` function to perform depth first and breadth first traversal of the graph respectively. Next, we choose a reasonable starting position for the traversal based on the connectivity of the graph. We do this by iterating over each vertex in the graph and comparing the number of its neighbors to the number of neighbors of the current starting vertex. We choose the vertex with the highest number of neighbors as the starting position. Finally, we call the `depth_first_traversal` function and the `breadth_first_traversal` function with the `graph` and `start` arguments to perform the traversals. The output of the program will be the sequence of vertices visited during the traversals.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值