代码
如果想直接看代码部分,请移步https://github.com/Alienge/Graph-Network.
背景
最近各种顶会上都可以看到
G
C
N
GCN
GCN和
G
A
T
GAT
GAT,虽然这两篇论文都差不多在18年左右出来的,但是现有论文的网络结构都或多或少的有
G
C
N
GCN
GCN和
G
A
T
GAT
GAT的影子,那么为什么有这么多人去研究图网络呢?或者换句话说使用图网络能解决什么问题?要回答这个问题很容易,因为图结构更符合我们现实生活中的逻辑关系,那么自然而然的就可以去解决很多问题了。比如,个性化推荐,社交网络,特征工程等。
本文会尽量弱化数学公式的影响。
基础知识
在介绍图网络之前我们需要了解一部分图论的基础知识。图是由若干个结点(Node)及连接两个结点的边(edge)所构成的图形,用于刻画不同结点之间的关系。如图1表示了一张图。
那么可以用两个集合量化该图,这两个集合分别是顶点集合和边的集合,分别用
V
V
V和
E
E
E表示。按照图论的定义,可以用一个这个二元组来定义这个图。即
G
=
{
V
,
E
}
G=\{V,E\}
G={V,E} 其中
V
V
V是顶点的集合,
E
E
E是边的集合。
拓展一下把这个用到 G C N GCN GCN或者 G A N GAN GAN中,对于每一个顶点,都有一个特征,所有顶点的特征聚集在一起可以用一个矩阵来表示,假设顶点有 ∣ V ∣ = N |V| = N ∣V∣=N个,特征的维度为 F F_{} F 图中顶点的特征也叫图的embedding 。那么就可以用一个矩阵 h h h来表示,其维度 s i z e = [ N , F ] size=[N, F] size=[N,F]. 而边的关系用矩阵e来表示,其 e i j e_{ij} eij表示顶点 v i v_{i} vi和顶点 v i j v_{ij} vij是否有边相连。维度 s i z e = [ N , N ] size=[N, N] size=[N,N].
e
i
j
=
{
0
如果
v
i
与
v
j
有边相连
1
o
t
h
e
r
w
i
s
e
e_{ij}= \begin{cases} 0& \text{如果$v_{i}$与$v_{j}$有边相连}\\ 1& \text{$otherwise$} \end{cases}
eij={01如果vi与vj有边相连otherwise
另外图还有一个度矩阵
D
D
D,其维度
s
i
z
e
=
[
N
,
N
]
size=[N, N]
size=[N,N],
D
i
i
D_{ii}
Dii表示顶点
v
i
v_{i}
vi的度。
例子 :
下面就以 图 1 图1 图1作为例来表示特征矩阵 f = h f = h f=h, 边矩阵 A = e A = e A=e和度矩阵 D D D。由于只是演示作用,不妨设置特征矩阵的维度为 1 1 1维。即 f = h = [ 4 , 2 , 4 , − 3 ] T f = h = [4, 2, 4, -3]^{T} f=h=[4,2,4,−3]T
图的拓扑结构 | 度矩阵(D) | 边矩阵(A=e) | 特征矩阵(f=h) |
---|---|---|---|
[ 2 0 0 0 0 3 0 0 0 0 2 0 0 0 0 1 ] \left[ \begin{matrix} 2& 0 & 0 & 0 \\ 0 & 3 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 0 & 0 & 0 &1 \end{matrix} \right] ⎣⎢⎢⎡2000030000200001⎦⎥⎥⎤ | [ 0 1 1 0 1 0 1 1 1 0 1 0 0 1 0 0 ] \left[ \begin{matrix} 0& 1 & 1 & 0 \\ 1 & 0 & 1 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 &0 \end{matrix} \right] ⎣⎢⎢⎡0110100111100100⎦⎥⎥⎤ | [ 4 2 4 − 3 ] \left[ \begin{matrix} 4 \\ 2 \\ 4 \\-3 \end{matrix} \right] ⎣⎢⎢⎡424−3⎦⎥⎥⎤ |
空域卷积和频域卷积
空域卷积(Spatial Convolution)。从设计理念上看,空域卷积与深度学习中的卷积的应用方式类似,其核心在于聚合邻居结点的信息。比如说,一种最简单的无参卷积方式可以是:将所有直连邻居结点的隐藏状态加和,来更新当前结点的隐藏状态。如图1中顶点 v 1 v_{1} v1的空域卷积结果为 f ( v 1 ) l + 1 = f ( v 0 ) l + f ( v 1 ) l + f ( v 2 ) l + f ( v 3 ) l 4 (1) f(v_{1})^{l+1} = \frac{f(v_{0}) ^{l}+ f(v_{1}) ^{l}+f(v_{2})^{l}+f(v_{3})^{l}}{4} \tag{1} f(v1)l+1=4f(v0)l+f(v1)l+f(v2)l+f(v3)l(1)其中 f f f表示是顶点到特征的映射。其中以 G A T GAT GAT为代表。在下面的说明中, 这种卷积时发生在 v e r t e x d o m a i n vertex\space domain vertex domain也称 s p a t i a l d o m a i n spatial \space domain spatial domain。
频域卷积(Spectral Convolution)。相比于空域卷积而言,它主要利用的是图傅里叶变换(Graph Fourier Transform)实现卷积。简单来讲,它利用图的拉普拉斯矩阵(Laplacian matrix)导出其频域上的的拉普拉斯算子,再类比频域上的欧式空间中的卷积,导出图卷积的公式。虽然公式的形式与空域卷积非常相似,但频域卷积的推导过程却有些艰深晦涩。其中以 G C N GCN GCN为代表。在下面的说明中,此类卷积是发生在 s p e c t r a l d o m a i n spectral \space domain spectral domain。
空域卷积相比频域卷积非常直观地借鉴了图像里的卷积操作,频域卷积相比空域卷积更加有理论依据。
GAT
在上一节提到的空域卷积的公式
(
1
)
(1)
(1)中,计算
l
+
1
l+1
l+1层的顶点
v
1
v_{1}
v1的特征时,
l
l
l层的顶点
v
0
,
v
1
,
v
2
,
v
3
v_{0},v_{1},v_{2},v_{3}
v0,v1,v2,v3对
l
+
1
l+1
l+1层的
v
1
v_{1}
v1的权重是一样的,显然这是不合理的。因此
G
A
T
GAT
GAT提出就是解决此问题的一种解决方案。
G
r
a
p
h
A
t
t
e
n
t
i
o
n
N
e
t
w
o
r
k
(
G
A
T
)
GraphAttentionNetwork(GAT)
GraphAttentionNetwork(GAT)提出了用注意力机制对邻近节点特征加权求和。 邻近节点特征的权重完全取决于节点特征,独立于图结构。那么
G
A
T
GAT
GAT解决了那些问题呢。
- G A T GAT GAT中,图中每个顶点可以根据邻接的顶点确定特征,不由我们指定,权重设置成参数,最终结果由梯度下降得到,免去了人工指定的麻烦。
- G A T GAT GAT的设置,只与相邻节点有关,无需得到整张图的信息。
GAT的理论依据
现有给定如下已知条件:顶点之间的边矩阵为
A
∈
R
N
×
N
A \in R^{N \times N}
A∈RN×N, 第
l
l
l层的顶点的特征向量集:
h
l
=
[
h
1
,
h
2
,
⋅
⋅
⋅
,
h
N
]
T
h^{l} = [h_{1}, h_{2},\cdot\cdot\cdot,h_{N} ]^{T}
hl=[h1,h2,⋅⋅⋅,hN]T, 其中
h
i
∈
R
F
h_{i} \in R^{F}
hi∈RF,
N
N
N为图中顶点的个数, 显然可以知道
h
∈
R
N
×
F
h \in R^{N \times F}
h∈RN×F。得到下一层
l
+
1
l+1
l+1的特征向量势必需要一个权重矩阵,假设下一层的特征向量的维度为
F
′
F'
F′, 即
h
i
l
+
1
∈
R
F
′
h_{i}^{l+1} \in R^{F'}
hil+1∈RF′, 那么需要的权重矩阵
W
∈
R
F
×
F
′
W \in R^{F \times F'}
W∈RF×F′。可以得到的
l
+
1
l+1
l+1层的特征向量集:
h
l
+
1
=
[
h
1
′
,
h
2
′
,
⋅
⋅
⋅
,
h
N
′
]
T
h^{l+1} = [h_{1}^{'}, h_{2}^{'},\cdot\cdot\cdot,h_{N}^{'} ]^{T}
hl+1=[h1′,h2′,⋅⋅⋅,hN′]T
显然可以知道
h
l
+
1
∈
R
N
×
F
′
h^{l+1} \in R^{N \times F'}
hl+1∈RN×F′。
有了这些就可以计算注意力了,针对每个节点可以得到对应的注意力系数, 注意力系数为:
e
i
j
=
a
(
W
T
h
i
,
W
T
h
j
)
(2)
e_{ij} = a(W^{T}h_{i},W^{T}h_{j}) \tag{2}
eij=a(WThi,WThj)(2)
注意一下
(
W
T
h
i
,
W
T
h
j
)
(W^{T}h_{i},W^{T}h_{j})
(WThi,WThj)是一个
c
o
n
c
a
t
concat
concat操作, 可以知道
(
W
T
h
i
,
W
T
h
j
)
∈
R
2
F
′
×
1
(W^{T}h_{i},W^{T}h_{j}) \in R^{2F' \times 1}
(WThi,WThj)∈R2F′×1, 而
a
∈
R
1
×
2
F
′
a \in R^{1 \times 2F'}
a∈R1×2F′
那么以这种方式就可以计算出所有的注意力系数,作者通过
M
a
s
k
A
t
t
e
n
t
i
o
n
Mask Attention
MaskAttention将这个注意力机制引入图结构中,
M
a
s
k
A
t
t
e
n
t
i
o
n
Mask Attention
MaskAttention的含义是:仅将注意力分配到顶点
i
i
i的邻居节点集
j
∈
N
i
j \in N_{i}
j∈Ni 为了使得注意力系数更容易计算和便于比较,我们引入了
s
o
f
t
m
a
x
softmax
softmax对所有的
i
i
i的相邻节点
j
∈
N
i
j \in N_{i}
j∈Ni进行正则化:
a
i
j
=
s
o
f
t
m
a
x
(
e
i
j
)
=
e
x
p
(
e
i
j
)
∑
k
∈
N
i
e
x
p
(
e
i
k
)
(3)
a_{ij} = softmax(e_{ij})=\frac{exp(e_{ij})}{\sum_{k\in N_{i}}exp(e_{ik})} \tag{3}
aij=softmax(eij)=∑k∈Niexp(eik)exp(eij)(3)
更直观一点解释就是行归一化,只不过是特殊的行归一化而已。
作者在这里在进行行归一化之前加了一个非线性函数
L
e
a
k
y
R
e
l
u
LeakyRelu
LeakyRelu。有了
(
2
)
(2)
(2),
(
3
)
(3)
(3)和非线性关系
L
e
a
k
R
e
l
u
LeakRelu
LeakRelu,那么我们就可以轻松得到最后的注意力系数:
a
i
j
=
s
o
f
t
m
a
x
(
L
e
a
k
y
R
e
l
u
(
e
i
j
)
)
=
e
x
p
(
L
e
a
k
y
R
e
l
u
(
a
[
W
h
i
,
W
h
j
]
)
)
∑
k
∈
N
i
e
x
p
(
L
e
a
k
y
R
e
l
u
(
a
[
W
h
i
,
W
h
k
]
)
)
(4)
a_{ij} = softmax(LeakyRelu(e_{ij}))=\frac{exp(LeakyRelu(a[Wh_{i},Wh_{j}]))}{\sum_{k\in N_{i}}exp(LeakyRelu(a[Wh_{i},Wh_{k}]))} \tag{4}
aij=softmax(LeakyRelu(eij))=∑k∈Niexp(LeakyRelu(a[Whi,Whk]))exp(LeakyRelu(a[Whi,Whj]))(4)
注意一下哈,我在参考其他代码的时候发现系数
a
a
a在每一次计算
e
i
j
e{ij}
eij的过程中是不变的。
那么上面这公式(2), (3)和(4) 时组成GAT的核心部分。最后一步得到最终的
a
t
t
e
n
t
i
o
n
attention
attention就需要把边的矩阵
A
A
A结合起来, 即最后的
m
a
s
k
mask
mask部分。
即
a
t
t
e
n
t
i
o
n
i
j
=
{
a
i
j
如果
v
i
与
v
j
有直接边相连
i
n
f
i
n
i
t
e
o
t
h
e
r
w
i
s
e
(5)
attention_{ij}= \begin{cases} a_{ij}& \text{如果$v_{i}$与$v_{j}$有直接边相连}\\ infinite& \text{$otherwise$} \end{cases} \tag{5}
attentionij={aijinfinite如果vi与vj有直接边相连otherwise(5)
其中
a
t
t
e
n
t
i
o
n
∈
R
N
×
N
attention \in R^{N\times N}
attention∈RN×N。
有了这个最终
l
+
1
l+1
l+1层的输出为:
h
l
+
1
=
σ
(
a
t
t
e
n
t
i
o
n
×
h
l
W
)
h^{l+1} = \sigma( attention \times h_{l}W)
hl+1=σ(attention×hlW)
最终得到
h
l
+
1
∈
R
N
×
F
′
h^{l+1} \in R^{N \times F'}
hl+1∈RN×F′
自此,
G
A
T
GAT
GAT的理论部分基本全部完成,其最终的目的时训练出注意力系数。以这个目的进行了一系列操作,本质上就是这个。
下面是重新整理公式与代码部分的相关的公式, 如果你不想看,可直接跳过.
为了写代码的方便,这里我们把计算
a
t
t
e
n
t
i
o
n
attention
attention的部分用矩阵写出来
已知
h
l
∈
R
N
×
F
h^{l} \in R^{N\times F}
hl∈RN×F ,
W
∈
R
F
×
F
′
W\in R^{F\times F'}
W∈RF×F′, 参数
a
∈
R
1
×
2
F
′
a \in R^{1\times 2F'}
a∈R1×2F′, 这里可以解释一下
a
a
a这里的作用,我们可以把
a
a
a分成两部分,一个是自注意力系数,一个是邻居节点的注意力系数,即
a
=
[
a
s
e
l
f
,
a
n
e
i
b
o
r
]
a = [a_{self}, a_{neibor}]
a=[aself,aneibor]
h
l
+
1
=
h
l
W
(6)
h^{l+1} = h^{l} W \tag{6}
hl+1=hlW(6)
a
t
t
e
n
t
i
o
n
s
e
l
f
=
h
l
+
1
a
s
e
l
f
T
(7)
attention_{self} = h^{l+1} a_{self}^{T}\tag{7}
attentionself=hl+1aselfT(7)
a
t
t
e
n
t
i
o
n
n
e
i
b
o
r
=
h
l
+
1
a
n
e
i
b
o
r
T
(8)
attention_{neibor} = h^{l+1} a_{neibor}^{T}\tag{8}
attentionneibor=hl+1aneiborT(8)
a
t
t
e
n
t
i
o
n
=
a
t
t
e
n
t
i
o
n
s
e
l
f
+
a
t
t
e
n
t
i
o
n
n
e
i
b
o
r
T
(9)
attention =attention_{self} + attention_{neibor}^{T}\tag{9}
attention=attentionself+attentionneiborT(9)
a
t
t
e
n
t
i
o
n
=
L
e
a
k
R
e
l
u
(
a
t
t
e
n
t
i
o
n
)
(10)
attention = LeakRelu(attention)\tag{10}
attention=LeakRelu(attention)(10)
a
t
t
e
n
t
i
o
n
i
j
=
{
a
t
t
e
n
t
i
o
n
i
j
如果
v
i
与
v
j
有直接边相连
i
n
f
i
n
i
t
e
o
t
h
e
r
w
i
s
e
(11)
attention_{ij}= \begin{cases} attention_{ij}& \text{如果$v_{i}$与$v_{j}$有直接边相连}\\ infinite& \text{$otherwise$} \end{cases} \tag{11}
attentionij={attentionijinfinite如果vi与vj有直接边相连otherwise(11)
a
t
t
e
n
t
i
o
n
=
s
o
f
t
m
a
x
(
a
t
t
e
t
i
o
n
)
(12)
attention = softmax(attetion)\tag{12}
attention=softmax(attetion)(12)
h
l
+
1
=
σ
(
a
t
t
e
n
t
i
o
n
×
h
l
+
1
)
(13)
h^{l+1} =\sigma(attention \times h^{l+1} ) \tag{13}
hl+1=σ(attention×hl+1)(13)
有了公式 ( 6 ) − ( 13 ) (6)-(13) (6)−(13), 我们可以很容易的实现 G A T GAT GAT的代码, 具体代码参考GAT的pytorch代码. 这个代码的实现只是我自己参考别人的代码实现,有些地方作了改动。当然也有star很多的代码pytorch星比较多的代码
GCN
深度学习中, C N N CNN CNN中的卷积本质上是一个共享参数的特征提取, 通过计算中心像素点以及相邻像素点的加权和来构成 f e a t u r e m a p feature map featuremap, 实现空间特征的提取。但是这种理论只适用于 E u c l i d e a n S t r u c t u r e Euclidean Structure EuclideanStructure, 对于 G r a p h Graph Graph这种 N o n E u c l i d e a n S t r u c t u r e Non Euclidean Structure NonEuclideanStructure并不是很适用。那么如何将 C N N CNN CNN中的思想引入到 G r a p h Graph Graph中,成为一个很大的问题。这里为什么要研究GCN的原因,参考的知乎请移步这里。
s p e c t r a l d o m a i n spectral \space domain spectral domain是GCN的理论基础, 主要借助图的 L a p l a c i a n m a t r i x Laplacian \space matrix Laplacian matrix的特征值和特征向量来研究图的性质。
GCN的理论依据
现有给定如下已知条件:顶点之间的边矩阵为
A
∈
R
N
×
N
A \in R^{N \times N}
A∈RN×N,
N
N
N为图中顶点的个数, 显然可以知道
h
∈
R
N
×
F
h \in R^{N \times F}
h∈RN×F 和和度矩阵
D
∈
R
N
×
N
D \in R^{N\times N}
D∈RN×N。
L
a
p
l
a
c
i
a
n
m
a
t
r
i
x
Laplacian \space matrix
Laplacian matrix定义为
L
=
D
−
A
(14)
L=D-A \tag{14}
L=D−A(14)
图的拓扑结构 | 度矩阵(D) | 边矩阵(A=e) | 特征矩阵(f=h) | Laplacian matrix |
---|---|---|---|---|
[ 2 0 0 0 0 3 0 0 0 0 2 0 0 0 0 1 ] \left[ \begin{matrix} 2& 0 & 0 & 0 \\ 0 & 3 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 0 & 0 & 0 &1 \end{matrix} \right] ⎣⎢⎢⎡2000030000200001⎦⎥⎥⎤ | [ 0 1 1 0 1 0 1 1 1 0 1 0 0 1 0 0 ] \left[ \begin{matrix} 0& 1 & 1 & 0 \\ 1 & 0 & 1 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 &0 \end{matrix} \right] ⎣⎢⎢⎡0110100111100100⎦⎥⎥⎤ | [ 4 2 4 − 3 ] \left[ \begin{matrix} 4 \\ 2 \\ 4 \\-3 \end{matrix} \right] ⎣⎢⎢⎡424−3⎦⎥⎥⎤ | [ 2 − 1 − 1 0 − 1 3 − 1 − 1 − 1 0 1 0 0 − 1 0 1 ] \left[ \begin{matrix} 2& -1 & -1 & 0 \\ -1 & 3 & -1 & -1 \\ -1 & 0 & 1 & 0 \\ 0 & -1 & 0 &1 \end{matrix} \right] ⎣⎢⎢⎡2−1−10−130−1−1−1100−101⎦⎥⎥⎤ |
这里不过多的介绍傅里叶分析里面的东西, 这里只需要记得 L a p l a c i a n Laplacian Laplacian变换可以将上面的 v e r t e x d o m a i n vertex domain vertexdomain变换到 s p e c t r a l d o m a i n spectral domain spectraldomain, 或者简单的把 L a p l a c i a n Laplacian Laplacian变换是操作在 G r a p h Graph Graph中的一个算子即可。这里并不影响你理解 G C N GCN GCN。这里简单说一下 L a p l a c i a n m a t r i x Laplacian \space matrix Laplacian matrix的几个好的性质:
- L a p l a c i a n m a t r i x Laplacian \space matrix Laplacian matrix是一个对称矩阵, 那么就可以进行特征分解, 也就是谱分解
- L a p l a c i a n m a t r i x Laplacian \space matrix Laplacian matrix是一个半正定的矩阵,也就是其特征值 λ i ≥ 0 \lambda_{i} \geq0 λi≥0
知道了这些,就可以对这些就可以对
L
L
L进行特征分解,在线性代数中,有
L
=
U
Λ
U
T
(15)
L = U\Lambda U^{T} \tag{15}
L=UΛUT(15) 其中
Λ
=
[
λ
1
0
⋯
0
0
λ
2
⋯
0
⋮
⋮
⋱
⋮
0
0
⋯
λ
N
]
\Lambda = \left[ \begin{matrix} \lambda_{1} & 0 & \cdots & 0 \\ 0 & \lambda_{2} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & \lambda_{N} \\ \end{matrix} \right]
Λ=⎣⎢⎢⎢⎡λ10⋮00λ2⋮0⋯⋯⋱⋯00⋮λN⎦⎥⎥⎥⎤
U
=
[
u
1
,
u
2
,
…
,
u
N
]
∈
R
N
×
N
U = [u_{1}, u_{2},\dots,u_{N}] \in R^{N\times N}
U=[u1,u2,…,uN]∈RN×N 。
那么这个
U
U
U就是一组标准的正交基向量, 显然有
U
U
T
=
I
(16)
UU^{T}=I \tag{16}
UUT=I(16)
这组标准正交基可以将
V
e
r
t
e
x
d
o
m
a
i
n
Vertex \space domain
Vertex domain 转化到
S
p
e
c
t
r
a
l
d
o
a
m
i
n
Spectral \space doamin
Spectral doamin中。现假设输入的矩阵为
x
∈
R
N
×
F
x\in R^{N\times F}
x∈RN×F, 那么转化到
S
p
e
c
t
r
a
l
d
o
a
m
i
n
Spectral \space doamin
Spectral doamin中变成:
x
^
=
U
T
x
(17)
\hat{x} = U^{T}x \tag{17}
x^=UTx(17)
所有的特征都转化到
S
p
e
c
t
r
a
l
d
o
a
m
i
n
Spectral \space doamin
Spectral doamin中了,那么自然而然的联想到是否有有在
V
e
r
t
e
x
d
o
m
a
i
n
Vertex\space domain
Vertex domain中的普通神经网络中的
y
=
θ
x
{y} = \theta{x}
y=θx
答案是肯定的,但是这个和在
S
p
e
c
t
r
a
l
d
o
a
m
i
n
Spectral \space doamin
Spectral doamin中有稍稍的不同, 我们以图1中的
G
r
a
p
h
Graph
Graph为例进行简单的解释, 应该是怎么样的一种
θ
\theta
θ的形式。
在图2中,输入特征矩阵 x x x (和GAT中的 h l h^{l} hl 一致), 经过空间变换(傅里叶变换)成频率空间, x ^ \hat{x} x^ 表示 x x x 经过变换后的结果。那么你看到的频率空间, x ^ \hat{x} x^ 就是图2中的 s ( w ) s(w) s(w), 而图2中 s ( t ) s(t) s(t) 是时间域的二维图。 本质上就是你以什么样的视角去看同一个东西。然后出现的不同结果。那么图3就是 x x x 在频率上显示的图形。
解释一下这个是什么意思, [ λ 1 , λ 2 , λ 3 , λ 4 ] [\lambda_{1},\lambda_{2},\lambda_{3},\lambda_{4}] [λ1,λ2,λ3,λ4]是在 s p e c t r a l d o m a i n spectral \space domain spectral domain的频率大小。而 [ u 1 x , u 2 x , u 3 x , u 4 x ] [u_{1}x, u_{2}x, u_{3}x,u_{4}x] [u1x,u2x,u3x,u4x]是每个频率上的数值大小。说白了就是 x x x在基向量 u 1 u1 u1上的投影大小。现在只需要在每个频率上加一个参数就可以实现 C N N CNN CNN上的 y = θ x y=\theta x y=θx了。即
那么就有
[
y
^
1
y
^
2
y
^
3
y
^
4
]
=
[
θ
1
0
0
0
0
θ
2
0
0
0
0
θ
3
0
0
0
0
θ
4
]
[
x
^
1
x
^
2
x
^
3
x
^
4
]
\left[ \begin{matrix} \hat{y}_{1} \\ \hat{y}_{2} \\ \hat{y}_{3} \\ \hat{y}_{4} \end{matrix} \right] = \left[ \begin{matrix} \theta_{1}& 0 & 0 & 0 \\ 0 & \theta_{2} & 0 & 0 \\ 0 & 0 & \theta_{3} & 0 \\ 0 & 0 & 0 &\theta_{4} \end{matrix} \right] \left[ \begin{matrix} \hat{x}_{1} \\ \hat{x}_{2} \\ \hat{x}_{3} \\ \hat{x}_{4} \end{matrix} \right]
⎣⎢⎢⎡y^1y^2y^3y^4⎦⎥⎥⎤=⎣⎢⎢⎡θ10000θ20000θ30000θ4⎦⎥⎥⎤⎣⎢⎢⎡x^1x^2x^3x^4⎦⎥⎥⎤
显然这个参数与
Λ
\Lambda
Λ和
θ
\theta
θ有关,不妨将其记录为
g
θ
(
Λ
)
g_{\theta}(\Lambda)
gθ(Λ)。那么就有关系:
y
^
=
g
θ
(
Λ
)
x
^
(18)
\hat{y} = g_{\theta}(\Lambda)\hat{x} \tag{18}
y^=gθ(Λ)x^(18)
由公式(17)可知,(18)式可以变成
y
^
=
g
θ
(
Λ
)
U
T
x
(19)
\hat{y} = g_{\theta}(\Lambda)U^{T}{x} \tag{19}
y^=gθ(Λ)UTx(19)
现在只需要把
y
^
\hat{y}
y^反傅里叶变化成
V
e
r
t
e
x
d
o
m
a
i
n
Vertex \space domain
Vertex domain中就可以。即
y
^
=
U
T
y
=
g
θ
(
Λ
)
U
T
x
(20)
\hat{y} =U^{T}y= g_{\theta}(\Lambda)U^{T}{x} \tag{20}
y^=UTy=gθ(Λ)UTx(20)
根据正交矩阵的性质, 也即公式(16), 就可以得到
y
=
U
g
θ
(
Λ
)
U
T
x
(21)
y =U g_{\theta}(\Lambda)U^{T}{x} \tag{21}
y=Ugθ(Λ)UTx(21)
又因为
g
θ
(
Λ
)
g_{\theta}(\Lambda)
gθ(Λ)是关于
Λ
\Lambda
Λ的函数, 就可以变成
y
=
g
θ
(
L
)
x
(22)
y =g_{\theta}(L){x} \tag{22}
y=gθ(L)x(22)
然而公式(22)是所有一切以
G
C
N
GCN
GCN罪恶的开始, 其他的所有都只是在做怎么估计这个
g
θ
(
L
)
g_{\theta}(L)
gθ(L)。
主流的
G
C
N
GCN
GCN是以
c
h
e
b
y
s
h
e
v
p
o
l
y
n
o
m
i
a
l
chebyshev \space polynomial
chebyshev polynomial为基础进行估计
g
θ
(
L
)
g_{\theta}(L)
gθ(L),
c
h
e
b
y
s
h
e
v
p
o
l
y
n
o
m
i
a
l
chebyshev \space polynomial
chebyshev polynomial为:
T
0
(
Λ
~
)
=
I
T
1
(
Λ
~
)
=
Λ
~
T
i
(
Λ
~
)
=
2
Λ
~
T
i
−
1
(
Λ
~
)
−
T
i
−
2
(
Λ
~
)
(23)
T_{0}(\widetilde{\Lambda}) = I \space \space T_{1}(\widetilde{\Lambda})=\widetilde{\Lambda}\space \space T_{i}(\widetilde{\Lambda}) = 2\widetilde{\Lambda}T_{i-1}(\widetilde{\Lambda})-T_{i-2}(\widetilde{\Lambda}) \tag{23}
T0(Λ
)=I T1(Λ
)=Λ
Ti(Λ
)=2Λ
Ti−1(Λ
)−Ti−2(Λ
)(23)
其中要满足两个关系式
Λ
~
=
2
Λ
λ
m
a
x
−
I
\widetilde{\Lambda} = \frac{2\Lambda}{\lambda_{max}}-I
Λ
=λmax2Λ−I,
Λ
~
∈
[
−
1
,
1
]
\widetilde{\Lambda}\in[-1,1]
Λ
∈[−1,1]。
有了上面的
c
h
e
b
y
s
h
e
v
p
o
l
y
n
o
m
i
a
l
chebyshev \space polynomial
chebyshev polynomial递推关系式(23),不妨设:
g
θ
(
Λ
)
=
∑
k
=
0
n
θ
k
Λ
k
g_{\theta}(\Lambda) = \sum_{k=0}^{n}\theta_{k}\Lambda^{k}
gθ(Λ)=k=0∑nθkΛk
在
c
h
e
b
y
s
h
e
v
p
o
l
y
n
o
m
i
a
l
chebyshev \space polynomial
chebyshev polynomial中,取值
n
=
1
,
λ
m
a
x
=
2
n=1,\lambda_{max}=2
n=1,λmax=2,公式(22)就可以变成:
y
=
θ
0
x
+
θ
1
(
L
−
I
)
x
(24)
y =\theta_{0}{x} +\theta_{1}(L-I)x \tag{24}
y=θ0x+θ1(L−I)x(24)
取
L
=
I
−
D
−
1
2
A
D
1
2
L=I-D^{\frac{-1}{2}}AD^{\frac{1}{2}}
L=I−D2−1AD21,这一步在原论文中成为正则化的结果.另外为了防止参数过多,令
θ
=
θ
0
=
−
θ
1
\theta=\theta_{0}=-\theta_{1}
θ=θ0=−θ1,那么式(24)可以变成
y
=
θ
(
I
+
D
−
1
2
A
D
1
2
)
x
(25)
y=\theta(I+D^{\frac{-1}{2}}AD^{\frac{1}{2}})x \tag{25}
y=θ(I+D2−1AD21)x(25).
又令
I
+
D
−
1
2
A
D
1
2
=
I
+
D
~
−
1
2
A
D
~
1
2
I+D^{\frac{-1}{2}}AD^{\frac{1}{2}}=I+\widetilde{D}^{\frac{-1}{2}}A\widetilde{D}^{\frac{1}{2}}
I+D2−1AD21=I+D
2−1AD
21,最终的形式变成了
y
=
θ
(
D
~
−
1
2
A
D
~
1
2
)
x
(26)
y=\theta(\widetilde{D}^{\frac{-1}{2}}A\widetilde{D}^{\frac{1}{2}})x \tag{26}
y=θ(D
2−1AD
21)x(26).
式(26)再加上一个非线性变化也就是
G
C
N
GCN
GCN的最终形式
y
=
σ
(
θ
(
D
~
−
1
2
A
D
~
1
2
)
x
)
(26)
y=\sigma(\theta(\widetilde{D}^{\frac{-1}{2}}A\widetilde{D}^{\frac{1}{2}})x )\tag{26}
y=σ(θ(D
2−1AD
21)x)(26).
代码部分也是参考式(26)的矩阵变形形式所写
y
=
σ
(
(
D
~
−
1
2
A
D
~
1
2
)
x
θ
)
(27)
y=\sigma((\widetilde{D}^{\frac{-1}{2}}A\widetilde{D}^{\frac{1}{2}})x \theta)\tag{27}
y=σ((D
2−1AD
21)xθ)(27).
把最终的结果转换成和GAT一样的参数变量,也即令
y
=
h
l
+
1
y=h^{l+1}
y=hl+1,
x
=
h
l
x = h^{l}
x=hl 就有
h
l
+
1
=
σ
(
(
D
~
−
1
2
A
D
~
1
2
)
h
l
θ
)
(28)
h^{l+1}=\sigma((\widetilde{D}^{\frac{-1}{2}}A\widetilde{D}^{\frac{1}{2}})h^{l} \theta)\tag{28}
hl+1=σ((D
2−1AD
21)hlθ)(28).
有了公式 ( 28 ) (28) (28), 我们可以很容易的实现 G C N GCN GCN的代码, 具体代码参考GCN的pytorch代码. 这个代码的实现只是我自己参考别人的代码实现,有些地方作了改动。当然也有star很多的代码星比较多的代码
结语
至此,GAT和GCN都基本讲完了, 在这个过程也学了很多东西,才怪.
参考文献
[1] https://arxiv.org/abs/1710.10903.
[2] https://blog.csdn.net/weixin_36474809/article/details/89401552.
[3]http://webia.lip6.fr/~durandt/pdfs/2017_CVPR/Durand_WILDCAT_CVPR_2017.pdf.
[4] https://www.zhihu.com/question/54504471?sort=created.
[5]https://www.bilibili.com/video/BV1G54y1971Sfrom=search&seid=3673449636835487631.
[6] https://blog.csdn.net/weixin_40013463/article/details/81089223.
[7] https://www.cnblogs.com/SivilTaram/p/graph_neural_network_1.html.