AI图神经网络:从GCN到Graph Transformer的演进
📅 2026/7/15 21:55:56
👁️ 阅读次数
📝 编程学习
AI图神经网络:从GCN到Graph Transformer的演进
图神经网络(Graph Neural Network, GNN)是处理非欧几里得数据的核心技术,在社交网络、分子发现、推荐系统、知识图谱等领域展现出独特优势。本文将系统梳理GNN的技术演进,从谱域方法到空间方法,再到最新的Graph Transformer,解析各类架构的原理与适用场景。
一、图数据与图学习的挑战
1.1 图数据的特性
import torch import torch.nn as nn class GraphData: """图数据的表示""" def __init__(self, num_nodes, edge_index, node_features=None): """ num_nodes: 节点数量 edge_index: [2, num_edges] 边的连接关系 node_features: [num_nodes, feature_dim] 节点特征 """ self.num_nodes = num_nodes self.edge_index = edge_index self.x = node_features # 构建邻接矩阵 self.adj = self.build_adjacency() def build_adjacency(self): """构建稀疏邻接矩阵""" adj = torch.zeros(self.num_nodes, self.num_nodes) for i in range(self.edge_index.size(1)): src, dst = self.edge_index[0, i], self.edge_index[1, i] adj[src, dst] = 1 return adj def normalize_adjacency(self): """对称归一化邻接矩阵: D^(-1/2) A D^(-1/2)""" # 度矩阵 degree = self.adj.sum(dim=1) degree_inv_sqrt = torch.pow(degree, -0.5) degree_inv_sqrt[torch.isinf(degree_inv_sqrt)] = 0 # 归一化 D_inv_sqrt = torch.diag(degree_inv_sqrt) normalized = D_inv_sqrt @ self.adj @ D_inv_sqrt return normalized1.2 图学习的核心任务
| 任务级别 | 任务描述 | 输出 | 示例 | |----------|----------|------|------| | 节点级 | 预测节点属性 | 每个节点一个标签 | 用户分类 | | 边级 | 预测边是否存在/属性 | 每条边一个标签 | 链接预测 | | 图级 | 预测整个图的属性 | 每个图一个标签 | 分子性质预测 | | 社区级 | 发现节点群组 | 节点分组 | 社区发现 |
二、谱域方法:图卷积的理论基础
2.1 谱图卷积网络(GCN)
GCN基于谱图理论,通过拉普拉斯矩阵的特征分解定义卷积:
class GCN(nn.Module): """图卷积网络(Kipf & Welling, 2017)""" def __init__(self, in_channels, hidden_channels, out_channels): super().__init__() self.conv1 = GCNConv(in_channels, hidden_channels) self.conv2 = GCNConv(hidden_channels, out_channels) self.dropout = nn.Dropout(0.5) def forward(self, x, edge_index): """ x: [num_nodes, in_channels] 节点特征 edge_index: [2, num_edges] 边索引 """ # 第一层GCN x = self.conv1(x, edge_index) x = torch.relu(x) x = self.dropout(x) # 第二层GCN x = self.conv2(x, edge_index) return x class GCNConv(nn.Module): """GCN卷积层实现""" def __init__(self, in_channels, out_channels): super().__init__() self.linear = nn.Linear(in_channels, out_channels) def forward(self, x, edge_index): """ 核心操作: H' = D^(-1/2) A D^(-1/2) X W """
编程学习
技术分享
实战经验