Skip to main content

General Base Layers for Graph Convolutions with tensorflow.keras

Project description

GitHub release (latest by date) Documentation Status PyPI version PyPI - Downloads kgcnn_unit_tests DOI GitHub

Keras Graph Convolutions

A set of layers for graph convolutions in TensorFlow Keras that use RaggedTensors.

Table of Contents

General

The package in kgcnn contains several layer classes to build up graph convolution models. Some models are given as an example. A documentation is generated in docs. This repo is still under construction. Any comments, suggestions or help are very welcome!

Requirements

For kgcnn, usually the latest version of tensorflow is required, but is listed as extra requirements in the setup.py for simplicity. Additional python packages are placed in the setup.py requirements and are installed automatically.

  • tensorflow>=2.4.1
  • rdkit>=2020.03.4

Installation

Clone repository https://github.com/aimat-lab/gcnn_keras and install with editable mode:

pip install -e ./gcnn_keras

or latest release via Python Package Index.

pip install kgcnn

Documentation

Auto-documentation is generated at https://kgcnn.readthedocs.io/en/latest/index.html .

Implementation details

Representation

The most frequent usage for graph convolutions is either node or graph classification. As for their size, either a single large graph, e.g. citation network or small (batched) graphs like molecules have to be considered. Graphs can be represented by an index list of connections plus feature information. Typical quantities in tensor format to describe a graph are listed below.

  • nodes: Node-list of shape (batch, N, F) where N is the number of nodes and F is the node feature dimension.
  • edges: Edge-list of shape (batch, M, F) where M is the number of edges and F is the edge feature dimension.
  • indices: Connection-list of shape (batch, M, 2) where M is the number of edges. The indices denote a connection of incoming i and outgoing j node as (i, j).
  • state: Graph state information of shape (batch, F) where F denotes the feature dimension.

A major issue for graphs is their flexible size and shape, when using mini-batches. Here, for a graph implementation in the spirit of keras, the batch dimension should be kept also in between layers. This is realized by using RaggedTensors.

Input

Here, for ragged tensors, the nodelist of shape (batch, None, F) and edgelist of shape (batch, None, F') have one ragged dimension (None, ). The graph structure is represented by an index-list of shape (batch, None, 2) with index of incoming i and outgoing j node as (i, j). The first index of incoming node i is usually expected to be sorted for faster pooling operations, but can also be unsorted (see layer arguments). Furthermore, the graph is directed, so an additional edge with (j, i) is required for undirected graphs. A ragged constant can be directly obtained from a list of numpy arrays: tf.ragged.constant(indices, ragged_rank=1, inner_shape=(2, )) which yields shape (batch, None, 2).

Model

Models can be set up in a functional way. Example message passing from fundamental operations:

import tensorflow.keras as ks
from kgcnn.layers.gather import GatherNodes
from kgcnn.layers.keras import Dense, Concatenate  # ragged support
from kgcnn.layers.pool.pooling import PoolingLocalMessages, PoolingNodes

n = ks.layers.Input(shape=(None, 3), name='node_input', dtype="float32", ragged=True)
ei = ks.layers.Input(shape=(None, 2), name='edge_index_input', dtype="int64", ragged=True)

n_in_out = GatherNodes()([n, ei])
node_messages = Dense(10, activation='relu')(n_in_out)
node_updates = PoolingLocalMessages()([n, node_messages, ei])
n_node_updates = Concatenate(axis=-1)([n, node_updates])
n_embedd = Dense(1)(n_node_updates)
g_embedd = PoolingNodes()(n_embedd)

message_passing = ks.models.Model(inputs=[n, ei], outputs=g_embedd)

or via sub-classing of the message passing base layer. Where only message_function and update_nodes must be implemented:

from kgcnn.layers.conv.message import MessagePassingBase
from kgcnn.layers.keras import Dense, Add

def MyMessageNN(MessagePassingBase):

    def __init__(self, units, **kwargs):
        super(MyMessageNN, self).__init__(**kwargs)
        self.dense = Dense(units)
        self.add = Add(axis=-1)

    def message_function(self, inputs, **kwargs):
        n_in, n_out, edges = inputs
        return self.dense(n_out)

    def update_nodes(self, inputs, **kwargs):
        nodes, nodes_update = inputs
        return self.add([nodes, nodes_update])

Literature

A version of the following models are implemented in literature:

Datasets

In data.datasets there are graph learning datasets. They are being downloaded from e.g. TUDatasets, MoleculeNet or defined freely using class definitions in data. For the simple case that the dataset fits in memory the base class is defined as:

class MemoryGraphDataset:

    def __init__(self):
        self.node_attributes = None
        self.node_labels = None

        self.edge_indices = None
        self.edge_attributes = None
        self.edge_labels = None

        self.graph_labels = None
        self.graph_attributes = None

or the extension to geometric information in addition to the graph's structure.

from kgcnn.data.base import MemoryGraphDataset

class MemoryGeometricGraphDataset(MemoryGraphDataset):

    def __init__(self, **kwargs):
        super(MemoryGeometricGraphDataset, self).__init__(**kwargs)
        self.node_coordinates = None

        self.range_indices = None
        self.range_attributes = None
        self.range_labels = None

        self.angle_indices = None
        self.angle_labels = None
        self.angle_attributes = None

Each property holds an iterable object (e.g. list, array) with the length of the dataset.

Examples

A set of example training can be found in training.

Issues

Some known issues to be aware of, if using and making new models or layers with kgcnn.

  • RaggedTensor can not yet be used as a keras model output (https://github.com/tensorflow/tensorflow/issues/42320), which means only padded tensors can be used for batched node embedding tasks.
  • Using RaggedTensor's for arbitrary ragged rank apart from kgcnn.layers.keras can cause significant performance decrease.

Citing

If you want to cite this repo, refer to our paper:

@article{REISER2021100095,
title = {Graph neural networks in TensorFlow-Keras with RaggedTensor representation (kgcnn)},
journal = {Software Impacts},
pages = {100095},
year = {2021},
issn = {2665-9638},
doi = {https://doi.org/10.1016/j.simpa.2021.100095},
url = {https://www.sciencedirect.com/science/article/pii/S266596382100035X},
author = {Patrick Reiser and Andre Eberhard and Pascal Friederich}
}

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kgcnn-1.1.0.tar.gz (111.0 kB view hashes)

Uploaded Source

Built Distribution

kgcnn-1.1.0-py3-none-any.whl (155.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page