图注意力网络(GAT) ICLR2018, Graph Attention Network论文详解

背景:ICLR2018 论文,Graph Attention Network在GNN中非常重要,再之前图卷积网络GCN的基础之上引入了注意力机制,非常实用。

论文地址:https://arxiv.org/abs/1710.10903

代码地址: https://github.com/Diego999/pyGAT

相关论文详解:

GCN (Graph Convolutional Network) 图卷积网络概览

图注意力网络(GAT) ICLR2018, Graph Attention Network论文详解

旷视CVPR2019图卷积多标签图像识别Multi-Label Image Recognition with Graph Convolutional Networks论文详解

无监督图嵌入Unsupervised graph embedding|基于对抗的图对齐adversarial graph alignment详解

Graph特征提取方法:谱聚类(Spectral Clustering)详解

目录

一、背景与概览

1.1 相关研究

1.2 贡献点

1.3 相关工作

谱方法 spectral approaches

非谱方法 non-spectral approaches

注意力机制 self-attention

二、方法(重点)

2.1 方法特性

2.2 图注意力层Graph Attention layer

输入与输出

特征提取与注意力机制

Output features

multi-head attention

2.3 与同类工作的对比

算法复杂度低

更好鲁棒性

不需要整张Graph

比LSTM更强

与MoNet的对比

三、实验与评估

3.1 数据集

3.2 半监督学习transductive learning

3.3 归纳学习inductive learning

四、结论与个人总结


一、背景与概览

图注意力网络(GAT)Graph attention network缩写为GAT,若按照首字母大写,会与对抗生成网络GAN混淆。所以后面GAT即本文的图注意力网络。

1.1 相关研究

下面三篇论文递进关系:

  • Semi-Supervised Classification with Graph Convolutional Networks,ICLR 2017,图卷积网络 https://arxiv.org/abs/1609.02907
  • Graph Attention Networks,ICLR 2018.  图注意力网络,就是此篇文章所解析的论文 https://arxiv.org/abs/1710.10903
  • Relational Graph Attention Networks ,ICLR2019  关联性图注意力网络,整合了GCN+Attention+Relational

1.2 贡献点

  • 引入masked self-attentional layers 来改进前面图卷积graph convolution的缺点
  • 对不同的相邻节点分配相应的权重,既不需要矩阵运算,也不需要事先知道图结构
  • 四个数据集上达到state of the art的准确率Cora、Citeseer、Pubmed、protein interaction

1.3 相关工作

对待图结构的数据有两种方法,谱方法和非谱方法

谱方法 spectral approaches

即Semi-Supervised Classification with Graph Convolutional Networks,ICLR 2017这篇文章中的方法

解析 :GCN (Graph Convolutional Network) 图卷积网络概览

Finally, Kipf & Welling (2017) simplified the previous method by restricting the filters to operate in a 1-step neighborhood around each node.在每个节点周围对卷积核做一阶邻接近似。但是此方法也有一些缺点:

  • 必须基于相应的图结构才能学到拉普拉斯矩阵L
  • 对于一个图结构训练好的模型,不能运用于另一个图结构(所以此文称自己为半监督的方法)

非谱方法 non-spectral approaches

One of the challenges of these approaches is to define an operator which works with different sized neighborhoods and maintains the weight sharing property of CNNs. 即每个节点的相邻链接数量是不一样的,如何能设置相应的卷积尺寸来保证CNN能够对不同的相邻节点进行操作。下文这种方法运用GraphSAGE, 取得了不错的结果

William L Hamilton, Rex Ying, and Jure Leskovec. Inductive representation learning on largegraphs. Neural Information Processing Systems (NIPS), 2017.

这种方法是将相邻节点设置为固定的长度,然后进行specific aggregator,例如mean over all the sampled neighbors’ feature vectors, or the result of feeding them through a recurrent neural network。这种方法在几个大的benchmarks上取得了非常好的效果。

注意力机制 self-attention

优点:可以处理任意大小输入的问题,并且关注最具有影响能力的输入。

注意力机制再RNN与CNN之中,都取得了不错的效果,并且可以达到state of the art的性能。

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. Attention is all you need. arXiv preprint arXiv:1706.03762, 2017

二、方法(重点)

一整套推导下来,并不算难,没有过于复杂的推导。下面每个公式以及每个变量我都明确的给出了其中的意义。

2.1 方法特性

