Convert Figma logo to code with AI

google-deepmind logograph_nets

Build Graph Nets in Tensorflow

5,342
782
5,342
9

Top Related Projects

9,746

TensorFlow-based neural network library

Graph Neural Network Library for PyTorch

13,348

Python package built to ease deep learning on graph, on top of existing DL frameworks.

1,914

Benchmark datasets, data loaders, and evaluators for graph machine learning

Graph Neural Networks with Keras and Tensorflow 2.

Quick Overview

The Google DeepMind Graph Nets library is a powerful tool for building and training graph neural networks (GNNs). It provides a flexible and modular framework for designing and experimenting with various GNN architectures, making it a valuable resource for researchers and developers working in the field of graph-based machine learning.

Pros

  • Modular and Flexible Design: The library offers a modular and flexible design, allowing users to easily customize and experiment with different GNN architectures and components.
  • Efficient and Scalable: The library is designed to be efficient and scalable, enabling the training and deployment of GNNs on large-scale datasets.
  • Extensive Documentation and Examples: The project provides comprehensive documentation and a wide range of example implementations, making it easier for users to get started and understand the library's capabilities.
  • Active Development and Community: The project is actively maintained by the Google DeepMind team, and it has a growing community of contributors and users, ensuring ongoing support and improvements.

Cons

  • Steep Learning Curve: The library's flexibility and power come with a relatively steep learning curve, especially for users new to graph neural networks and the TensorFlow ecosystem.
  • Limited Support for Non-TensorFlow Backends: The library is primarily designed to work with TensorFlow, which may limit its adoption by users who prefer other deep learning frameworks, such as PyTorch.
  • Potential Performance Overhead: The modular and flexible design of the library may introduce some performance overhead compared to more specialized or streamlined GNN implementations.
  • Dependency on TensorFlow: The library's reliance on TensorFlow may be a drawback for users who prefer to work with other deep learning frameworks or want to avoid the complexity of the TensorFlow ecosystem.

Code Examples

Here are a few short code examples demonstrating the usage of the Google DeepMind Graph Nets library:

  1. Creating a Simple Graph Network:
import graph_nets as gn
import tensorflow as tf

# Define the input graph
input_graph = gn.graphs.GraphsTuple(
    nodes=tf.constant([[1.0], [2.0], [3.0]]),
    edges=tf.constant([[0, 1], [1, 2]]),
    globals=tf.constant([0.5]),
    receivers=tf.constant([0, 1]),
    senders=tf.constant([1, 2]),
    n_node=tf.constant([3]),
    n_edge=tf.constant([2])
)

# Define the graph network model
model = gn.modules.GraphNetwork(
    edge_model_fn=lambda: tf.keras.layers.Dense(1),
    node_model_fn=lambda: tf.keras.layers.Dense(1),
    global_model_fn=lambda: tf.keras.layers.Dense(1)
)

# Apply the model to the input graph
output_graph = model(input_graph)
  1. Defining a Custom Graph Network Module:
import graph_nets as gn
import tensorflow as tf

class CustomGraphNetwork(gn.modules.Module):
    def __init__(self, hidden_size=16):
        super(CustomGraphNetwork, self).__init__()
        self.edge_model = tf.keras.layers.Dense(hidden_size)
        self.node_model = tf.keras.layers.Dense(hidden_size)
        self.global_model = tf.keras.layers.Dense(hidden_size)

    def _build(self, input_graph):
        return gn.modules.GraphNetwork(
            edge_model_fn=lambda: self.edge_model,
            node_model_fn=lambda: self.node_model,
            global_model_fn=lambda: self.global_model
        )(input_graph)
  1. Training a Graph Network Model:
import graph_nets as gn
import tensorflow as tf

# Define the training data
train_data = gn.data.GraphsTuple(...)

