# Graph Convolutional Network¶

Author: Qi Huang, Minjie Wang, Yu Gai, Quan Gan, Zheng Zhang

This is a gentle introduction of using DGL to implement Graph Convolutional Networks (Kipf & Welling et al., Semi-Supervised Classification with Graph Convolutional Networks). We build upon the earlier tutorial on DGLGraph and demonstrate how DGL combines graph with deep neural network and learn structural representations.

## Model Overview¶

### GCN from the perspective of message passing¶

We describe a layer of graph convolutional neural network from a message passing perspective; the math can be found here. It boils down to the following step, for each node $$u$$:

1) Aggregate neighbors’ representations $$h_{v}$$ to produce an intermediate representation $$\hat{h}_u$$. 2) Transform the aggregated representation $$\hat{h}_{u}$$ with a linear projection followed by a non-linearity: $$h_{u} = f(W_{u} \hat{h}_u)$$.

We will implement step 1 with DGL message passing, and step 2 with the apply_nodes method, whose node UDF will be a PyTorch nn.Module.

### GCN implementation with DGL¶

We first define the message and reduce function as usual. Since the aggregation on a node $$u$$ only involves summing over the neighbors’ representations $$h_v$$, we can simply use builtin functions:

import dgl
import dgl.function as fn
import torch as th
import torch.nn as nn
import torch.nn.functional as F
from dgl import DGLGraph

gcn_msg = fn.copy_src(src='h', out='m')
gcn_reduce = fn.sum(msg='m', out='h')


We then define the node UDF for apply_nodes, which is a fully-connected layer:

class NodeApplyModule(nn.Module):
def __init__(self, in_feats, out_feats, activation):
super(NodeApplyModule, self).__init__()
self.linear = nn.Linear(in_feats, out_feats)
self.activation = activation

def forward(self, node):
h = self.linear(node.data['h'])
if self.activation is not None:
h = self.activation(h)
return {'h' : h}


We then proceed to define the GCN module. A GCN layer essentially performs message passing on all the nodes then applies the NodeApplyModule. Note that we omitted the dropout in the paper for simplicity.

class GCN(nn.Module):
def __init__(self, in_feats, out_feats, activation):
super(GCN, self).__init__()
self.apply_mod = NodeApplyModule(in_feats, out_feats, activation)

def forward(self, g, feature):
g.ndata['h'] = feature
g.update_all(gcn_msg, gcn_reduce)
g.apply_nodes(func=self.apply_mod)
return g.ndata.pop('h')


The forward function is essentially the same as any other commonly seen NNs model in PyTorch. We can initialize GCN like any nn.Module. For example, let’s define a simple neural network consisting of two GCN layers. Suppose we are training the classifier for the cora dataset (the input feature size is 1433 and the number of classes is 7). The last GCN layer computes node embeddings, so the last layer in general doesn’t apply activation.

class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.gcn1 = GCN(1433, 16, F.relu)
self.gcn2 = GCN(16, 7, None)

def forward(self, g, features):
x = self.gcn1(g, features)
x = self.gcn2(g, x)
return x
net = Net()
print(net)


Out:

Net(
(gcn1): GCN(
(apply_mod): NodeApplyModule(
(linear): Linear(in_features=1433, out_features=16, bias=True)
)
)
(gcn2): GCN(
(apply_mod): NodeApplyModule(
(linear): Linear(in_features=16, out_features=7, bias=True)
)
)
)


We load the cora dataset using DGL’s built-in data module.

from dgl.data import citation_graph as citegrh
import networkx as nx
features = th.FloatTensor(data.features)
labels = th.LongTensor(data.labels)
g = data.graph
g.remove_edges_from(nx.selfloop_edges(g))
g = DGLGraph(g)


When a model is trained, we can use the following method to evaluate the performance of the model on the test dataset:

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


We then train the network as follows:

import time
import numpy as np
dur = []
for epoch in range(50):
if epoch >=3:
t0 = time.time()

net.train()
logits = net(g, features)
logp = F.log_softmax(logits, 1)

loss.backward()
optimizer.step()

if epoch >=3:
dur.append(time.time() - t0)

acc = evaluate(net, g, features, labels, test_mask)
print("Epoch {:05d} | Loss {:.4f} | Test Acc {:.4f} | Time(s) {:.4f}".format(
epoch, loss.item(), acc, np.mean(dur)))


Out:

