Understanding the building blocks and design choices of graph neural networks.
This article is one of two Distill publications about graph neural networks.
Take a look at
A Gentle Introduction to Graph Neural Networks
Many systems and interactions - social networks, molecules, organizations, citations, physical models, transactions - can be represented quite naturally as graphs. How can we reason about and make predictions within these systems?
One idea is to look at tools that have worked well in other domains: neural networks have shown immense predictive power in a variety of learning tasks. However, neural networks have been traditionally used to operate on fixed-size and/or regular-structured inputs (such as sentences, images and video). This makes them unable to elegantly process graph-structured data.
Graph neural networks (GNNs) are a family of neural networks that can operate naturally on graph-structured data. By extracting and utilizing features from the underlying graph, GNNs can make more informed predictions about entities in these interactions, as compared to models that consider individual entities in isolation.
GNNs are not the only tools available to model graph-structured data:
graph kernels
In this article, we will illustrate the challenges of computing over graphs, describe the origin and design of graph neural networks, and explore the most popular GNN variants in recent times. Particularly, we will see that many of these variants are composed of similar building blocks.
First, let’s discuss some of the complications that graphs come with.
Graphs are extremely flexible mathematical models; but this means they lack consistent structure across instances.
Consider the task of predicting whether a given chemical molecule is toxic
Looking at a few examples, the following issues quickly become apparent:
Representing graphs in a format that can be computed over is non-trivial, and the final representation chosen often depends significantly on the actual problem.
Extending the point above: graphs often have no inherent ordering present amongst the nodes. Compare this to images, where every pixel is uniquely determined by its absolute position within the image!
As a result, we would like our algorithms to be node-order equivariant: they should not depend on the ordering of the nodes of the graph. If we permute the nodes in some way, the resulting representations of the nodes as computed by our algorithms should also be permuted in the same way.
Graphs can be really large! Think about social networks like Facebook and Twitter, which have over a billion users. Operating on data this large is not easy.
Luckily, most naturally occuring graphs are ‘sparse’: they tend to have their number of edges linear in their number of vertices. We will see that this allows the use of clever methods to efficiently compute representations of nodes within the graph. Further, the methods that we look at here will have significantly fewer parameters in comparison to the size of the graphs they operate on.
There are many useful problems that can be formulated over graphs:
A common precursor in solving many of these problems is node representation learning: learning to map individual nodes to fixed-size real-valued vectors (called ‘representations’ or ‘embeddings’).
In Learning GNN Parameters, we will see how the learnt embeddings can be used for these tasks.
Different GNN variants are distinguished by the way these representations are computed. Generally, however, GNNs compute node representations in an iterative process. We will use the notation to indicate the representation of node after the iteration. Each iteration can be thought of as the equivalent of a ‘layer’ in standard neural networks.
We will define a graph as a set of nodes, , with a set of edges connecting them. Nodes can have individual features as part of the input: we will denote by the individual feature for node . For example, the ‘node features’ for a pixel in a color image would be the red, green and blue channel (RGB) values at that pixel.
For ease of exposition, we will assume is undirected, and all nodes are of the same type.
Sometimes we will need to denote a graph property by a matrix , where each row represents a property corresponding to a particular vertex .
Convolutional Neural Networks have been seen to be quite powerful in extracting features from images. However, images themselves can be seen as graphs with a very regular grid-like structure, where the individual pixels are nodes, and the RGB channel values at each pixel as the node features.
A natural idea, then, is to consider generalizing convolutions to arbitrary graphs. Recall, however, the challenges
listed out in the previous section: in particular, ordinary convolutions are not node-order invariant, because
they depend on the absolute positions of pixels.
It is initially unclear as how to generalize convolutions over grids to convolutions over general graphs,
where the neighbourhood structure differs from node to node.
We begin by introducing the idea of constructing polynomial filters over node neighbourhoods, much like how CNNs compute localized filters over neighbouring pixels. Then, we will see how more recent approaches extend on this idea with more powerful mechanisms. Finally, we will discuss alternative methods that can use ‘global’ graph-level information for computing node representations.
Given a graph , let us fix an arbitrary ordering of the nodes of . We denote the adjacency matrix of by , we can construct the diagonal degree matrix of as:
where denotes the entry in the row corresponding to and the column corresponding to in the matrix . We will use this notation throughout this section.
Then, the graph Laplacian is the square matrix defined as:
The graph Laplacian gets its name from being the discrete analog of the Laplacian operator from calculus.
Although it encodes precisely the same information as the adjacency matrix
Now that we have understood what the graph Laplacian is,
we can build polynomials
These polynomials can be thought of as the equivalent of ‘filters’ in CNNs, and the coefficients as the weights of the ‘filters’.
For ease of exposition, we will focus on the case where nodes have one-dimensional features: each of the for is just a real number. The same ideas hold when each of the are higher-dimensional vectors, as well.
Using the previously chosen ordering of the nodes, we can stack all of the node features to get a vector .
Once we have constructed the feature vector ,
we can define its convolution with a polynomial filter as:
To understand how the coefficients affect the convolution,
let us begin by considering the ‘simplest’ polynomial:
when and all of the other coefficients are .
In this case, is just :
Now, if we increase the degree, and consider the case where
instead and and all of the other coefficients are .
Then, is just , and so:
We see that the features at each node are combined
with the features of its immediate neighbours .
At this point, a natural question to ask is:
How does the degree of the polynomial influence the behaviour of the convolution?
Indeed, it is not too hard to show that:
Effectively, the convolution at node occurs only with nodes which are not more than hops away. Thus, these polynomial filters are localized. The degree of the localization is governed completely by .
To help you understand these ‘polynomial-based’ convolutions better, we have created the visualization below. Vary the polynomial coefficients and the input grid to see how the result of the convolution changes. The grid under the arrow shows the equivalent convolutional kernel applied at the highlighted pixel in to get the resulting pixel in . The kernel corresponds to the row of for the highlighted pixel. Note that even after adjusting for position, this kernel is different for different pixels, depending on their position within the grid.
Hover over a pixel in the input grid (left, representing ) to highlight it and see the equivalent convolutional kernel for that pixel under the arrow. The result of the convolution is shown on the right: note that different convolutional kernels are applied at different pixels, depending on their location.
Click on the input grid to toggle pixel values between (white) and (blue). To randomize the input grid, press ‘Randomize Grid’. To reset all pixels to , press ‘Reset Grid’. Use the sliders at the bottom to change the coefficients . To reset all coefficients to , press ‘Reset Coefficients.’
What is the motivation behind these choices?
The polynomial filters we considered here are actually independent of the ordering of the nodes. This is particularly easy to see when the degree of the polynomial is : where each node’s feature is aggregated with the sum of its neighbour’s features. Clearly, this sum does not depend on the order of the neighbours. A similar proof follows for higher degree polynomials: the entries in the powers of are equivariant to the ordering of the nodes.
As above, let’s assume an arbitrary node-order over the nodes of our graph. Any other node-order can be thought of as a permutation of this original node-order. We can represent any permutation by a permutation matrix . will always be an orthogonal matrix: Then, we call a function node-order equivariant iff for all permutations : When switching to the new node-order using the permutation , the quantities below transform in the following way: and so, for the case of polynomial filters where , we can see that: as claimed.
We now describe how we can build a graph neural network by stacking ChebNet (or any polynomial filter) layers one after the other with non-linearities, much like a standard CNN. In particular, if we have different polynomial filter layers, the of which has its own learnable weights , we would perform the following computation:
Note that these networks reuse the same filter weights across different nodes, exactly mimicking weight-sharing in Convolutional Neural Networks (CNNs) which reuse weights for convolutional filters across a grid.
ChebNet was a breakthrough in learning localized filters over graphs, and it motivated many to think of graph convolutions from a different perspective.
We return back to the result of convolving by the polynomial kernel , focussing on a particular vertex :As we noted before, this is a -hop localized convolution. But more importantly, we can think of this convolution as arising of two steps:
Key Idea: What if we consider different kinds of ‘aggregation’ and ‘combination’ steps, beyond what are possible using polynomial filters?
By ensuring that the aggregation is node-order equivariant, the overall convolution becomes node-order equivariant.
These convolutions can be thought of as ‘message-passing’ between adjacent nodes: after each step, every node receives some ‘information’ from its neighbours.
By iteratively repeating the -hop localized convolutions times (i.e., repeatedly ‘passing messages’), the receptive field of the convolution effectively includes all nodes upto hops away.
Message-passing forms the backbone of many GNN architectures today. We describe the most popular ones in depth below:
An interesting point is to assess different aggregation functions: are some better and others worse?
Here, we’ve talk about GNNs where the computation only occurs at the nodes.
More recent GNN models
such as Message-Passing Neural Networks
Below is an interactive visualization of these GNN models on small graphs. For clarity, the node features are just real numbers here, shown inside the squares next to each node, but the same equations hold when the node features are vectors.
In practice, each iteration above is generally thought of as a single ‘neural network layer’.
This ideology is followed by many popular Graph Neural Network libraries,
The methods we’ve seen so far perform ‘local’ convolutions: every node’s feature is updated using a function of its local neighbours’ features.
While performing enough steps of message-passing will eventually ensure that information from all nodes in the graph is passed, one may wonder if there are more direct ways to perform ‘global’ convolutions.
The answer is yes; we will now describe an approach that was actually first put forward
in the context of neural networks by
As before, we will focus on the case where nodes have one-dimensional features. After choosing an arbitrary node-order, we can stack all of the node features to get a ‘feature vector’ .
Key Idea: Given a feature vector , the Laplacian allows us to quantify how smooth is, with respect to .
How?
After normalizing such that ,
if we look at the following quantity involving :
is a real, symmetric matrix, which means it has all real eigenvalues .
As discussed before, we can consider any image as a grid graph, where each pixel is a node,
connected by edges to adjacent pixels.
Thus, a pixel can have either or neighbours, depending on its location within the image grid.
Each pixel gets a value as part of the image. If the image is grayscale, each value will be a single
real number indicating how dark the pixel is. If the image is colored, each value will be a -dimensional
vector, indicating the values for the red, green and blue (RGB) channels.
This construction allows us to compute the graph Laplacian and the eigenvector matrix . Given an image, we can then investigate what its spectral representation looks like.
To shed some light on what the spectral representation actually encodes, we perform the following experiment over each channel of the image independently:
Finally, we stack the resulting channels back together to get back an image. We can now see how the resulting image changes with choices of . Note that when , the resulting image is identical to the original image, as we can reconstruct each channel exactly.
As decreases, we see that the output image gets blurrier. If we decrease to , the output image is entirely the same color throughout. We see that we do not need to keep all components; we can retain a lot of the information in the image with significantly fewer components. We can relate this to the Fourier decomposition of images: the more eigenvectors we use, the higher frequencies we can represent on the grid.
To complement the visualization above, we additionally visualize the first few eigenvectors on a smaller grid below. We change the coefficients of the first out of eigenvectors in the spectral representation and see how the resulting image changes:
These visualizations should convince you that the first eigenvectors are indeed smooth, and the smoothness correspondingly decreases as we consider later eigenvectors.
For any image , we can think of the initial entries of the spectral representation as capturing ‘global’ image-wide trends, which are the low-frequency components, while the later entries as capturing ‘local’ details, which are the high-frequency components.
We now have the background to understand spectral convolutions and how they can be used to compute embeddings/feature representations of nodes.
As before, the model we describe below has layers: each layer has learnable parameters , called the ‘filter weights’. These weights will be convolved with the spectral representations of the node features. As a result, the number of weights needed in each layer is equal to , the number of eigenvectors used to compute the spectral representations. We had shown in the previous section that we can take and still not lose out on significant amounts of information.
Thus, convolution in the spectral domain enables the use of significantly fewer parameters than just direct convolution in the natural domain. Further, by virtue of the smoothness of the Laplacian eigenvectors across the graph, using spectral representations automatically enforces an inductive bias for neighbouring nodes to get similar representations.
Assuming one-dimensional node features for now, the output of each layer is a vector of node representations , where each node’s representation corresponds to a row of the vector.
We fix an ordering of the nodes in . This gives us the adjacency matrix and the graph Laplacian , allowing us to compute . Finally, we can describe the computation that the layers perform, one after the other:
The method above generalizes easily to the case where each , as well:
see
With the insights from the previous section, we see that convolution in the spectral-domain of graphs can be thought of as the generalization of convolution in the frequency-domain of images.
We can show spectral convolutions are node-order equivariant using a similar approach as for Laplacian polynomial filters.
As in our proof before,
let’s fix an arbitrary node-order.
Then, any other node-order can be represented by a
permutation of this original node-order.
We can associate this permutation with its permutation matrix .
Under this new node-order,
the quantities below transform in the following way:
which implies that, in the embedding computation:
Hence, as is applied elementwise:
as required.
Further, we see that the spectral quantities and
are unchanged by permutations of the nodes.
The theory of spectral convolutions is mathematically well-grounded; however, there are some key disadvantages that we must talk about:
While spectral convolutions have largely been superseded by
‘local’ convolutions for the reasons discussed above,
there is still much merit to understanding the ideas behind them.
Indeed, a recently proposed GNN model called Directional Graph Networks
A simpler way to incorporate graph-level information
is to compute embeddings of the entire graph by pooling node
(and possibly edge) embeddings,
and then using the graph embedding to update node embeddings,
following an iterative scheme similar to what we have looked at here.
This is an approach used by Graph Networks
All of the embedding computations we’ve described here, whether spectral or spatial, are completely differentiable. This allows GNNs to be trained in an end-to-end fashion, just like a standard neural network, once a suitable loss function is defined:
The broad success of pre-training for natural language processing models
such as ELMo
Another self-supervised technique is to enforce that neighbouring nodes get similar embeddings,
mimicking random-walk approaches such as node2vec
where is a multi-set of nodes visited when random walks are started from .
For large graphs, where computing the sum over all nodes may be computationally expensive,
techniques such as Noise Contrastive Estimation
While we have looked at many techniques and ideas in this article,
the field of Graph Neural Networks is extremely vast.
We have been forced to restrict our discussion to a small subset of the entire literature,
while still communicating the key ideas and design principles behind GNNs.
We recommend the interested reader take a look at
We end with pointers and references for additional concepts readers might be interested in:
It turns out that accomodating the different structures of graphs is often hard to do efficiently, but we can still represent many GNN update equations using as sparse matrix-vector products (since generally, the adjacency matrix is sparse for most real-world graph datasets.) For example, the GCN variant discussed here can be represented as: Restructuring the update equations in this way allows for efficient vectorized implementations of GNNs on accelerators such as GPUs.
Regularization techniques for standard neural networks,
such as Dropout
Here, we have focused on undirected graphs, to avoid going into too many unnecessary details. However, there are some simple variants of spatial convolutions for:
There do exist more sophisticated techniques that can take advantage of the different structures of these graphs:
see
This article discusses how GNNs compute useful representations of nodes. But what if we wanted to compute representations of graphs for graph-level tasks (for example, predicting the toxicity of a molecule)?
A simple solution is to just aggregate the final node embeddings and pass them through another neural network : However, there do exist more powerful techniques for ‘pooling’ together node representations:
The experiments from Spectral Representations of Natural Images can be reproduced using the following Colab notebook: Spectral Representations of Natural Images.
To aid in the creation of future interactive articles, we have created ObservableHQ notebooks for each of the interactive visualizations here:
We are deeply grateful to ObservableHQ, a wonderful platform for developing interactive visualizations. The static visualizations would not have been possible without Inkscape and Alexander Lenail’s Neural Network SVG Generator. The molecule diagrams depicted above were obtained and modified from Wikimedia Commons, available in the public domain.
We would like to acknowledge the following Distill articles for inspiration on article design:
We would like to thank Thomas Kipf for his valuable feedback on the technical content within this article.
We would like to thank David Nichols for creating Coloring for Colorblindness which helped us improve the accessibility of this article’s color scheme.
We would also like to acknowledge CS224W: Machine Learning with Graphs as an excellent reference from which the authors benefitted significantly.
Ashish Tendulkar from Google Research India provided significant feedback on the content within this article, helping its readability. He also helped with identifying the topics this article should cover, and with brainstorming the experiments here.
Adam Pearce from Google Research helped us immensely with article hosting and rendering.
Finally, we would like to thank Anirban Santara, Sujoy Paul and Ansh Khurana from Google Research India for their help with setting up and running experiments.
Ameya Daigavane drafted most of the text, designed experiments and created the interactive visualizations in this article. Balaraman Ravindran and Gaurav Aggarwal extensively guided the overall direction of the article, deliberated over the design and scope of experiments, provided much feedback on the interactive visualizations, edited the text, and described improvements to make the article more accessible to readers.
Review 1 - Chaitanya K. Joshi
Review 2 - Nick Moran
Review 3 - Anonymous
If you see mistakes or want to suggest changes, please create an issue on GitHub.
Diagrams and text are licensed under Creative Commons Attribution CC-BY 4.0 with the source available on GitHub, unless noted otherwise. The figures that have been reused from other sources don’t fall under this license and can be recognized by a note in their caption: “Figure from …”.
For attribution in academic contexts, please cite this work as
Daigavane, et al., "Understanding Convolutions on Graphs", Distill, 2021.
BibTeX citation
@article{daigavane2021understanding, author = {Daigavane, Ameya and Ravindran, Balaraman and Aggarwal, Gaurav}, title = {Understanding Convolutions on Graphs}, journal = {Distill}, year = {2021}, note = {https://distill.pub/2021/understanding-gnns}, doi = {10.23915/distill.00032} }