Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Introduction to Graphs and Graph Neural Networks

What is a graph?

A graph is a structure containing a set of nodes (also called vertices) and edges that link between nodes. We denote the set of nodes/vertices as VV and the edges as E⊆V×VE \subseteq V \times V.

A graph G=(V,E)G = (V, E) is said to have order n=∣V∣n = |V| and size s=∣E∣s = |E|. The degree dd of a particular vertex is given by the number of edges connected to it.

Adjacency matrix

The connections in a graph can be defined using an adjacency matrix AA. The entries AijA_{ij} are nonzero if there is an edge between vertices ii and jj. For a simple undirected graph, the adjacency matrix must be symmetric Aij=AjiA_{ij} = A_{ji}.

Nodes can have values (features)

Beyond just specifying connections between elements, graph nodes can contain information encoded in a feature vector x\mathbf{x}. Below, see an example where each node contains a scalar feature.

Edges can have values (weights)

Likewise, edges in a graph can have features, which are sometimes interpreted as weights on each connection. See example below

Graph edges can be directed

For directed graphs (or digraphs), edges are unidirectional

In this case the adjancy matrix no longer has to be symmetric!

Concrete examples

image.pngimage.png

image.png link

image.png link

image.png link

image.png link

Graph convolution

Let’s start by reviewing some of the key concepts from convolutional neural networks. CNNs leverage parameter sharing and sparse connectivity to achieve translation equivariance. Using local pooling, they can achieve local translation invariance as well. Finally, CNNs learn a hierarchy of features, starting with basic objects like edges and corners in early layers, followed by more complex features in later layers.

Beyond those properties, CNNs are computationally efficient. Their weight sharing means they have a smaller footprint than an FCNN applied to similar data, and the convolution operation can be applied to inputs of arbitrary spatial dimension. That is, a CNN trained on 224×224224 \times 224 images can be applied to 28×2828 \times 28 images.

Permutation invariance

Graph data does not require translation invariance, because there is no notion of spatial position. The only things graphs have are node features, adjacency matrix, and optionally edge features. They also require permutation invariance - if the order of the nodes are scrambled, the output of an operation must not change.

A basic graph convolution

One option is to sum over the incoming features at each node to determine the updated feature value. We can apply a weight matrix WijW_{ij} to each incoming feature, but it will be the same weight matrix. For example, the preactivation zi\mathbf{z}_i at node ii will be

zi=b+xi W1+∑j∈N(i)xj W2\begin{align*} \mathbf{z}_i &= \mathbf{b} + \mathbf{x}_i \, \mathbf{W}_1+ \sum_{j \in \mathcal{N}(i)} \mathbf{x}_j \, \mathbf{W}_2 \end{align*}

Here, b\mathbf{b} is a learnable bias, W1\mathbf{W}_1 is a matrix applied to the local feature, and W2\mathbf{W}_2 is a matrix applied to each neighbor node’s feature. In principle, W1,W2\mathbf{W}_1,\mathbf{W}_2 can be matrices of shape fin×foutf_{\text{in}} \times f_{\text{out}} to change the feature dimension at each update.

The term j∈N(i)j \in \mathcal{N}(i) is a sum over the neighbors of node ii. This could be computed using the adjacency matrix, i.e.

∑j∈N(i)xj=∑jAijxj\begin{align*} \sum_{j \in \mathcal{N}(i)} \mathbf{x}_j = \sum_{j} A_{ij} \mathbf{x}_j \end{align*}

More general graph convolutions

There are many ways to perform a graph convolution. The version above was the form proposed by Kipf and Welling in 2016 (link). A more general way to view this is as a two-step process:

  1. Message passing - each node gathers information from its neighbors

  2. Node update - each node updates its features based on the incoming messages

The message passing phase can be framed mathematically as

mi=∑j∈N(i)M(xi,xj,eij)\begin{align*} \mathbf{m}_i = \sum_{j \in \mathcal{N}(i)} M \left( \mathbf{x}_i, \mathbf{x}_j, e_{ij} \right) \end{align*}

Here, MM is the message function that determines the message from each neighbor jj based on their hidden states, and optionally any features or weights on the incoming edge. If there are no edge features, then we use eij=Aije_{ij} = A_{ij}.

In our previous example, the message function was simply

M(xi,xj,eij)=xjW2\begin{align*} M \left( \mathbf{x}_i, \mathbf{x}_j, e_{ij} \right) = \mathbf{x}_j \mathbf{W}_2 \end{align*}

Other flavors of message functions include attention mechanisms

M(xi,xj,eij)=a(xi,xj) ψ(xj)\begin{align*} M \left( \mathbf{x}_i, \mathbf{x}_j, e_{ij} \right) &= a(\mathbf{x}_i, \mathbf{x}_j) \, \psi (\mathbf{x}_j) \end{align*}

where a(xi,xj)a(\mathbf{x}_i, \mathbf{x}_j) is a learnable function that computes the importance of an incoming value ψ(xj)\psi(\mathbf{x}_j). A common formulation is scaled-dot product attention

M(xi,xj,eij)=softmax((xiWQ)⋅(xjWK)fk)(xjWV)\begin{align*} M \left( \mathbf{x}_i, \mathbf{x}_j, e_{ij} \right) &= \text{softmax} \left( \frac{\left( \mathbf{x}_i \mathbf{W}_Q \right) \cdot \left( \mathbf{x}_j \mathbf{W}_K \right)}{\sqrt{f_k}} \right) \left( \mathbf{x}_j \mathbf{W}_V \right) \end{align*}

Here, WQ,WK∈Rfin×fk\mathbf{W}_Q, \mathbf{W}_K \in \mathbb{R}^{f_{\text{in}} \times f_{k}} are query and key weight matrices and WV∈Rfin×fout\mathbf{W}_V \in \mathbb{R}^{f_{\text{in}} \times f_{\text{out}}} is a value weight matrix.

The node update phase uses an update function to compute the next feature value at the particular node.

xi(t+1)=Ut(mi(t),xi(t))\begin{align*} \mathbf{x}^{(t+1)}_i = U_t \left( \mathbf{m}^{(t)}_i, \mathbf{x}^{(t)}_i \right) \end{align*}

In our previous example, the node update function was simply

xi(t+1)=xi(t)W1+mi+b\begin{align*} \mathbf{x}^{(t+1)}_i = \mathbf{x}^{(t)}_i \mathbf{W}_1 + \mathbf{m}_i + \mathbf{b} \end{align*}

Graph pooling

Graph convolution, in its many forms, does not change the structure of the graph. That is, there are always the same number of nodes and edges. Instead, graph convolutional layers update the features within the graph through repeated computations.

It may be useful to build analogies to local max pooling in CNNs. We may talk about that in later lectures, but this is in general a complicated process. Instead, we’ll use global sum, average, or max pooling which aggregates the feature values over the entire graph

Putting it all together

Building a batch of data