/home/ubuntu/.pyenv/versions/miniconda3-latest/lib/python3.7/site-packages/numpy/core/fromnumeric.py:3257: RuntimeWarning: Mean of empty slice.
out=out, **kwargs)
/home/ubuntu/.pyenv/versions/miniconda3-latest/lib/python3.7/site-packages/numpy/core/_methods.py:161: RuntimeWarning: invalid value encountered in double_scalars
ret = ret.dtype.type(ret / rcount)
Epoch 00000 | Loss 1.9444 | Test Acc 0.1470 | Time(s) nan
Epoch 00001 | Loss 1.9181 | Test Acc 0.1610 | Time(s) nan
Epoch 00002 | Loss 1.8911 | Test Acc 0.1900 | Time(s) nan
Epoch 00003 | Loss 1.8613 | Test Acc 0.2360 | Time(s) 0.0839
Epoch 00004 | Loss 1.8310 | Test Acc 0.2630 | Time(s) 0.0840
Epoch 00005 | Loss 1.8007 | Test Acc 0.2850 | Time(s) 0.0837
Epoch 00006 | Loss 1.7705 | Test Acc 0.2980 | Time(s) 0.0836
Epoch 00007 | Loss 1.7408 | Test Acc 0.3100 | Time(s) 0.0838
Epoch 00008 | Loss 1.7109 | Test Acc 0.3170 | Time(s) 0.0837
Epoch 00009 | Loss 1.6810 | Test Acc 0.3280 | Time(s) 0.0839
Epoch 00010 | Loss 1.6513 | Test Acc 0.3550 | Time(s) 0.0841
Epoch 00011 | Loss 1.6219 | Test Acc 0.3760 | Time(s) 0.0841
Epoch 00012 | Loss 1.5942 | Test Acc 0.3910 | Time(s) 0.0840
Epoch 00013 | Loss 1.5674 | Test Acc 0.4030 | Time(s) 0.0841
Epoch 00014 | Loss 1.5413 | Test Acc 0.4140 | Time(s) 0.0841
Epoch 00015 | Loss 1.5157 | Test Acc 0.4270 | Time(s) 0.0842
Epoch 00016 | Loss 1.4912 | Test Acc 0.4430 | Time(s) 0.0842
Epoch 00017 | Loss 1.4676 | Test Acc 0.4520 | Time(s) 0.0843
Epoch 00018 | Loss 1.4451 | Test Acc 0.4600 | Time(s) 0.0842
Epoch 00019 | Loss 1.4233 | Test Acc 0.4640 | Time(s) 0.0842
Epoch 00020 | Loss 1.4021 | Test Acc 0.4730 | Time(s) 0.0842
Epoch 00021 | Loss 1.3815 | Test Acc 0.4760 | Time(s) 0.0842
Epoch 00022 | Loss 1.3616 | Test Acc 0.4810 | Time(s) 0.0842
Epoch 00023 | Loss 1.3423 | Test Acc 0.4890 | Time(s) 0.0842
Epoch 00024 | Loss 1.3236 | Test Acc 0.5080 | Time(s) 0.0842
Epoch 00025 | Loss 1.3056 | Test Acc 0.5180 | Time(s) 0.0843
Epoch 00026 | Loss 1.2881 | Test Acc 0.5240 | Time(s) 0.0843
Epoch 00027 | Loss 1.2713 | Test Acc 0.5310 | Time(s) 0.0844
Epoch 00028 | Loss 1.2550 | Test Acc 0.5400 | Time(s) 0.0843
Epoch 00029 | Loss 1.2392 | Test Acc 0.5570 | Time(s) 0.0844
Epoch 00030 | Loss 1.2238 | Test Acc 0.5670 | Time(s) 0.0844
Epoch 00031 | Loss 1.2089 | Test Acc 0.5800 | Time(s) 0.0843
Epoch 00032 | Loss 1.1944 | Test Acc 0.5860 | Time(s) 0.0843
Epoch 00033 | Loss 1.1803 | Test Acc 0.5960 | Time(s) 0.0843
Epoch 00034 | Loss 1.1666 | Test Acc 0.6000 | Time(s) 0.0843
Epoch 00035 | Loss 1.1532 | Test Acc 0.6070 | Time(s) 0.0843
Epoch 00036 | Loss 1.1401 | Test Acc 0.6160 | Time(s) 0.0843
Epoch 00037 | Loss 1.1273 | Test Acc 0.6220 | Time(s) 0.0843
Epoch 00038 | Loss 1.1147 | Test Acc 0.6240 | Time(s) 0.0843
Epoch 00039 | Loss 1.1023 | Test Acc 0.6310 | Time(s) 0.0843
Epoch 00040 | Loss 1.0901 | Test Acc 0.6340 | Time(s) 0.0843
Epoch 00041 | Loss 1.0782 | Test Acc 0.6400 | Time(s) 0.0843
Epoch 00042 | Loss 1.0664 | Test Acc 0.6410 | Time(s) 0.0843
Epoch 00043 | Loss 1.0548 | Test Acc 0.6460 | Time(s) 0.0842
Epoch 00044 | Loss 1.0434 | Test Acc 0.6470 | Time(s) 0.0842
Epoch 00045 | Loss 1.0322 | Test Acc 0.6520 | Time(s) 0.0842
Epoch 00046 | Loss 1.0211 | Test Acc 0.6600 | Time(s) 0.0842
Epoch 00047 | Loss 1.0101 | Test Acc 0.6600 | Time(s) 0.0841
Epoch 00048 | Loss 0.9993 | Test Acc 0.6650 | Time(s) 0.0841
Epoch 00049 | Loss 0.9886 | Test Acc 0.6670 | Time(s) 0.0841


## GCN in one formula¶

Mathematically, the GCN model follows this formula:

$$H^{(l+1)} = \sigma(\tilde{D}^{-\frac{1}{2}}\tilde{A}\tilde{D}^{-\frac{1}{2}}H^{(l)}W^{(l)})$$

Here, $$H^{(l)}$$ denotes the $$l^{th}$$ layer in the network, $$\sigma$$ is the non-linearity, and $$W$$ is the weight matrix for this layer. $$D$$ and $$A$$, as commonly seen, represent degree matrix and adjacency matrix, respectively. The ~ is a renormalization trick in which we add a self-connection to each node of the graph, and build the corresponding degree and adjacency matrix. The shape of the input $$H^{(0)}$$ is $$N \times D$$, where $$N$$ is the number of nodes and $$D$$ is the number of input features. We can chain up multiple layers as such to produce a node-level representation output with shape :mathN times F, where $$F$$ is the dimension of the output node feature vector.

The equation can be efficiently implemented using sparse matrix multiplication kernels (such as Kipf’s pygcn code). The above DGL implementation in fact has already used this trick due to the use of builtin functions. To understand what is under the hood, please read our tutorial on PageRank.

Total running time of the script: ( 0 minutes 17.986 seconds)

Gallery generated by Sphinx-Gallery