The idea is to compute the hidden representations of each node in the graph, by attending over its neighbors, following a self-attention strategy. 针对每一个节点运算相应的隐藏信息,在运算其相邻节点的时候引入注意力机制:

  • 高效:针对相邻的节点对,并且可以并行运算
  • 灵活:针对有不同度(degree)的节点,可以运用任意大小的weight与之对应。(这里我们解释一个概念,节点的度degree:表示的是与这个节点相连接的节点的个数)
  • 可移植:可以将模型应用于从未见过的图结构数据,不需要与训练集相同。

2.2 图注意力层Graph Attention layer

输入与输出

输入

N为节点的个数,F为feature的个数,这表示输入为N个节点的每个节点的F个feature

输出

表示对这N个节点的 F' 个输出,输出位N个节点的每个节点的F'个feature

这里我们明确一下,针对的是N个节点,按照其输入的feature预测输出的feature。

特征提取与注意力机制

为了得到相应的输入与输出的转换,我们需要根据输入的feature至少一次线性变换得到输出的feature,所以我们需要对所有节点训练一个权值矩阵:,这个权值矩阵就是输入与输出的F个feature与输出的F'个feature之间的关系。

We then perform self-attention on the nodes—a shared attentional mechanism,针对每个节点实行self-attention的注意力机制,机制为  

注意力互相关系数为attention coefficients:

  • 这个公式表示的节点 j 对于节点 i 的重要性,而不去考虑图结构性的信息
  • 如前面所言,向量h就是 feature向量
  • 下标i,j表示第i个节点和第j个节点

作者通过masked attention将这个注意力机制引入图结构之中,masked attention的含义 :只计算节点 i 的相邻的节点 j

 节点 j 为,其中Ni为 节点i的所有相邻节点。为了使得互相关系数更容易计算和便于比较,我们引入了softmax对所有的i的相邻节点j进行正则化:

实验之中,注意力机制a是一个单层的前馈神经网络,通过权值向量来确定,并且加入了 LeakyRelu的非线性激活,这里小于零斜率为0.2。(这里我们回顾下几种Relu函数,relu:小于0就是0,大于零斜率为1;LRelu:小于零斜率固定一个值,大于零斜率为1;PRelu:小于零斜率可变,大于零斜率为1;还有CRelu,Elu,SELU)。注意力机制如下:

,也是我们前面需要得到的注意力互相关系数

在模型中应用相互注意机制a(Whi,Whj),通过权重向量 a 参数化,应用 LeakyReLU 激活

  • 模型权重为
  • 转置表示为T
  • concatenation 用 || 表示
  • 公式含义就是权值矩阵与F'个特征相乘,然后节点相乘后并列在一起,与权重相乘,LRelu激活后指数操作得到softmax的分子

相应代码:

    def forward(self, x):
        # [B_batch,N_nodes,C_channels]
        B, N, C = x.size()
        # h = torch.bmm(x, self.W.expand(B, self.in_features, self.out_features))  # [B,N,C]
        h = torch.matmul(x, self.W)  # [B,N,C]
        a_input = torch.cat([h.repeat(1, 1, N).view(B, N * N, C), h.repeat(1, N, 1)], dim=2).view(B, N, N,
                                                                                                  2 * self.out_features)  # [B,N,N,2C]
        # temp = self.a.expand(B, self.out_features * 2, 1)
        # temp2 = torch.matmul(a_input, self.a)
        attention = self.leakyrelu(torch.matmul(a_input, self.a).squeeze(3))  # [B,N,N]

        attention = F.softmax(attention, dim=2)  # [B,N,N]
        attention = F.dropout(attention, self.dropout, training=self.training)
        h_prime = torch.bmm(attention, h)  # [B,N,N]*[B,N,C]-> [B,N,C]
        out = F.elu(h_prime + self.beta * h)
        return out

Output features

通过上面,运算得到了正则化后的不同节点之间的注意力互相关系数normalized attention coefficients,可以用来预测每个节点的output feature:

  • 我们再回顾一下含义,W为与feature相乘的权值矩阵
  • a为前面算得的注意力互相关系数
  • sigema为非线性激活
  • 遍历的j 表示所有与i 相邻的节点
  • 这个公式表示就是,该节点的输出feature与与之相邻的所有节点有关,是他们的线性和的非线性激活
  • 这个线性和的线性系数是前面求得的注意力互相关系数

multi-head attention

multi-head attention与下面这个工作类似:

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. Attention is all you need. arXiv preprint arXiv:1706.03762, 2017.

