图神经网络实战(8)——图注意力网络(Graph Attention Networks, GAT)

本文深入探讨了图注意力网络(GAT),它通过自注意力机制改进了图卷积网络(GCN)。文章详细介绍了GAT的四个关键步骤:线性变换、激活函数、Softmax归一化和多头注意力。此外,通过实例展示了如何使用NumPy和PyTorch Geometric实现GAT,并在Cora和CiteSeer数据集上进行训练,对比了GAT与GCN的性能。实验结果表明,GAT在节点分类任务上表现优越。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. 前言

图注意力网络 (Graph Attention Networks, GAT) 是对图卷积网络 (Graph Convolutional Network, GCN) 的改进,提出了一种通过自注意力 (self-attention) 过程计算的加权因子,而不是使用静态的归一化系数。自注意力过程也是最成功的深度学习架构之一——Transformer 的核心,TransformerBERTGPT-3 广泛应用。GAT2017 年由 Veličković 等人提出,凭借其出色的性能,已成为最受欢迎的图神经网络 (Graph Neural Networks, GNN) 架构之一。
在本节中,我们通过四个步骤了解图注意力层的工作原理,这是理解自注意力工作原理的绝佳示例。了解了理论知

### 关于TensorFlow实现GATv2 #### 使用TensorFlow构建GATv2模型概述 为了在TensorFlow中实现GATv2,可以遵循一种模块化的方法来定义图注意力层以及整体的GATv2架构。这种设计允许灵活调整超参数并适应不同的数据集需求。 #### 定义Graph Attention Layer (GAL) 考虑到GATv2利用了动态注意力机制[^3],这意味着注意力系数不仅仅基于节点自身的特征,同时也考虑到了邻居之间的连接模式。因此,在创建`GraphAttentionLayer`类时,应当特别注意如何编码这些交互特性: ```python import tensorflow as tf from tensorflow.keras import layers class GraphAttentionLayer(layers.Layer): def __init__(self, units, activation=tf.nn.elu, **kwargs): super().__init__(**kwargs) self.units = units self.activation = activation def build(self, input_shape): # 初始化权重矩阵W和其他必要的变量 shape = [input_shape[-1], self.units] initializer = tf.initializers.GlorotUniform() self.kernel = self.add_weight(shape=shape, initializer=initializer, trainable=True) def call(self, inputs, adj_matrix): h_prime = tf.matmul(inputs, self.kernel) # 节点特征变换 e = _calculate_dynamic_attention(h_prime, adj_matrix) # 动态计算注意力分数 zero_vec = -9e15 * tf.ones_like(e) attention = tf.where(adj_matrix > 0, e, zero_vec) normalized_attentions = tf.nn.softmax(attention, axis=-1) output = tf.matmul(normalized_attentions, h_prime) return self.activation(output) def _calculate_dynamic_attention(features, adjacency): """辅助函数用于根据给定的特征和邻接矩阵计算动态注意力""" batch_size = tf.shape(features)[0] num_nodes = tf.shape(features)[1] features_i = tf.tile(tf.expand_dims(features, 2), [1, 1, num_nodes, 1]) features_j = tf.transpose(features_i, perm=[0, 2, 1, 3]) concat_features = tf.concat([features_i, features_j], axis=-1) # 假设这里有一个简单的线性映射来获取最终的注意力得分;实际应用可能更复杂 att_scores = layers.Dense(1)(concat_features) att_scores = tf.squeeze(att_scores, -1) return att_scores ``` 上述代码片段展示了如何使用TensorFlow/Keras API定义一个基本版本的`GraphAttentionLayer`,它能够处理带有自环边的标准无向图结构,并支持多头注意力机制扩展[^1]。 #### 构建完整的GATv2模型 一旦有了基础的图注意力单元之后,就可以很容易地堆叠多个这样的层形成更深的网络结构,从而构成完整的GATv2模型: ```python class GATv2Model(tf.keras.Model): def __init__(self, hidden_units, class_num, heads=8, dropout_rate=0.6, **kwargs): super().__init__(**kwargs) self.dropout_layer = layers.Dropout(dropout_rate) self.attention_layers = [ GraphAttentionLayer(hidden_units // heads, name=f'gal_{i}') for i in range(heads)] self.output_layer = layers.Dense(class_num, activation='softmax') def call(self, node_features, adjacency_matrix, training=False): x = self.dropout_layer(node_features, training=training) attentions = [] for layer in self.attention_layers: a = layer(x, adjacency_matrix) attentions.append(a) combined_representation = tf.reduce_mean(attentions, axis=0) final_output = self.output_layer(combined_representation) return final_output ``` 此部分实现了两层的GATv2模型框架,其中包含了图注意力层(GraphAttentionLayer),并且在整个流程里加入了Dropout正则化技术以防止过拟合现象的发生。
评论 69
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼小辉丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值