Implementing Graph Neural Networks With TensorFlow
Implementing Graph Neural Networks With TensorFlow
TensorFlow-Keras
1
Institute of Nanotechnology, Karlsruhe Institute of Technology (KIT), Hermann-von-Helmholtz-Platz 1, 76344
Eggenstein-Leopoldshafen,Germany
2
Institute of Theoretical Informatics, Karlsruhe Institute of Technology (KIT), Am Fasanengarten 5, 76131
Karlsruhe, Germany
Graph neural networks are a versatile machine learning architecture that received a lot of
attention recently. In this technical report, we present an implementation of convolution and
pooling layers for TensorFlow-Keras models, which allows a seamless and flexible
integration into standard Keras layers to set up graph models in a functional way. This
implies the usage of mini-batches as the first tensor dimension, which can be realized via the
new RaggedTensor class of TensorFlow best suited for graphs. We developed the Keras
Graph Convolutional Neural Network Python package kgcnn based on TensorFlow-Keras
that provides a set of Keras layers for graph networks which focus on a transparent tensor
structure passed between layers and an ease-of-use mindset.
In order to utilize the full scope of different graph A main issue with handling graphs is their flexible size,
operations for setting up a custom GNN model, a which is why graph data can not be easily arranged in
modular framework of convolution and pooling layers is tensors as it is done for example in image processing.
necessary. We briefly summarize and discuss existing Especially arranging smaller graphs of different size in
graph libraries and their code coverage. Then, a short mini-batches poses a problem with fixed sized tensors.
overview of representing graphs in tensor form is given. A way to circumvent this problem is to use
Finally, we introduce our graph package kgcnn for zero-padding with masking or composite tensors such
Tensorflow's 2.0 Keras API,33–35 which seamlessly as ragged or sparse tensors. Another possibility is to
integrates graph layers into the Keras36 environment. join small graphs into a single large graph, where the
individual subgraphs are not connected to each other,
which is illustrated in Figure 1 and is often referred to
Graph Libraries as disjoint representation. The tensors used to describe
a graph are typically given by a node list n of shape
Since graph neural networks require modified ([batch], N, F), a connection table of edge indices of
convolution and pooling operators, many Python incoming and outgoing node m with shape ([batch], M,
packages for deep learning have emerged for either 2) and a corresponding edge feature list e of shape
TensorFlow33,34 or PyTorch37 to work with graphs. We try ([batch], M, F). Here, N the number of nodes, F denotes
to summarize the most notable ones without any claim the dimension of the node representation and M the
that this list is complete. number of edges. A common representation of a
graph's structure is given by the adjacency matrix 𝐴 of
PyTorch Geometric.38 A PyTorch based graph library shape ([batch], N, N) which has 𝐴𝑖𝑗 = 1 if the graph has
which is probably the largest and most used graph
an edge between nodes i and j and 𝐴𝑖𝑗 = 0 otherwise.
learning Python package up to date. It implements a
huge variety of different graph models and uses a
disjoint graph representation to deal with batched
graphs (graph representations are discussed in the next
section).
Deep Graph Library (DGL).39 A graph model library with
a flexible backend and a performance optimized
implementation. It has its own graph data class with
Figure 1: Disjoint graph representation with adjacency matrix
many loading options. Moreover, variants such as A, node list n and connection table m. Edge features are
generative graph models,40 Capsule41 and added in form of a feature list e or a feature matrix E matching
42
transformers are included. A. The indices in m match the total graph as indicated by
Spektral.43 A Keras36 implementation of graph arrows. The subgraph distinction encoded by color has to be
convolutional networks. Originally restricted to spectral stored separately.
graph filters,17 it now includes spatial convolution and
pooling operations. The graph representation is made However, with RaggedTensors, node features and edge
flexible by different graph modes detected by each index lists can be passed to Keras models with a
layer. flexible tensor dimension that incorporates different
StellarGraph.44 A Keras36 implementation that numbers of nodes and edges. For example, a ragged
implements a set of convolution layers and a few node tensor of shape (batch, None, F) can
pooling layers plus a custom graph data format. accommodate a flexible graph size in the second
dimension. It is to note that even sparse matrices,
With PyTorch Geometric and DGL there are already which are commonly used to represent the adjacency
large graph libraries with a lot of contributors from both matrix in a disjoint representation, are internally stored
academics and industry. The focus of the graph as a value plus index tensor. This means that the
package presented here is on a neat integration of ragged tensor representation can be cast into a sparse
graphs into the TensorFlow-Keras framework in the or padded representation with little cost, if necessary.
most straightforward way. Thereby, we hope to provide TensorFlow 2.0 further supports limited sparse matrix
operations, which can be used for graph convolution seamless integration with other Keras models. We plan
models like GCN. to continue to extend the kgcnn library to incorporate
new models, in particular GNNExplainer48 and
Keras graph package - kgcnn DiffPool,23 and improve functionality.