在上面的output feature加入计算multi-head的运算公式:

  • concate操作为||
  • 第k个注意力机制为
  • 共大K个注意力机制需要考虑,小k表示大K中的第k个
  • 输入特征的线性变换表示为
  • 最终的输出为h' 共由KF' 个特征影响

例如,K=3时候,结构如下

例如此图,节点1在邻域中具有多端注意机制,不同的箭头样式表示独立的注意力计算,通过连接或平均每个head获取 h1

对于最终的输出,concate操作可能不那么敏感了,所以我们直接用K平均来取代concate操作,得到最终的公式:

2.3 与同类工作的对比

因为我们比较重点关注于运用,这部分我们粗略的看一下就好,需要的话再来细看更新。

算法复杂度低

GAT运算得到F'个特征需要的算法复杂度

  • F':为输出特征的个数
  • F:为输入特征的个数
  • |V| :节点的个数
  • |E|:节点之间连接的个数

并且引入K之后,对于每个head的运算都独立并且可以并行

更好鲁棒性

与GCN的不同在于,GAT针对不同的相邻节点的重要性进行预测,模型具有更好的性能并且对于扰动更加鲁棒。

不需要整张Graph

引入注意力机制之后,只与相邻节点有关,即共享边的节点有关,无需得到整张graph的信息。

  • 即使丢失了i,j之间的链接,则不计算即可
  • 可以将模型运用于inductive learning,更好解释性,即使graph完全看不到completely unseen,也可以运行训练过程

比LSTM更强

The recently published inductive method of Hamilton et al. (2017) samples a fixed-size neighborhood of each node, in order to keep its computational footprint consistent; this does not allow it access to the entirety of the neighborhood while performing inference. Moreover, this technique achieved some of its strongest results when an LSTM (Hochreiter & Schmidhuber, 1997)-based neighborhood aggregator is used. This assumes the existence of a consistent sequential node ordering across neighborhoods, and the authors have rectified it by consistently feeding randomly-ordered sequences to the LSTM. Our technique does not suffer from either of these issues—it works with the entirety of the neighborhood (at the expense of a variable computational footprint, which is still on-par with methods like the GCN), and does not assume any ordering within it.

2017年Hamilton提出的inductive method为每一个node都抽取一个固定尺寸的neighborhood,为了计算的时候footprint是一致的(指的应该是计算的时候处理neighborhood的模式是固定的,不好改变,因此每次都抽样出固定数量的neighbor参与计算),这样,在计算的时候就不是所有的neighbor都能参与其中。此外,Hamilton的这个模型在使用一些基于LSTM的方法的时候能得到最好的结果,这样就是假设了每个node的neighborhood的node一直存在着一个顺序,使得这些node成为一个序列。但是本文提出的方法就没有这个问题,每次都可以将neighborhood所有的node都考虑进来,而且不需要事先假定一个neighborhood的顺序

与MoNet的对比

As mentioned in Section 1, GAT can be reformulated as a particular instance of MoNet (Monti et al., 2016). More specifically, setting the pseudo-coordinate function to be u(x, y) = f(x)kf(y), where f(x) represent (potentially MLP-transformed) features of node x and k is concatenation; and the weight function to be wj(u) = softmax(MLP(u)) (with the softmax performed over the entire neighborhood of a node) would make MoNet’s patch operator similar to ours. Nevertheless, one should note that, in comparison to previously considered MoNet instances, our model uses node features for similarity computations, rather than the node’s structural properties (which would assume knowing the graph structure upfront). GAT可以看做一个MoNet的特例。

三、实验与评估

实验分成两部分,transductive learning(半监督学习)和inductive learning(归纳学习)。模型用了两层的GAT

3.1 数据集

图结构的数据集,以及数据集之中的信息如下:

3.2 半监督学习transductive learning

  • 两层 GAT
  • 在Cora 数据集上优化网络结构的超参数,应用到Citeseer 数据集
  • 第一层 8 head, F`=8 , ELU 作为非线性函数
  • 第二层为分类层,一个 attention head 特征数C,后跟 softmax 函数,为了应对小训练集,正则化(L2)
  • 两层都采用 0.6 的dropout,相当于计算每个node位置的卷积时都是随机的选取了一部分近邻节点参与卷积

两个细节暂时不细看,贴出结果,均取得了state-of-the-art的结果

3.3 归纳学习inductive learning

  • 三层GAT 模型
  • 前两层 K=4, F1=256 ,ELU作为非线性函数
  • 最后一层用来分类 K=6, F`=121 , 激活函数为sigmoid
  • 该任务中,训练集足够大不需要使用 正则化 和 dropout

