Skip to content
ml-recall

Topic notes

Graph Neural Networks

Graph neural networks; graphs as data, permutation symmetry, message passing, GCN, GraphSAGE, GAT, readout, depth and over-smoothing, and PyTorch Geometric's edge_index.

5-min read AI-checked

Practise 31 questions Take a test

Key ideas

Formulas

H(l+1)=σ(D~−1/2A~D~−1/2H(l)W(l)),A~=A+I,D~ii=∑jA~ijH^{(l+1)} = \sigma\big(\tilde D^{-1/2}\tilde A\tilde D^{-1/2} H^{(l)} W^{(l)}\big), \qquad \tilde A = A + I, \quad \tilde D_{ii} = \sum_j \tilde A_{ij}

Kipf and Welling write A^\hat A for the whole normalised matrix D~−1/2A~D~−1/2\tilde D^{-1/2}\tilde A\tilde D^{-1/2}2; some texts use A^\hat A, D^\hat D for A+IA + I and its degrees, so check which is meant. Node-wise, with d~i=D~ii\tilde d_i = \tilde D_{ii} (the degree counting the self-loop): hi′=σ(∑j∈N(i)∪{i}1d~id~j W⊤hj)h_i' = \sigma\big(\sum_{j \in \mathcal N(i) \cup \{i\}} \frac{1}{\sqrt{\tilde d_i \tilde d_j}}\, W^\top h_j\big). A layer with CC inputs and FF outputs has a C×FC \times F weight matrix (CFCF parameters, plus FF with a bias), whatever NN and ∣E∣|E| are2.

General message passing, GraphSAGE and GAT1,3,4:

hi′=γ(hi,⨁j∈N(i)ϕ(hi,hj,eji)),hvk=σ(Wk⋅CONCAT(hvk−1,AGGk{huk−1:u∈N(v)}))h_i' = \gamma\Big(h_i, \bigoplus_{j \in \mathcal N(i)} \phi(h_i, h_j, e_{ji})\Big), \qquad h_v^{k} = \sigma\big(W^k \cdot \mathrm{CONCAT}(h_v^{k-1}, \mathrm{AGG}_k\{h_u^{k-1} : u \in \mathcal N(v)\})\big) αij=exp⁡(LeakyReLU(a⊤[Whi ∥ Whj]))∑k∈N(i)exp⁡(LeakyReLU(a⊤[Whi ∥ Whk])),hi′=σ(∑j∈N(i)αijWhj)\alpha_{ij} = \frac{\exp\big(\mathrm{LeakyReLU}(\mathbf a^\top [W h_i \,\Vert\, W h_j])\big)}{\sum_{k \in \mathcal N(i)} \exp\big(\mathrm{LeakyReLU}(\mathbf a^\top [W h_i \,\Vert\, W h_k])\big)}, \qquad h_i' = \sigma\Big(\sum_{j \in \mathcal N(i)} \alpha_{ij} W h_j\Big)

GraphSAGE also rescales hvkh_v^k to unit length3. In the GAT formulas N(i)\mathcal N(i) includes ii itself; the LeakyReLU has slope 0.2; one head has FF′+2F′FF' + 2F' parameters (WW and a\mathbf a), and KK heads multiply this by KK4. Readout: hG=∑vhvh_G = \sum_v h_v (or the mean or max). Over-smoothing limit on a connected graph: A^=D~−1/2A~D~−1/2\hat A = \tilde D^{-1/2}\tilde A\tilde D^{-1/2} satisfies A^ D~1/21=D~1/21\hat A\,\tilde D^{1/2}\mathbf 1 = \tilde D^{1/2}\mathbf 1, so A^kx\hat A^k x tends to a multiple of D~1/21\tilde D^{1/2}\mathbf 1 (entries proportional to d~i\sqrt{\tilde d_i}), while the mean (D~−1A~)kx(\tilde D^{-1}\tilde A)^k x tends to a constant vector5.

Worked example

Star graph: centre 0 with leaves 1, 2, 3; features x=(2,1,0,3)x = (2, 1, 0, 3), W=1W = 1, no bias, ReLU.

In code

import torch
from torch_geometric.data import Data
from torch_geometric.nn import GCNConv

edge_index = torch.tensor([[0, 1, 0, 2, 0, 3],   # source nodes
                           [1, 0, 2, 0, 3, 0]])  # target nodes (both directions)
data = Data(x=torch.tensor([[2.0], [1.0], [0.0], [3.0]]), edge_index=edge_index)
print(data.num_nodes, data.num_edges)  # 4 6
conv = GCNConv(1, 1)  # adds self-loops, symmetric normalisation, bias
with torch.no_grad():
    conv.lin.weight.fill_(1.0)
    conv.bias.zero_()
    print(conv(data.x, data.edge_index).view(-1))  # tensor([1.9142, 1.2071, 0.7071, 2.2071])
print(sum(p.numel() for p in GCNConv(1433, 16).parameters()))  # 22944

Common pitfalls

References

  1. Sanchez-Lengeling et al. (2021), A Gentle Introduction to Graph Neural Networks, Distill — Images as graphs; What types of problems have graph structured data?; The challenges of using graphs in machine learning; Graph Neural Networks (pooling, passing messages); Into the Weeds (Comparing aggregation operations; matrix multiplications as walks on a graph)
  2. Kipf & Welling (2017), Semi-Supervised Classification with Graph Convolutional Networks — Sec. 2 (Eq. 2); Sec. 2.2 Layer-Wise Linear Model (Eqs. 7–8, renormalization trick); Sec. 3.1 Example (Eq. 9); Sec. 4.2 Neural Networks on Graphs; Sec. 5–6 (Cora set-up); Appendix B Experiments on Model Depth
  3. Hamilton, Ying & Leskovec (2017), Inductive Representation Learning on Large Graphs — Abstract; Sec. 3.1 Embedding generation (Algorithm 1, Neighborhood definition); Sec. 3.3 Aggregator Architectures; Appendix A Minibatch pseudocode
  4. Veličković et al. (2018), Graph Attention Networks — Sec. 2.1 Graph Attentional Layer (Eqs. 1–6); Sec. 2.2 Comparisons to Related Work; Sec. 3.1 Datasets (Inductive learning); Sec. 3.3 Experimental Setup
  5. Li, Han & Wu (2018), Deeper Insights into Graph Convolutional Networks for Semi-Supervised Learning — Sec. 3 Analysis (Why GCNs Work; When GCNs Fail, i.e. the karate-club experiment, the text before Theorem 1 and its proof)
  6. PyTorch Geometric documentation: Introduction by Example — Data Handling of Graphs; Common Benchmark Datasets; Mini-batches