GCN代码简析(dgl实现)

1. GCN

论文:SEMI-SUPERVISED CLASSIFICATION WITH GRAPH CONVOLUTIONAL NETWORKS

GCN的逐层传播公式为:$$H^{(l+1)}=\sigma\left(\tilde{D}^{-\frac12}\tilde{A}\tilde{D}^{-\frac12}H^{(l)}W^{(l)}\right)$$

其中:

  • $\tilde A = A + I_N$ 表示无向图G加上了自己的邻接矩阵

  • $\tilde D_{ii} = \sum_j \tilde A_{ij}$ 是节点的度

  • $W^{(l)}$ 是l层的可学习参数

  • $H^{(l)} \in \mathbb R^{N \times D}$ 是l层激活后的节点Embedding,$H_{0} = X$

推导过程参考:

GNN 教程:GCN - ArchWalker

其中切比雪夫多项式近似的部分可以参考:

Chebyshev多项式作为GCN卷积核

如果想更多了解一些GCN相关的原理可以参考:

如何理解 Graph Convolutional Network(GCN)?

2. GCN代码实现与GraphConv源码解读

GCN代码实现

GCN代码为官方给出的示例,地址为:dgl官方GCN实例

import argparse

import dgl
import dgl.nn as dglnn

import torch
import torch.nn as nn
import torch.nn.functional as F
from dgl import AddSelfLoop
from dgl.data import CiteseerGraphDataset, CoraGraphDataset, PubmedGraphDataset


class GCN(nn.Module):
def __init__(self, in_size, hid_size, out_size):
super(GCN).__init__()
self.layers = nn.ModuleList()
# two-layer GCN
# 定义了一个两层的GCN
self.layers.append(
dglnn.GraphConv(in_size, hid_size, activation=F.relu)
)
self.layers.append(dglnn.GraphConv(hid_size, out_size))
self.dropout = nn.Dropout(0.5)

def forward(self, g, features):
h = features
for i, layer in enumerate(self.layers):
# 在第一层传播之前不进行dropout
if i != 0:
h = self.dropout(h)
h = layer(g, h)
return h


def evaluate(g, features, labels, mask, model):
model.eval()
with torch.no_grad():
logits = model(g, features)
logits = logits[mask]
labels = labels[mask]
_, indices = torch.max(logits, dim=1)
correct = torch.sum(indices == labels)
return correct.item() * 1.0 / len(labels)


def train(g, features, labels, masks, model):
# define train/val samples, loss function and optimizer
train_mask = masks[0]
val_mask = masks[1]
loss_fcn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-2, weight_decay=5e-4)

# training loop
for epoch in range(200):
model.train()
logits = model(g, features)
loss = loss_fcn(logits[train_mask], labels[train_mask])
optimizer.zero_grad()
loss.backward()
optimizer.step()
acc = evaluate(g, features, labels, val_mask, model)
print(
"Epoch {:05d} | Loss {:.4f} | Accuracy {:.4f} ".format(
epoch, loss.item(), acc
)
)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="cora",
help="Dataset name ('cora', 'citeseer', 'pubmed').",
)
parser.add_argument(
"--dt",
type=str,
default="float",
help="data type(float, bfloat16)",
)
args = parser.parse_args()
print(f"Training with DGL built-in GraphConv module.")

# load and preprocess dataset
# 在加载数组集前对图添加自环
transform = (
AddSelfLoop()
) # by default, it will first remove self-loops to prevent duplication
if args.dataset == "cora":
data = CoraGraphDataset(transform=transform)
elif args.dataset == "citeseer":
data = CiteseerGraphDataset(transform=transform)
elif args.dataset == "pubmed":
data = PubmedGraphDataset(transform=transform)
else:
raise ValueError("Unknown dataset: {}".format(args.dataset))
g = data[0]
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
g = g.int().to(device)
features = g.ndata["feat"]
labels = g.ndata["label"]
masks = g.ndata["train_mask"], g.ndata["val_mask"], g.ndata["test_mask"]

