Writing

Graph Neural Networks- Architectures review!

In short: a walk through the graph neural networks I found most useful. GCN, DeepWalk and GraphSAGE, starting from the three matrices you need before any of it makes sense. If a problem can be drawn…

published
read time
4 min
words
838
lang
en

In short: a walk through the graph neural networks I found most useful. GCN, DeepWalk and GraphSAGE, starting from the three matrices you need before any of it makes sense.

A graph of numbered nodes in four colour-coded clusters

If a problem can be drawn as a graph, a graph neural network is often the accurate and efficient way to answer it. I start with the simple models and work up to the newer ones. ChebNet gets its own post.

Ball and stick model of an acetone molecule: three black carbon atoms, a red oxygen atom on a double bond and six white hydrogen atoms
The ball-and-stick model is the simplest use of graphs in chemistry.

The vocabulary you need first

I am mostly following Kipf and Welling on semi-supervised classification with GCNs [1]. I plotted the example graph with GraphOnline.

G = (v, e)

That is, a graph G is made of v nodes and e edges.

Example graph with nodes 0 to 4 joined by five weighted edges: 0-1 (42), 0-2 (3), 1-3 (1), 2-3 (11) and 2-4 (5)
Five nodes (vertices) and five edges (links). Edges can carry a value.

Three matrices do most of the work:

Five by five adjacency matrix A of the example graph, holding the edge weights 42, 3, 1, 11 and 5
The adjacency matrix is node x node. It says which nodes are connected.
Five by five diagonal degree matrix D with 2, 2, 2, 2 and 1 on the diagonal
The degree matrix is a node x node diagonal matrix. It counts the edges landing on each node.
Five by five Laplacian matrix L, with the degrees on the diagonal and the negated edge weights elsewhere
The Laplacian matrix is just D minus A.

Those three are the key to graph-based processing. You can define others. The feature matrix holds the features of each node. If the nodes are people, the features might be age, height and weight. So it has n rows and f columns, where f is the number of features.


Graph Convolutional Network (GCN)

Think of the graph you just drew as layer one. What is layer two? You apply some math to the matrices, and the math runs on the node features. So you have a function F, adjacency A, and features X. Define Hl for the values at each layer.

Equation: H superscript 0 equals X

In math:

Equation: H at layer l plus 1 equals F of H at layer l and A

F can be anything. I use ReLU. You also need a matrix W to carry the layer-specific trainable weights.

Equation: H at layer l plus 1 equals ReLU of A times H at layer l times W at layer l

In a GCN, each node ends up as the aggregation of its neighbours. This is where semi-supervised learning comes in. In a graph you often do not have a label for every node, so you use the labelled ones to predict the rest. In fully supervised learning you have all the labels. Here you do not, hence semi-supervised.

CarefulAggregation is biased by degree. In the example graph node 2 has three edges and node 4 has one. More neighbours, more value, for no good reason. You have to normalize symmetrically using the degree matrix.
Equations: A hat equals A plus I, and normalized A equals D hat to the minus one half, times A hat, times D hat to the minus one half

I is the identity matrix, which is what lets you normalize A. D hat is the diagonal degree matrix of A hat. That gives the final equation:

Equation: H at layer l plus 1 equals ReLU of D hat to the minus one half, A hat, D hat to the minus one half, H, W at layer l

DeepWalk

My reference here is "DeepWalk: Online Learning of Social Representations" by Perozzi et al., who demonstrate it on Zachary's Karate network [2]. This one learns unsupervised, so there may be no feature matrix at all. The algorithm starts from random features and develops them each iteration.

The problem they were solving: cluster the members of a karate club.

Graph of the 34 karate club members with their links, coloured into four clusters: purple, green, red and cyan
Karate club clustered in four.

They separated labels from features to prevent cascading errors. You learn from a subset of the data, and RandomWalk plus SkipGram get you the rest of the way. In RandomWalk, at each node you pick a walk size and a walk length.


GraphSAGE

GraphSAGE is the practical one. With the earlier networks, growing the graph means retraining the model. GraphSAGE makes that easy [3].

  1. Sample the neighbourhoodPick which neighbours of a node you are going to look at.
  2. AggregateCombine the feature information coming from those neighbours.
  3. PredictProduce the graph context and the label.

Earlier nets

  • Learn a vector per node
  • Extending the graph means training again

GraphSAGE

  • Learns a set of aggregation functions over a node's neighbourhood
  • Handles nodes it has not seen

Time to define embedding, which is not a strange thing at all. It is a calculation on node features. Picture a graph of three nodes in a triangle. To get the new value for node 0, you feed its neighbours' values into a function. Averaging, for example.

Hand-drawn sketch: nodes 1 and 2 feed into a box labelled function, whose output goes to node 0

First assign random values to each node. Here are the random numbers I gave the example graph:

The example graph with a random value next to each node: 0.8, 0.5, 0.3, 0.6 and 0.7 for nodes 0 to 4

Take node 2. Capture its neighbours and apply the function. Nodes 0, 3 and 4 are the input, so 0.8, 0.6 and 0.7 go in and the output is the new value of node 2.

CarefulNotice node 2 itself never went into the function. That is a disaster. When you implement this, add a self loop to every node so its own value is included.

Repeat for every node and you have layer one. Do it again for layer two.

Which function should you use? Anything, even a small neural network. Mostly people average:

Equation: the new value of node 2 equals 0.3 plus 0.8 plus 0.7 plus 0.6, divided by 4, equals 1.95

Or take the maximum, which picks 0.8 in this example. That is the pool aggregator. And note this was one-hop embedding. In two-hop you pull in the neighbours of the neighbours as well.

Next

In the next posts I will cover the networks used in graph signal processing (GSP).

References

  1. arxiv.org/abs/1609.02907
  2. arxiv.org/abs/1403.6652
  3. snap.stanford.edu/graphsage

related

Keep reading