Skip to main content

A hackable TensorFlow GraphNets library

Project description

TensorFlow compatibility and test status

Tests Coverage

TensorFlow TensorFlow Probability Status
2.17.x 0.24.x TF 2.17 TFP 0.24
2.18.x 0.25.x TF 2.18 TFP 0.25
2.19.x 0.25.x TF 2.19 TFP 0.25
2.20.x 0.25.x TF 2.20 TFP 0.25
2.21.x 0.25.x TF 2.21 TFP 0.25

The matrix above is validated by scripts/run_tf_matrix_tests.sh and in CI (.github/workflows/tests.yml).

tf_gnns - A Hackable GraphNets library

alt-img A library for easy construction of message-passing networks in TensorFlow Keras. It is largely inspired by this DeepMind paper and the corresponding open-source library (original graph_nets library).

The tf_gnns library has no external dependencies except TensorFlow 2.x (there is no support for TF 1.x graph/session-based computation). It implements alternative design constraints from graph_nets, taking advantage of Keras facilities to build complex models easily and without large drops in performance.

tf_gnns is built to support arbitrary node/edge/global attributes and update functions. A set of utility functions for MLP construction with Keras is also provided (i.e., handling input/output sizes for valid networks), replacing Sonnet.

The main motivation for this library was the absence of a relatively short and efficient implementation of GNNs explicitly created to take advantage of Keras functionality. GNN implementations that take advantage of tensorflow_probability functionality are planned for future releases (such as the one used in [2]).

Installing tf_gnns


NOTE

The current tested matrix is TensorFlow 2.17 through 2.21 with the matching TensorFlow Probability versions shown above.


Install with uv (recommended):

uv sync

Or install with pip:

# optional - recommended:
# pip install tensorflow==2.15 
# pip install tensorflow_probability==0.22
pip install tf_gnns

Run tests:

uv sync --group dev
uv run pytest -v

Run tests with coverage and update badge payload:

scripts/run_coverage.sh

Run compatibility tests across TensorFlow versions:

scripts/run_tf_matrix_tests.sh 2.17 2.18 2.19 2.20 2.21

Execution and compilation

tf_gnns execution paths are eager by default so they can remain backend-portable with Keras 3. If you are using the TensorFlow backend and want graph compilation, compile at the application level:

import tensorflow as tf
from tf_gnns.models.graphnet import GraphNetMLP

model = GraphNetMLP(units=32, core_steps=2)

@tf.function
def train_step(graph_tensor_dict):
    with tf.GradientTape() as tape:
        out = model(graph_tensor_dict)
        loss = tf.reduce_mean(out["nodes"])  # example loss
    grads = tape.gradient(loss, model.trainable_variables)
    return loss, grads

This keeps library internals backend-agnostic while still allowing TensorFlow users to optimize execution.

Torch backend note

For Keras 3 + Torch backend with Triton enabled, this repository is currently tested with:

  • torch==2.11.0
  • triton==3.6.0 (installed as a dependency of torch 2.11.0)

Recommended setup:

pip install "torch==2.11.0"
KERAS_BACKEND=torch pytest -q tests

If you are using a different Torch/Triton combo and hit import-time crashes in triton / torch._dynamo, pinning to the combination above is the first step.

Build the Docker test image for a specific TensorFlow version:

docker build --build-arg TENSORFLOW_VERSION=2.17 -t tf-gnns:test .

Use through Docker

You can build a Docker image that uses tf_gnns with the following command, based on Ubuntu 22:

docker build . -t tf_gnns_215 --network host  --build-arg TENSORFLOW_VERSION=2.15

The container implements some logic to sort out the necessary dependencies. Namely,

  • Numpy 1.x is required for tf <= 2.14
  • Keras 2 support needs to be enabled for tf >= 2.16
  • The tensorflow_probability version is selected through a mapping given the tensorflow version.

Examples

tf_gnns basics

You can inspect some basic functionality in the following Colab notebook:

Open In Colab

List sorting example

(Example from the original deepmind/graph_nets library) If you are familiar with the original graph_nets library, this example will help you understand how you can transition to tf_gnns.

Sort a list of elements. This notebook and the accompanying code demonstrates how to use the Graph Nets library to learn to sort a list of elements.

A list of elements is treated as a fully connected graph between the elements. The network is trained to label the start node, and which (directed) edges correspond to the links to the next largest element, for each node.

After training, prediction ability is tested by comparing output to true sorted lists. Then the network's ability to generalize is tested by using it to sort larger lists.

Open In Colab

Protein-Protein Interaction example

This example shows how to adapt torch_geometric (aka PyG) inputs to tf_gnns inputs. The notebook can be run end-to-end in Google Colab, and out of the box it gives a test-set F1 score that is competitive with SOTA. Open In Colab

Keras 3 + Torch backend example

This example demonstrates using the higher-level GraphNet constructs with Keras 3 configured for the PyTorch backend. Open In Colab

Performance

From initial tests, the performance of tf_gnns seems to be at least as good as deepmind/graph_nets when using tensor dictionaries.

Publications using tf_gnns

The library has been used so far in the following publications:

[1] Bayesian graph neural networks for strain-based crack localization

[2] Remaining Useful Life Estimation Under Uncertainty with Causal GraphNets

[3] Relational VAE: A Continuous Latent Variable Model for Graph Structured Data

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

tf_gnns-0.2.0.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tf_gnns-0.2.0-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

Details for the file tf_gnns-0.2.0.tar.gz.

File metadata

  • Download URL: tf_gnns-0.2.0.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tf_gnns-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b5e2318551467089bddfb631251b8a1d66e5b742c01654c1924bec7657cc7a0b
MD5 33f69f8836e29f938aad1f7b872fd305
BLAKE2b-256 ede30dd98dbb8051229af758ad407e9a9c474bf7de755aca2a88de43014d83df

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_gnns-0.2.0.tar.gz:

Publisher: release-pypi.yml on mylonasc/tf_gnns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_gnns-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tf_gnns-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tf_gnns-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f77e9e6c002e6334c52c8ce2f3177df316d7ed9dee0ec0761a7c679ef42017d
MD5 7544f410597c484ebd46482b149fa156
BLAKE2b-256 ff8270f7d52798e095610df6d41bf4748f7936662a70ad95659ddde7a202cfb8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_gnns-0.2.0-py3-none-any.whl:

Publisher: release-pypi.yml on mylonasc/tf_gnns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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