# create GCN model
in_size = features.shape[1]
out_size = data.num_classes
model = GCN(in_size, 16, out_size).to(device)

# convert model and graph to bfloat16 if needed
if args.dt == "bfloat16":
g = dgl.to_bfloat16(g)
features = features.to(dtype=torch.bfloat16)
model = model.to(dtype=torch.bfloat16)

# model training
print("Training...")
train(g, features, labels, masks, model)

# test the model
print("Testing...")
acc = evaluate(g, features, labels, masks[2], model)
print("Test accuracy {:.4f}".format(acc))

GraphConv源码解读

下方为dgl中GraphConv层的源码,此代码为dgl(1.1.1版本的实现),官方文档地址为:GraphConv,本文对源码添加了注释。

注释为个人理解,如发现错误请指正

文档中提及的无权图的逐层传播公式为

$h_i^{(l+1)} = \sigma(b^{(l)} + \sum_{j\in\mathcal{N}(i)}\frac{1}{c_{ji}}h_j^{(l)}W^{(l)})$

此公式为在节点层面的表示

其中$c_{ji}$为节点度的平方根因子,$c_{ji} = \sqrt{|\mathcal{N}(j)|}\sqrt{|\mathcal{N}(i)|}$。

此处的逐层传播公式实际上与上边的公式含义一致

$$H^{(l+1)}=\sigma\left(\tilde{D}^{-\frac12}\tilde{A}\tilde{D}^{-\frac12}H^{(l)}W^{(l)}\right)$$该式中的$\tilde{D}^{-\frac12}\tilde{A}\tilde{D}^{-\frac12}$实际上为对称归一化的拉普拉斯矩阵$L_{sym}$,而$L_{sym}$可以表示为