两个任务都是用Glorot初始化初始的,并且是用Adam SGD来最小化交叉熵进行优化

四、结论与个人总结

引用了注意力机制,并且模型性能达到state of the art.

运算相邻节点,更加具有鲁棒性,不需要整张图。

更具有可解释性,公式也更直观。

相关论文详解:

GCN (Graph Convolutional Network) 图卷积网络概览

图注意力网络(GAT) ICLR2018, Graph Attention Network论文详解

旷视CVPR2019图卷积多标签图像识别Multi-Label Image Recognition with Graph Convolutional Networks论文详解

无监督图嵌入Unsupervised graph embedding|基于对抗的图对齐adversarial graph alignment详解

Graph特征提取方法:谱聚类(Spectral Clustering)详解

  • 163
    点赞
  • 968
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 89
    评论
好的,关于注意力机制的代码,我们先来了解一下注意力机制(Graph Attention Network,简称GAT)。 GAT是一种基于结构的注意力机制,它在结构中进行节点分类、分类等任务时,能够自适应地学习每个节点在不同邻居节点上所占的重要性,从而更准确地进行分类。下面是一个简单的GAT实现: ```python import torch import torch.nn as nn import torch.nn.functional as F class GraphAttentionLayer(nn.Module): def __init__(self, in_features, out_features, dropout, alpha, concat=True): super(GraphAttentionLayer, self).__init__() self.in_features = in_features self.out_features = out_features self.dropout = dropout self.alpha = alpha self.concat = concat self.W = nn.Parameter(torch.zeros(size=(in_features, out_features))) nn.init.xavier_uniform_(self.W.data, gain=1.414) self.a = nn.Parameter(torch.zeros(size=(2*out_features, 1))) nn.init.xavier_uniform_(self.a.data, gain=1.414) self.leakyrelu = nn.LeakyReLU(self.alpha) def forward(self, h, adj): Wh = torch.mm(h, self.W) a_input = self._prepare_attentional_mechanism_input(Wh) e = self.leakyrelu(torch.matmul(a_input, self.a).squeeze(2)) zero_vec = -9e15*torch.ones_like(e) attention = torch.where(adj > 0, e, zero_vec) attention = F.softmax(attention, dim=1) attention = F.dropout(attention, self.dropout, training=self.training) h_prime = torch.matmul(attention, Wh) if self.concat: return F.elu(h_prime) else: return h_prime def _prepare_attentional_mechanism_input(self, Wh): N = Wh.size()[0] Wh_repeated_in_chunks = Wh.repeat_interleave(N, dim=0) Wh_repeated_alternating = Wh.repeat(N, 1) all_combinations_matrix = torch.cat([Wh_repeated_in_chunks, Wh_repeated_alternating], dim=1) return all_combinations_matrix.view(N, N, 2 * self.out_features) ``` 在这个代码中,我们定义了一个名为GraphAttentionLayer的类,它继承于nn.Module类。在它的__init__方法中,我们定义了一些必要的参数,包括输入特征维度、输出特征维度、dropout率、LeakyReLU函数的负斜率系数以及是否将节点特征与注意力机制的输出进行拼接。W和a是需要学习的参数,其中W是线性变换的权重矩阵,a是注意力机制的权重矩阵。我们使用xavier_uniform_方法对这两个参数进行初始化。 在forward方法中,我们首先将节点特征矩阵h与权重矩阵W相乘,得到Wh。然后,我们通过_prepare_attentional_mechanism_input方法将Wh转换为用于注意力计算的输入矩阵a_input。接着,我们将a_input与注意力权重矩阵a相乘,得到每个节点与其邻居之间的注意力系数e。我们使用LeakyReLU函数将e中的负值裁剪掉。然后,我们对每个节点的邻居节点计算softmax,得到它们之间的注意力权重。我们使用dropout对注意力权重进行随机失活,以防止过拟合。最后,我们将注意力权重与Wh相乘,得到每个节点的新特征h_prime。如果concat参数为True,我们将h_prime与原始节点特征进行拼接并使用ELU函数作为输出;否则,我们直接返回h_prime作为输出。 需要注意的是,这个代码中的实现是基于PyTorch框架的,如果你使用其他的深度学习框架,可能需要做一些调整。同时,这个代码只是GAT的一个简单实现,如果你想深入学习GAT,还需要阅读相关论文并了解更多细节。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

祥瑞Coding

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

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

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

打赏作者

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

抵扣说明:

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

余额充值