IEEE Computational Intelligence Magazine - February 2021 - 101

Thanks to the increasing popularity
of GNNs, many software libraries
implementing the building blocks of
graph representation learning have been
developed in recent years, paving the
way for the adoption of GNNs in other
fields of science. One of the major challenges faced by researchers and software
developers who wish to contribute to
the larger scientific community is to
make software both accessible and intuitive, so that even non-technical audiences can benefit from the advances
carried by intelligent systems. In this
spirit, Keras is an application programming interface (API) for creating neural
-networks, developed according to the
guiding principle that " being able to go
from idea to result with the least possible delay is key to doing good research "
[33]. Keras is designed to reduce the
cognitive load of end users, shifting the
focus away from the boilerplate implementation details and allowing instead
to focus on the creation of models. As
such, Keras is extremely beginnerfriendly and, for many, an entry point to
machine learning itself. At the same
time, Keras integrates smoothly with its
TensorFlow [34] backend and enables
users to build any model that they
could have implemented in pure TensorFlow. This flexibility makes Keras an
excellent tool even for expert deep
learning practitioners and has recently
led to TensorFlow's adoption of Keras as
the official interface to the framework.
In this paper we present Spektral, a
Python library for building graph neural
networks using TensorFlow and the
Keras API. Spektral implements some of
the most important papers from the
GNN literature as Keras layers, and it
integrates seamlessly within Keras models and with the most important features
of Keras like the training loop, callbacks,
distributed training, and automatic support for GPUs and TPUs. As such, Spektral inherits the philosophy of ease of
use and flexibility that characterizes
Keras. The components of Spektral act as
standard TensorFlow operations and can
be easily used even in more advanced
settings, integrating tightly with all the
features of TensorFlow and allowing for

easy deployment to production systems.
For these reasons, Spektral is the ideal
library to implement GNNs in the TensorFlow ecosystem, both for total beginners and experts alike.
All features of Spektral are documented in detail1 and a collection of
examples is provided with the source
code. The project is released on GitHub2
under MIT license.
II. Library Overview
A. Representing Graphs

Let G = {X, E} be a graph where
X = {x i ! R F|i = 1, f, N} is the set of
nodes with F-dimensional real attributes,
and E = {e ij ! R S|x i, x j ! X} the set
of edges with S dimensional real attributes. In Spektral, we represent G by its
binary adjacency matrix A ! {0, 1} N # N ,
node features X ! R N # F , and edge features E ! R N # N # S . Any format accepted
by Keras to represent the above matrices
is also supported by Spektral, which
means that it also natively supports the
NumPy stack of scientific computing
libraries for Python. Most of the layers
and utilities implemented in Spektral also
support the sparse matrices of the SciPy
library, making them computationally
efficient both in time and memory.
B. Data Modes

Spektral makes very few assumptions on
how a user may want to represent
graphs and transparently deals with
batches of graphs represented as higherorder tensors or disjoint unions. The
library supports four different ways of
representing graphs (or batches thereof),
which we refer to as data modes.
In single mode, the data describes a
single graph with its adjacency matrix
and attributes, and inference usually happens at the level of individual nodes. Disjoint mode is a special case of single mode,
where the graph is obtained as the disjoint union of a set of smaller graphs. In
this case the node attributes of the
graphs are stacked in a single matrix and
their adjacency matrices are combined in

a block-diagonal matrix. This is a practical way of representing batches of variable-order graphs, although it requires an
additional data structure to keep track of
the different components. Alternatively,
in batch mode, a set of graphs is represented by stacking their adjacency and attributes matrices in higher-order tensors of
shape B # N # f. This mode is akin to
traditional batch processing in machine
learning and can be more naturally
adopted in deep learning architectures.
However, it requires the graphs in a
batch to have the same number of nodes.
Finally, mixed mode is the one most often
found in traditional machine learning literature and consists of a graph with fixed
support but variable attributes. Common
examples of this mode are found in
computer vision, where images have a
fixed 2-dimensional grid support and
only differ in the pixel values (i.e., the
node attributes), and in traditional graph
signal processing applications.
The utils module exposes some
useful utilities for manipulating the data
and converting between the different
data modes.
C. Message Passing

Message-passing networks are a general
paradigm introduced by Gilmer et al.
[19] that unifies most GNN methods
found in the literature as a combination
of message, aggregation, and update functions. Message-passing layers are equivalent in role to the convolutional operators
in convolutional neural networks, and
are the essential components of graph
representation learning. Message-passing
layers in Spektral are available in the

1

https://graphneural.network
https://github.com/danielegrattarola/spektral

2

FIGURE 3 The stylised ghost logo of Spektral.

FEBRUARY 2021 | IEEE COMPUTATIONAL INTELLIGENCE MAGAZINE

101


https://graphneural.network https://github.com/danielegrattarola/spektral

IEEE Computational Intelligence Magazine - February 2021

Table of Contents for the Digital Edition of IEEE Computational Intelligence Magazine - February 2021

IEEE Computational Intelligence Magazine - February 2021 - Cover1
IEEE Computational Intelligence Magazine - February 2021 - Cover2
IEEE Computational Intelligence Magazine - February 2021 - 1
IEEE Computational Intelligence Magazine - February 2021 - 2
IEEE Computational Intelligence Magazine - February 2021 - 3
IEEE Computational Intelligence Magazine - February 2021 - 4
IEEE Computational Intelligence Magazine - February 2021 - 5
IEEE Computational Intelligence Magazine - February 2021 - 6
IEEE Computational Intelligence Magazine - February 2021 - 7
IEEE Computational Intelligence Magazine - February 2021 - 8
IEEE Computational Intelligence Magazine - February 2021 - 9
IEEE Computational Intelligence Magazine - February 2021 - 10
IEEE Computational Intelligence Magazine - February 2021 - 11
IEEE Computational Intelligence Magazine - February 2021 - 12
IEEE Computational Intelligence Magazine - February 2021 - 13
IEEE Computational Intelligence Magazine - February 2021 - 14
IEEE Computational Intelligence Magazine - February 2021 - 15
IEEE Computational Intelligence Magazine - February 2021 - 16
IEEE Computational Intelligence Magazine - February 2021 - 17
IEEE Computational Intelligence Magazine - February 2021 - 18
IEEE Computational Intelligence Magazine - February 2021 - 19
IEEE Computational Intelligence Magazine - February 2021 - 20
IEEE Computational Intelligence Magazine - February 2021 - 21
IEEE Computational Intelligence Magazine - February 2021 - 22
IEEE Computational Intelligence Magazine - February 2021 - 23
IEEE Computational Intelligence Magazine - February 2021 - 24
IEEE Computational Intelligence Magazine - February 2021 - 25
IEEE Computational Intelligence Magazine - February 2021 - 26
IEEE Computational Intelligence Magazine - February 2021 - 27
IEEE Computational Intelligence Magazine - February 2021 - 28
IEEE Computational Intelligence Magazine - February 2021 - 29
IEEE Computational Intelligence Magazine - February 2021 - 30
IEEE Computational Intelligence Magazine - February 2021 - 31
IEEE Computational Intelligence Magazine - February 2021 - 32
IEEE Computational Intelligence Magazine - February 2021 - 33
IEEE Computational Intelligence Magazine - February 2021 - 34
IEEE Computational Intelligence Magazine - February 2021 - 35
IEEE Computational Intelligence Magazine - February 2021 - 36
IEEE Computational Intelligence Magazine - February 2021 - 37
IEEE Computational Intelligence Magazine - February 2021 - 38
IEEE Computational Intelligence Magazine - February 2021 - 39
IEEE Computational Intelligence Magazine - February 2021 - 40
IEEE Computational Intelligence Magazine - February 2021 - 41
IEEE Computational Intelligence Magazine - February 2021 - 42
IEEE Computational Intelligence Magazine - February 2021 - 43
IEEE Computational Intelligence Magazine - February 2021 - 44
IEEE Computational Intelligence Magazine - February 2021 - 45
IEEE Computational Intelligence Magazine - February 2021 - 46
IEEE Computational Intelligence Magazine - February 2021 - 47
IEEE Computational Intelligence Magazine - February 2021 - 48
IEEE Computational Intelligence Magazine - February 2021 - 49
IEEE Computational Intelligence Magazine - February 2021 - 50
IEEE Computational Intelligence Magazine - February 2021 - 51
IEEE Computational Intelligence Magazine - February 2021 - 52
IEEE Computational Intelligence Magazine - February 2021 - 53
IEEE Computational Intelligence Magazine - February 2021 - 54
IEEE Computational Intelligence Magazine - February 2021 - 55
IEEE Computational Intelligence Magazine - February 2021 - 56
IEEE Computational Intelligence Magazine - February 2021 - 57
IEEE Computational Intelligence Magazine - February 2021 - 58
IEEE Computational Intelligence Magazine - February 2021 - 59
IEEE Computational Intelligence Magazine - February 2021 - 60
IEEE Computational Intelligence Magazine - February 2021 - 61
IEEE Computational Intelligence Magazine - February 2021 - 62
IEEE Computational Intelligence Magazine - February 2021 - 63
IEEE Computational Intelligence Magazine - February 2021 - 64
IEEE Computational Intelligence Magazine - February 2021 - 65
IEEE Computational Intelligence Magazine - February 2021 - 66
IEEE Computational Intelligence Magazine - February 2021 - 67
IEEE Computational Intelligence Magazine - February 2021 - 68
IEEE Computational Intelligence Magazine - February 2021 - 69
IEEE Computational Intelligence Magazine - February 2021 - 70
IEEE Computational Intelligence Magazine - February 2021 - 71
IEEE Computational Intelligence Magazine - February 2021 - 72
IEEE Computational Intelligence Magazine - February 2021 - 73
IEEE Computational Intelligence Magazine - February 2021 - 74
IEEE Computational Intelligence Magazine - February 2021 - 75
IEEE Computational Intelligence Magazine - February 2021 - 76
IEEE Computational Intelligence Magazine - February 2021 - 77
IEEE Computational Intelligence Magazine - February 2021 - 78
IEEE Computational Intelligence Magazine - February 2021 - 79
IEEE Computational Intelligence Magazine - February 2021 - 80
IEEE Computational Intelligence Magazine - February 2021 - 81
IEEE Computational Intelligence Magazine - February 2021 - 82
IEEE Computational Intelligence Magazine - February 2021 - 83
IEEE Computational Intelligence Magazine - February 2021 - 84
IEEE Computational Intelligence Magazine - February 2021 - 85
IEEE Computational Intelligence Magazine - February 2021 - 86
IEEE Computational Intelligence Magazine - February 2021 - 87
IEEE Computational Intelligence Magazine - February 2021 - 88
IEEE Computational Intelligence Magazine - February 2021 - 89
IEEE Computational Intelligence Magazine - February 2021 - 90
IEEE Computational Intelligence Magazine - February 2021 - 91
IEEE Computational Intelligence Magazine - February 2021 - 92
IEEE Computational Intelligence Magazine - February 2021 - 93
IEEE Computational Intelligence Magazine - February 2021 - 94
IEEE Computational Intelligence Magazine - February 2021 - 95
IEEE Computational Intelligence Magazine - February 2021 - 96
IEEE Computational Intelligence Magazine - February 2021 - 97
IEEE Computational Intelligence Magazine - February 2021 - 98
IEEE Computational Intelligence Magazine - February 2021 - 99
IEEE Computational Intelligence Magazine - February 2021 - 100
IEEE Computational Intelligence Magazine - February 2021 - 101
IEEE Computational Intelligence Magazine - February 2021 - 102
IEEE Computational Intelligence Magazine - February 2021 - 103
IEEE Computational Intelligence Magazine - February 2021 - 104
IEEE Computational Intelligence Magazine - February 2021 - 105
IEEE Computational Intelligence Magazine - February 2021 - 106
IEEE Computational Intelligence Magazine - February 2021 - 107
IEEE Computational Intelligence Magazine - February 2021 - 108
IEEE Computational Intelligence Magazine - February 2021 - Cover3
IEEE Computational Intelligence Magazine - February 2021 - Cover4
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202311
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202308
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202305
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202302
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202211
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202208
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202205
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202202
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202111
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202108
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202105
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202102
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202011
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202008
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202005
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202002
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201911
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201908
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201905
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201902
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201811
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201808
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201805
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201802
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter12
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall12
https://www.nxtbookmedia.com