$$L_{i,j}^{\text{sym}}:=\left{\begin{matrix}1&\text{if} \ i=j \ \text {and}\deg(v_i)\neq0\\frac{1}{\sqrt{\deg(v_i)\deg(v_j)}}&\text{if} \ i\neq j \ \text{and} \ v_i \ \text{is adjacent to} \ v_j\0&\text{otherwise}\end{matrix}\right.$$

其中,$v_i$和$v_j$相连时,所乘的因子就相当于$c_{ij}$,由于在dgl中使用消息传播机制,因此不需要乘整个矩阵,只需要对对应的元素做出归一化即可。

class GraphConv(nn.Module):
r"""Graph convolutional layer from `Semi-Supervised Classification with Graph Convolutional
Networks <https://arxiv.org/abs/1609.02907>`__

Mathematically it is defined as follows:

.. math::
h_i^{(l+1)} = \sigma(b^{(l)} + \sum_{j\in\mathcal{N}(i)}\frac{1}{c_{ji}}h_j^{(l)}W^{(l)})

where :math:`\mathcal{N}(i)` is the set of neighbors of node :math:`i`,
:math:`c_{ji}` is the product of the square root of node degrees
(i.e., :math:`c_{ji} = \sqrt{|\mathcal{N}(j)|}\sqrt{|\mathcal{N}(i)|}`),
and :math:`\sigma` is an activation function.

If a weight tensor on each edge is provided, the weighted graph convolution is defined as:

.. math::
h_i^{(l+1)} = \sigma(b^{(l)} + \sum_{j\in\mathcal{N}(i)}\frac{e_{ji}}{c_{ji}}h_j^{(l)}W^{(l)})

where :math:`e_{ji}` is the scalar weight on the edge from node :math:`j` to node :math:`i`.
This is NOT equivalent to the weighted graph convolutional network formulation in the paper.

To customize the normalization term :math:`c_{ji}`, one can first set ``norm='none'`` for
the model, and send the pre-normalized :math:`e_{ji}` to the forward computation. We provide
:class:`~dgl.nn.pytorch.EdgeWeightNorm` to normalize scalar edge weight following the GCN paper.

Parameters
----------
in_feats : int
Input feature size; i.e, the number of dimensions of :math:`h_j^{(l)}`.
out_feats : int
Output feature size; i.e., the number of dimensions of :math:`h_i^{(l+1)}`.
norm : str, optional
How to apply the normalizer. Can be one of the following values:

* ``right``, to divide the aggregated messages by each node's in-degrees,
which is equivalent to averaging the received messages.

* ``none``, where no normalization is applied.

* ``both`` (default), where the messages are scaled with :math:`1/c_{ji}` above, equivalent
to symmetric normalization.

* ``left``, to divide the messages sent out from each node by its out-degrees,
equivalent to random walk normalization.
weight : bool, optional
If True, apply a linear layer. Otherwise, aggregating the messages
without a weight matrix.
bias : bool, optional
If True, adds a learnable bias to the output. Default: ``True``.
activation : callable activation function/layer or None, optional
If not None, applies an activation function to the updated node features.
Default: ``None``.
allow_zero_in_degree : bool, optional
If there are 0-in-degree nodes in the graph, output for those nodes will be invalid
since no message will be passed to those nodes. This is harmful for some applications
causing silent performance regression. This module will raise a DGLError if it detects
0-in-degree nodes in input graph. By setting ``True``, it will suppress the check
and let the users handle it by themselves. Default: ``False``.

Attributes
----------
weight : torch.Tensor
The learnable weight tensor.
bias : torch.Tensor
The learnable bias tensor.

Note
----
Zero in-degree nodes will lead to invalid output value. This is because no message
will be passed to those nodes, the aggregation function will be appied on empty input.
A common practice to avoid this is to add a self-loop for each node in the graph if
it is homogeneous, which can be achieved by:

>>> g = ... # a DGLGraph
>>> g = dgl.add_self_loop(g)

Calling ``add_self_loop`` will not work for some graphs, for example, heterogeneous graph
since the edge type can not be decided for self_loop edges. Set ``allow_zero_in_degree``
to ``True`` for those cases to unblock the code and handle zero-in-degree nodes manually.
A common practise to handle this is to filter out the nodes with zero-in-degree when use
after conv.

Examples
--------
>>> import dgl
>>> import numpy as np
>>> import torch as th
>>> from dgl.nn import GraphConv

>>> # Case 1: Homogeneous graph
>>> g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3]))
>>> g = dgl.add_self_loop(g)
>>> feat = th.ones(6, 10)
>>> conv = GraphConv(10, 2, norm='both', weight=True, bias=True)
>>> res = conv(g, feat)
>>> print(res)
tensor([[ 1.3326, -0.2797],
[ 1.4673, -0.3080],
[ 1.3326, -0.2797],
[ 1.6871, -0.3541],
[ 1.7711, -0.3717],
[ 1.0375, -0.2178]], grad_fn=<AddBackward0>)
>>> # allow_zero_in_degree example
>>> g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3]))
>>> conv = GraphConv(10, 2, norm='both', weight=True, bias=True, allow_zero_in_degree=True)
>>> res = conv(g, feat)
>>> print(res)
tensor([[-0.2473, -0.4631],
[-0.3497, -0.6549],
[-0.3497, -0.6549],
[-0.4221, -0.7905],
[-0.3497, -0.6549],
[ 0.0000, 0.0000]], grad_fn=<AddBackward0>)

>>> # Case 2: Unidirectional bipartite graph
>>> u = [0, 1, 0, 0, 1]
>>> v = [0, 1, 2, 3, 2]
>>> g = dgl.heterograph({('_U', '_E', '_V') : (u, v)})
>>> u_fea = th.rand(2, 5)
>>> v_fea = th.rand(4, 5)
>>> conv = GraphConv(5, 2, norm='both', weight=True, bias=True)
>>> res = conv(g, (u_fea, v_fea))
>>> res
tensor([[-0.2994, 0.6106],
[-0.4482, 0.5540],
[-0.5287, 0.8235],
[-0.2994, 0.6106]], grad_fn=<AddBackward0>)
"""

def __init__(
self,
in_feats,
out_feats,
norm="both",
weight=True,
bias=True,
activation=None,
allow_zero_in_degree=False,
):
super(GraphConv, self).__init__()
if norm not in ("none", "both", "right", "left"):
raise DGLError(
'Invalid norm value. Must be either "none", "both", "right" or "left".'
' But got "{}".'.format(norm)
)
# 输入维度
self._in_feats = in_feats
# 输出维度
self._out_feats = out_feats
# 归一化方式
# right: 将聚合的消息除以结点的入度,相当于平均接受的消息
# none: 不使用
# both: 默认方式,消息将通过1/c_{ij}被缩放,相当于对称归一化
# left: 将聚合的消息除以结点的出度,相当于随机游走归一化
self._norm = norm
# 是否允许度为0的结点
self._allow_zero_in_degree = allow_zero_in_degree

# 是否使用权重
if weight:
self.weight = nn.Parameter(th.Tensor(in_feats, out_feats))
else:
self.register_parameter("weight", None)
# 是否使用偏置
if bias:
self.bias = nn.Parameter(th.Tensor(out_feats))
else:
self.register_parameter("bias", None)

# 初始化可学习参数
self.reset_parameters()
# 激活函数
self._activation = activation

def reset_parameters(self):
r"""

Description
-----------
Reinitialize learnable parameters.

Note
----
The model parameters are initialized as in the
`original implementation <https://github.com/tkipf/gcn/blob/master/gcn/layers.py>`__
where the weight :math:`W^{(l)}` is initialized using Glorot uniform initialization
and the bias is initialized to be zero.

"""
if self.weight is not None:
init.xavier_uniform_(self.weight)
if self.bias is not None:
init.zeros_(self.bias)

def set_allow_zero_in_degree(self, set_value):
r"""

Description
-----------
Set allow_zero_in_degree flag.
设置是否允许度为0的标志
Parameters
----------
set_value : bool
The value to be set to the flag.
"""
self._allow_zero_in_degree = set_value

def forward(self, graph, feat, weight=None, edge_weight=None):
r"""

Description
-----------
Compute graph convolution.
图卷积计算
Parameters
----------
graph : DGLGraph
The graph.
feat :
结点的特征,对同质图为(N, D)
N为结点个数, D为输入结点特征的维度
torch.Tensor or pair of torch.Tensor
If a torch.Tensor is given, it represents the input feature of shape
:math:`(N, D_{in})`
where :math:`D_{in}` is size of input feature, :math:`N` is the number of nodes.
If a pair of torch.Tensor is given, which is the case for bipartite graph, the pair
must contain two tensors of shape :math:`(N_{in}, D_{in_{src}})` and
:math:`(N_{out}, D_{in_{dst}})`.
weight :
可选参数,为卷积层提供一个权重,若卷积层已经存在一个权重,那么会抛出一个错误
torch.Tensor, optional
Optional external weight tensor.
edge_weight :
可选参数,边权重
torch.Tensor, optional
Optional tensor on the edge. If given, the convolution will weight
with regard to the message.

Returns
-------
返回输出特征
torch.Tensor
The output feature

Raises
------
DGLError
Case 1: 图中有度为0的结点,可以通过设置allow_zero=True来允许有度为0的结点。
If there are 0-in-degree nodes in the input graph, it will raise DGLError
since no message will be passed to those nodes. This will cause invalid output.
The error can be ignored by setting ``allow_zero_in_degree`` parameter to ``True``.

Case 2: 从外部提供权重的同时模块定义了自己的权重
External weight is provided while at the same time the module
has defined its own weight parameter.

Note
----
* 输入形状: (N, *, \text{in_feats})
Input shape: :math:`(N, *, \text{in_feats})` where * means any number of additional
dimensions, :math:`N` is the number of nodes.
* 输出形状: (N, *, \text{out_feats})
Output shape: :math:`(N, *, \text{out_feats})` where all but the last dimension are
the same shape as the input.
* 权重形状: (\text{in_feats}, \text{out_feats})
Weight shape: :math:`(\text{in_feats}, \text{out_feats})`.
"""
with graph.local_scope(): # 使用局部范围,不影响图中节点的值
if not self._allow_zero_in_degree: # 如果不允许输入度为0的结点
if (graph.in_degrees() == 0).any():
raise DGLError(
"There are 0-in-degree nodes in the graph, "
"output for those nodes will be invalid. "
"This is harmful for some applications, "
"causing silent performance regression. "
"Adding self-loop on the input graph by "
"calling `g = dgl.add_self_loop(g)` will resolve "
"the issue. Setting ``allow_zero_in_degree`` "
"to be `True` when constructing this module will "
"suppress the check and let the code run."
)
# 定义聚合函数
aggregate_fn = fn.copy_u("h", "m")
# 如果边有权重
if edge_weight is not None:
assert edge_weight.shape[0] == graph.num_edges()
graph.edata["_edge_weight"] = edge_weight
# 更换聚合函数,使特征乘以边权后再聚合
aggregate_fn = fn.u_mul_e("h", "_edge_weight", "m")

# (BarclayII) For RGCN on heterogeneous graphs we need to support GCN on bipartite.
# 将特征分解为源节点特征和目标节点特征
feat_src, feat_dst = expand_as_pair(feat, graph)
# 如果使用的归一化方式为‘left’或’both‘
if self._norm in ["left", "both"]:
# 获取所有结点的出度
# todo: to(feat_src)的作用和含义?
degs = graph.out_degrees().to(feat_src).clamp(min=1)
# 按归一化类型设置参数
if self._norm == "both":
# D^(-1/2)
norm = th.pow(degs, -0.5)
else:
norm = 1.0 / degs
# 修改norm的形状方便与feat_src相乘
shp = norm.shape + (1,) * (feat_src.dim() - 1)
norm = th.reshape(norm, shp)
# 相当于 D^(-1/2)* feat_src
feat_src = feat_src * norm

# 上文提到的raise DGLError的case2
if weight is not None:
if self.weight is not None:
raise DGLError(
"External weight is provided while at the same time the"
" module has defined its own weight parameter. Please"
" create the module with flag weight=False."
)
else:
weight = self.weight

# 如果输入维度大于输出维度, 就先乘W, 减小特征向量的大小方便聚合
# 否则就先聚合再乘以权重
if self._in_feats > self._out_feats:
# mult W first to reduce the feature size for aggregation.
if weight is not None:
feat_src = th.matmul(feat_src, weight)
# 将源节点特征放入特征域'h'
graph.srcdata["h"] = feat_src
# 通过上边定义的聚合函数, 首先将'h'的特征复制到'm', 然后将'm'的特征聚合存放在'h'
graph.update_all(aggregate_fn, fn.sum(msg="m", out="h"))
rst = graph.dstdata["h"]
else:
# aggregate first then mult W
# 与上边相同, 执行顺序不同, 优化计算
graph.srcdata["h"] = feat_src
graph.update_all(aggregate_fn, fn.sum(msg="m", out="h"))
rst = graph.dstdata["h"]
if weight is not None:
rst = th.matmul(rst, weight)

# right或者both
if self._norm in ["right", "both"]:
degs = graph.in_degrees().to(feat_dst).clamp(min=1)
if self._norm == "both":
norm = th.pow(degs, -0.5)
else:
norm = 1.0 / degs
shp = norm.shape + (1,) * (feat_dst.dim() - 1)
norm = th.reshape(norm, shp)
rst = rst * norm # 同上,rst * D^(-1/2)

# 如果有偏置,加上偏置值
if self.bias is not None:
rst = rst + self.bias

# 通过设置的激活函数进行激活
if self._activation is not None:
rst = self._activation(rst)

return rst

def extra_repr(self):
"""Set the extra representation of the module,
which will come into effect when printing the model.
"""
summary = "in={_in_feats}, out={_out_feats}"
summary += ", normalization={_norm}"
if "_activation" in self.__dict__:
summary += ", activation={_activation}"
return summary.format(**self.__dict__)