# Define the model
model = gn.modules.GraphNetwork(
    edge_model_fn=lambda: tf.keras.layers.Dense(1),
    node_model_fn=lambda: tf.keras.layers.Dense(1),
    global_model_

Competitor Comparisons

9,746

TensorFlow-based neural network library

Pros of Sonnet

  • More general-purpose neural network library, suitable for a wider range of applications
  • Offers a higher level of abstraction, making it easier to build complex models
  • Provides a larger set of pre-built modules and layers

Cons of Sonnet

  • Less specialized for graph-based tasks compared to Graph Nets
  • May require more setup and configuration for graph-specific operations
  • Potentially steeper learning curve for users focused solely on graph neural networks

Code Comparison

Sonnet example:

import sonnet as snt

linear = snt.Linear(output_size=10)
output = linear(input_tensor)

Graph Nets example:

import graph_nets as gn

graph = gn.graphs.GraphsTuple(...)
output = gn.modules.GraphNetwork()(graph)

Both libraries are built on top of TensorFlow and offer modular approaches to building neural networks. However, Graph Nets is specifically designed for graph-based machine learning tasks, while Sonnet provides a more general-purpose toolkit for building neural network components.

Graph Neural Network Library for PyTorch

Pros of PyTorch Geometric

  • Larger community and more frequent updates
  • Extensive documentation and tutorials
  • Wider range of implemented graph neural network models

Cons of PyTorch Geometric

  • Steeper learning curve for beginners
  • Less focus on relational graph networks

Code Comparison

PyTorch Geometric:

import torch
from torch_geometric.nn import GCNConv

class GCN(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = GCNConv(num_features, 16)
        self.conv2 = GCNConv(16, num_classes)

Graph Nets:

import graph_nets as gn
import sonnet as snt

class GNN(snt.Module):
    def __init__(self):
        super().__init__()
        self.graph_network = gn.modules.GraphNetwork(
            edge_model_fn=lambda: snt.nets.MLP([32, 32]),
            node_model_fn=lambda: snt.nets.MLP([32, 32]),
            global_model_fn=lambda: snt.nets.MLP([32, 32]))

Both libraries offer powerful tools for working with graph neural networks, but PyTorch Geometric provides a more extensive ecosystem with a wider range of models and applications. Graph Nets, on the other hand, offers a more specialized focus on relational graph networks and integrates well with other DeepMind libraries.

13,348

Python package built to ease deep learning on graph, on top of existing DL frameworks.

Pros of DGL

  • Broader framework support (PyTorch, MXNet, TensorFlow)
  • More extensive documentation and tutorials
  • Larger community and more frequent updates

Cons of DGL

  • Steeper learning curve for beginners
  • Less focus on specific graph neural network architectures

Code Comparison

DGL:

import dgl
import torch

g = dgl.graph(([0, 1], [1, 2]))
g.ndata['h'] = torch.ones(3, 5)
g.edata['w'] = torch.ones(2, 3)

Graph Nets:

import graph_nets as gn
import tensorflow as tf

graph = gn.graphs.GraphsTuple(
    nodes=tf.ones((3, 5)),
    edges=tf.ones((2, 3)),
    globals=None,
    senders=[0, 1],
    receivers=[1, 2],
    n_node=tf.constant([3]),
    n_edge=tf.constant([2])
)

DGL offers a more concise API for graph creation and attribute assignment, while Graph Nets requires a more explicit definition of graph structure. DGL's approach may be more intuitive for users familiar with PyTorch, while Graph Nets aligns closely with TensorFlow conventions.

1,914

Benchmark datasets, data loaders, and evaluators for graph machine learning

Pros of OGB

  • Provides a comprehensive benchmark suite for graph machine learning tasks
  • Offers a wide range of real-world datasets across various domains
  • Includes standardized evaluation protocols for fair comparisons

Cons of OGB

  • Focuses primarily on benchmarking rather than providing a flexible framework for building graph neural networks
  • May have a steeper learning curve for users new to graph machine learning

Code Comparison

OGB example:

from ogb.nodeproppred import NodePropPredDataset

dataset = NodePropPredDataset(name = "ogbn-arxiv")
graph, label = dataset[0]

Graph Nets example:

import graph_nets as gn

graph = gn.graphs.GraphsTuple(
    nodes=tf.constant([[1.0], [2.0], [3.0]]),
    edges=tf.constant([[1.0], [2.0]]),
    globals=tf.constant([[1.0]]),
    senders=tf.constant([0, 1]),
    receivers=tf.constant([1, 2]),
    n_node=tf.constant([3]),
    n_edge=tf.constant([2])
)

The OGB code focuses on loading pre-defined datasets, while Graph Nets provides a more flexible approach for creating custom graph structures.

Graph Neural Networks with Keras and Tensorflow 2.

Pros of Spektral

  • Built on top of Keras, offering seamless integration with TensorFlow
  • Provides a wider range of graph neural network layers and models
  • Includes built-in datasets and data loading utilities for graph-based tasks

Cons of Spektral

  • Less focus on relational reasoning and more on traditional graph neural networks
  • May have a steeper learning curve for users not familiar with Keras/TensorFlow

Code Comparison

Spektral:

from spektral.layers import GCNConv
from tensorflow.keras.models import Model

class GCN(Model):
    def __init__(self):
        super().__init__()
        self.conv1 = GCNConv(16, activation='relu')
        self.conv2 = GCNConv(10, activation='softmax')

Graph Nets:

import graph_nets as gn
import sonnet as snt

class GNN(snt.Module):
    def __init__(self):
        super().__init__()
        self.graph_network = gn.modules.GraphNetwork(
            edge_model_fn=lambda: snt.nets.MLP([16, 16]),
            node_model_fn=lambda: snt.nets.MLP([16, 16]),
            global_model_fn=lambda: snt.nets.MLP([16, 16]))

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Graph Nets DeepMind shortest path

Graph Nets library

Graph Nets is DeepMind's library for building graph networks in Tensorflow and Sonnet.

Contact graph-nets@google.com for comments and questions.

What are graph networks?

A graph network takes a graph as input and returns a graph as output. The input graph has edge- (E ), node- (V ), and global-level (u) attributes. The output graph has the same structure, but updated attributes. Graph networks are part of the broader family of "graph neural networks" (Scarselli et al., 2009).

To learn more about graph networks, see our arXiv paper: Relational inductive biases, deep learning, and graph networks.

Graph network

Installation

The Graph Nets library can be installed from pip.

This installation is compatible with Linux/Mac OS X, and Python 2.7 and 3.4+.

The library will work with both the CPU and GPU version of TensorFlow, but to allow for that it does not list Tensorflow as a requirement, so you need to install Tensorflow separately if you haven't already done so.

To install the Graph Nets library and use it with TensorFlow 1 and Sonnet 1, run:

(CPU)

$ pip install graph_nets "tensorflow>=1.15,<2" "dm-sonnet<2" "tensorflow_probability<0.9"

(GPU)

$ pip install graph_nets "tensorflow_gpu>=1.15,<2" "dm-sonnet<2" "tensorflow_probability<0.9"

To install the Graph Nets library and use it with TensorFlow 2 and Sonnet 2, run:

(CPU)

$ pip install graph_nets "tensorflow>=2.1.0-rc1" "dm-sonnet>=2.0.0b0" tensorflow_probability

(GPU)

$ pip install graph_nets "tensorflow_gpu>=2.1.0-rc1" "dm-sonnet>=2.0.0b0" tensorflow_probability

The latest version of the library requires TensorFlow >=1.15. For compatibility with earlier versions of TensorFlow, please install v1.0.4 of the Graph Nets library.

Usage example

The following code constructs a simple graph net module and connects it to data.

import graph_nets as gn
import sonnet as snt

# Provide your own functions to generate graph-structured data.
input_graphs = get_graphs()

# Create the graph network.
graph_net_module = gn.modules.GraphNetwork(
    edge_model_fn=lambda: snt.nets.MLP([32, 32]),
    node_model_fn=lambda: snt.nets.MLP([32, 32]),
    global_model_fn=lambda: snt.nets.MLP([32, 32]))

# Pass the input graphs to the graph network, and return the output graphs.
output_graphs = graph_net_module(input_graphs)

Demo Jupyter notebooks

The library includes demos which show how to create, manipulate, and train graph networks to reason about graph-structured data, on a shortest path-finding task, a sorting task, and a physical prediction task. Each demo uses the same graph network architecture, which highlights the flexibility of the approach.

Try the demos in your browser in Colaboratory

To try out the demos without installing anything locally, you can run the demos in your browser (even on your phone) via a cloud Colaboratory backend. Click a demo link below, and follow the instructions in the notebook.


Run "shortest path demo" in browser

The "shortest path demo" creates random graphs, and trains a graph network to label the nodes and edges on the shortest path between any two nodes. Over a sequence of message-passing steps (as depicted by each step's plot), the model refines its prediction of the shortest path.

Shortest path


Run "sort demo" in browser (Run TF2 version)

The "sort demo" creates lists of random numbers, and trains a graph network to sort the list. After a sequence of message-passing steps, the model makes an accurate prediction of which elements (columns in the figure) come next after each other (rows).

Sort


Run "physics demo" in browser

The "physics demo" creates random mass-spring physical systems, and trains a graph network to predict the state of the system on the next timestep. The model's next-step predictions can be fed back in as input to create a rollout of a future trajectory. Each subplot below shows the true and predicted mass-spring system states over 50 steps. This is similar to the model and experiments in Battaglia et al. (2016)'s "interaction networks".

Physics


Run "graph nets basics demo" in browser (Run TF2 version)

The "graph nets basics demo" is a tutorial containing step by step examples about how to create and manipulate graphs, how to feed them into graph networks and how to build custom graph network modules.


Run the demos on your local machine

To install the necessary dependencies, run:

$ pip install jupyter matplotlib scipy

To try the demos, run:

$ cd <path-to-graph-nets-library>/demos
$ jupyter notebook

then open a demo through the Jupyter notebook interface.

Other graph neural network libraries

Check out these high-quality open-source libraries for graph neural networks: