Package: tf2-gnn
Graph Neural Networks in TF2Implementation and example training scripts of various flavours of graph neural network in
TensorFlow 2.0.
Much of it is based on the code in thetf-gnn-samplesrepo.The code is maintained by theAI4Science teamat Microsoft Research.
We arehiring.Currently this package is not under active developement, but it does continue to be used as a backend forour generative model of molecules.InstallationYou can install thetf2_gnnmodule from the Python Package Index usingpip install tf2_gnn.Alternatively (for example, for development), you can check out this repository,
navigate to it and runpip install -e ./to install it as a local editable package.You will then be able to use thetf2_gnn.layers.GNNclass and related utilities.This code was tested in Python 3.7, 3.8 and 3.9.Testing the InstallationTo test if all components are set up correctly, you can run a simple experiment on the
protein-protein interaction (PPI) task first described byZitnik & Leskovec, 2017.
You can download the data for this task fromhttps://data.dgl.ai/dataset/ppi.zipand unzip it into a local directory (e.g.,data/ppi).
Then, you can use the convenience utilitytf2_gnn_train(see--helpfor a description
of options) to train a Relational Graph Convoluational Network model as follows:$ tf2_gnn_train RGCN PPI --max-epochs 10 data/ppi/
Setting random seed 0.
Trying to load task/model-specific default parameters from /dpuhome/files/users/mabrocks/Projects/TF2-GNN/tf2_gnn/cli_utils/default_hypers/PPI_RGCN.json ... File found.
 Dataset default parameters: {'max_nodes_per_batch': 10000, 'add_self_loop_edges': True, 'tie_fwd_bkwd_edges': False}
Loading data from data/ppi/.
 Loading PPI train data from data/ppi/.
 Loading PPI valid data from data/ppi/.
[...]
Dataset parameters: {"max_nodes_per_batch": 8000, "add_self_loop_edges": true, "tie_fwd_bkwd_edges": false}
Model parameters: {"gnn_aggregation_function": "sum", "gnn_message_activation_function": "ReLU", "gnn_hidden_dim": 320, "gnn_use_target_state_as_input": false, "gnn_normalize_by_num_incoming": true, "gnn_num_edge_MLP_hidden_layers": 0, "gnn_message_calculation_class": "RGCN", "gnn_initial_node_representation_activation": "tanh", "gnn_dense_intermediate_layer_activation": "tanh", "gnn_num_layers": 4, "gnn_dense_every_num_layers": 10000, "gnn_residual_every_num_layers": 10000, "gnn_use_inter_layer_layernorm": false, "gnn_layer_input_dropout_rate": 0.1, "gnn_global_exchange_mode": "gru", "gnn_global_exchange_every_num_layers": 10000, "gnn_global_exchange_weighting_fun": "softmax", "gnn_global_exchange_num_heads": 4, "gnn_global_exchange_dropout_rate": 0.2, "optimizer": "Adam", "learning_rate": 0.001, "learning_rate_decay": 0.98, "momentum": 0.85, "gradient_clip_value": 1.0}
Initial valid metric: Avg MicroF1: 0.368.
   (Stored model metadata to trained_model/RGCN_PPI__2020-02-25_11-10-38_best.pkl and weights to trained_model/RGCN_PPI__2020-02-25_11-10-38_best.hdf5)
== Epoch 1
 Train:  25.6870 loss | Avg MicroF1: 0.401 | 2.63 graphs/s
 Valid:  33.1668 loss | Avg MicroF1: 0.419 | 4.01 graphs/s
  (Best epoch so far, target metric decreased to -0.41886 from -0.36762.)
   (Stored model metadata to trained_model/RGCN_PPI__2020-02-25_11-10-38_best.pkl and weights to trained_model/RGCN_PPI__2020-02-25_11-10-38_best.hdf5)
[...]After training finished,tf2_gnn_test trained_model/RGCN_PPI__2020-02-25_11-10-38_best.pkl data/ppican be used to test the trained model.Code StructureLayersThe core functionality of the library is implemented as TensorFlow 2 (Keras) layers,
enabling easy integration into other code.tf2_gnn.layers.GNNThis implements a deep Graph Neural Network, stacking several layers of message passing.
On construction, a dictionary of hyperparameters needs to be provided (default
values can be obtained fromGNN.get_default_hyperparameters()).
These hyperparameters configure the exact stack of GNN layers:"num_layers"sets the number of GNN message passing layers (usually, a number
between 2 and 16)"message_calculation_class"configures the message passing style.
This chooses thetf2_gnn.layers.message_passing.*layer used in each step.We currently support the following:GGNN: Gated Graph Neural Networks (Li et al., 2015).RGCN: Relational Graph Convolutional Networks (Schlichtkrull et al., 2017).RGAT: Relational Graph Attention Networks (Veličković et al., 2018).RGIN: Relational Graph Isomorphism Networks (Xu et al., 2019).GNN-Edge-MLP: Graph Neural Network with Edge MLPs - a variant of RGCN in which messages on edges are computed using full MLPs, not just a single layer applied to the source state.GNN-FiLM: Graph Neural Networks with Feature-wise Linear Modulation (Brockschmidt, 2019) - a new extension of RGCN with FiLM layers.Some of these expose additional hyperparameters; refer to their implementation for
details."hidden_dim"sets the size of the output of all message passing layers."layer_input_dropout_rate"sets the dropout rate (during training) for the
input of each message passing layer."residual_every_num_layers"sets how often a residual connection is inserted
between message passing layers. Concretely, a value ofkmeans that every layerlthat is a multiple ofk(and only those!) will not receive the outputs of
layerl-1as input, but instead the mean of the outputs of layersl-1andl-k."use_inter_layer_layernorm"is a boolean flag indicating ifLayerNormshould be
used between different message passing layers."dense_every_num_layers"configures how often a per-node representation dense layer
is inserted between the message passing layers.
Setting this to a large value (greather than"num_layers") means that no dense
layers are inserted at all."dense_intermediate_layer_activation"configures the activation function used after
the dense layer; the default of"tanh"can help stabilise training of very deep
GNNs."global_exchange_every_num_layers"configures how often a graph-level exchange of
information is performed.
For this, a graph level representation (seetf2_gnn.layers.NodesToGraphRepresentationbelow) is computed and then used to update the representation of each node.
The style of this update is configured by"global_exchange_mode", offering three
modes:"mean", which just computes the arithmetic mean of the node and graph-level
representation."mlp", which computes a new representation using an MLP that gets the
concatenation of node and graph level representations as input."gru", which uses a GRU cell that gets the old node representation as state
and the graph representation as input.TheGNNlayer takes aGNNInputnamed tuple as input, which encapsulates initial
node features, adjacency lists, and auxiliary information.
The easiest way to construct such a tuple is to use the provideddatasetclasses in combination with the providedmodel.tf2_gnn.layers.NodesToGraphRepresentationThis implements the task of computing a graph-level representation given node-level
representations (e.g., obtained by theGNNlayer).Currently, this is only implemented by theWeightedSumGraphRepresentationlayer,
which produces a graph representation by a multi-headed weighted sum of (transformed)
node representations, configured by the following hyperparameters set in the
layer constructor:graph_representation_sizesets the size of the computed representation.
By setting this to1, this layer can be used to directly implement graph-level
regression tasks.num_headsconfigures the number of parallel (independent) weighted sums that
are computed, whose results are concatenated to obtain the final result.
Note that this means that thegraph_representation_sizeneeds to be a multiple
of thenum_headsvalue.weighting_funcan take two values:"sigmoid"computes a weight for each node independently by first computing
a per-node score, which is then squashed through a sigmoid.
This is appropriate for tasks that are related to counting occurrences of a
feature in a graph, where the node weight is used to ignore certain nodes."softmax"computes weights for all graph nodes together by first computing
per-node scores, and then performing a softmax over all scores.
This is appropriate for tasks that require identifying important parts of
the graph.scoring_mlp_layers,scoring_mlp_activation_fun,scoring_mlp_dropout_rateconfigure the MLP that computes the per-node scores.transformation_mlp_layers,transformation_mlp_activation_fun,transformation_mlp_dropout_rateconfigure the MLP that computes the
transformed node representations that are summed up.DatasetsWe use a sparse representation of graphs, which requires a complex batching strategy
in which the graphs making up a minibatch are joined into a single graph of many
disconnected components.
The extensibletf2_gnn.data.GraphDatasetclass implements this procedure, and can
be subclassed to handle task-specific datasets and additional properties.
It exposes aget_tensorflow_datasetmethod that can be used to obtain atf.data.Datasetthat can be used in training/evaluation loops.We currently provide three implementations of this:tf2_gnn.data.PPIDatasetimplements reading the protein-protein interaction (PPI)
data first used byZitnik & Leskovec, 2017.tf2_gnn.data.QM9Datasetimplements reading the quantum chemistry data first
used byRamakrishnan et al., 2014.tf2_gnn.data.JsonLGraphPropertyDatasetimplements reading a generic dataset
made up of graphs with a single property, stored in JSONLines format:Files "train.jsonl.gz", "valid.jsonl.gz" and "test.jsonl.gz" are expected to
store the train/valid/test datasets.Each of the files is gzipped text file in which each line is a valid
JSON dictionary witha"graph"key, which in turn points to a dictionary with keys"node_features"(list of numerical initial node labels),"adjacency_lists"(list of list of directed edge pairs),a"Property"key having a a single floating point value.ModelsWe provide some built-in models intf2_gnn.models, which can either be directly
re-used or serve as inspiration for other models:tf2_gnn.models.GraphRegressionTaskimplements a graph-level regression model,
for example to make molecule-level predictions such as in the QM9 task.tf2_gnn.models.GraphBinaryClassificationTaskimplements a binary classification
model.tf2_gnn.models.NodeMulticlassTaskimplements a node-level multiclass classification
model, suitable to implement the PPI task.TasksTasks are a combination of datasets, models and specific hyperparameter settings.
These can be registered (and then used by name) using the utilities intf2_gnn.utils.task_utils(where a few default tasks are defined as well) and then
used in tools such astf2_gnn_train.AuthorsHenry Jackson-FluxMarc BrockschmidtMegan StanleyPashmina CameronKrzysztof MaziarzReferencesBrockschmidt, 2019Marc Brockschmidt. GNN-FiLM: Graph Neural Networks with Feature-wise Linear
Modulation. (https://arxiv.org/abs/1906.12192)Li et al., 2015Yujia Li, Daniel Tarlow, Marc Brockschmidt, and Richard Zemel. Gated Graph
Sequence Neural Networks. In International Conference on Learning
Representations (ICLR), 2016. (https://arxiv.org/pdf/1511.05493.pdf)Ramakrishnan et al., 2014Raghunathan Ramakrishnan, Pavlo O. Dral, Matthias Rupp, and O. Anatole
Von Lilienfeld. Quantum Chemistry Structures and Properties of 134 Kilo
Molecules. Scientific Data, 1, 2014.
(https://www.nature.com/articles/sdata201422/)Schlichtkrull et al., 2017Michael Schlichtkrull, Thomas N. Kipf, Peter Bloem, Rianne van den Berg,
Ivan Titov, and Max Welling. Modeling Relational Data with Graph
Convolutional Networks. In Extended Semantic Web Conference (ESWC), 2018.
(https://arxiv.org/pdf/1703.06103.pdf)Veličković et al. 2018Petar Veličković, Guillem Cucurull, Arantxa Casanova, Adriana Romero, Pietro
Liò, and Yoshua Bengio. Graph Attention Networks. In International Conference
on Learning Representations (ICLR), 2018. (https://arxiv.org/pdf/1710.10903.pdf)Xu et al. 2019Keyulu Xu, Weihua Hu, Jure Leskovec, and Stefanie Jegelka. How Powerful are
Graph Neural Networks? In International Conference on Learning Representations
(ICLR), 2019. (https://arxiv.org/pdf/1810.00826.pdf)Zitnik & Leskovec, 2017Marinka Zitnik and Jure Leskovec. Predicting Multicellular Function Through
Multi-layer Tissue Networks. Bioinformatics, 33, 2017.
(https://arxiv.org/abs/1707.04638)ContributingThis project welcomes contributions and suggestions.  Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visithttps://cla.opensource.microsoft.com.When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct.
For more information see theCode of Conduct FAQor
contactopencode@microsoft.comwith any additional questions or comments.