tf-geometric


Nametf-geometric JSON
Version 0.1.6 PyPI version JSON
download
home_pagehttps://github.com/CrawlScript/tf_geometric
SummaryEfficient and Friendly Graph Neural Network Library for TensorFlow 1.x and 2.x.
upload_time2023-05-26 09:14:21
maintainer
docs_urlNone
authorJun Hu
requires_python>3.5.0
licenseGNU General Public License v3.0 (See LICENSE)
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            tf_geometric
============

Efficient and Friendly Graph Neural Network Library for TensorFlow 1.x and 2.x.

Inspired by **rusty1s/pytorch_geometric**\ , we build a GNN library for TensorFlow.

Homepage and Documentation
--------------------------


* Homepage: `https://github.com/CrawlScript/tf_geometric <https://github.com/CrawlScript/tf_geometric>`_
* Documentation: `https://tf-geometric.readthedocs.io <https://tf-geometric.readthedocs.io>`_ (\ `中文版 <https://tf-geometric.readthedocs.io/en/latest/index_cn.html>`_\ )
* Paper: `Efficient Graph Deep Learning in TensorFlow with tf_geometric <https://arxiv.org/abs/2101.11552>`_

Efficient and Friendly
----------------------

We use Message Passing mechanism to implement graph neural networks, which is way efficient than the dense matrix based implementations and more friendly than the sparse matrix based ones.
In addition, we provide easy and elegant APIs for complex GNN operations.
The following example constructs a graph and applies a Multi-head Graph Attention Network (GAT) on it:

.. code-block:: python

   # coding=utf-8
   import numpy as np
   import tf_geometric as tfg
   import tensorflow as tf

   graph = tfg.Graph(
       x=np.random.randn(5, 20),  # 5 nodes, 20 features,
       edge_index=[[0, 0, 1, 3],
                   [1, 2, 2, 1]]  # 4 undirected edges
   )

   print("Graph Desc: \n", graph)

   graph = graph.to_directed()  # pre-process edges
   print("Processed Graph Desc: \n", graph)
   print("Processed Edge Index:\n", graph.edge_index)

   # Multi-head Graph Attention Network (GAT)
   gat_layer = tfg.layers.GAT(units=4, num_heads=4, activation=tf.nn.relu)
   output = gat_layer([graph.x, graph.edge_index])
   print("Output of GAT: \n", output)

Output:

.. code-block:: html

   Graph Desc:
    Graph Shape: x => (5, 20)  edge_index => (2, 4)    y => None

   Processed Graph Desc:
    Graph Shape: x => (5, 20)  edge_index => (2, 8)    y => None

   Processed Edge Index:
    [[0 0 1 1 1 2 2 3]
    [1 2 0 2 3 0 1 1]]

   Output of GAT:
    tf.Tensor(
   [[0.22443159 0.         0.58263206 0.32468423]
    [0.29810357 0.         0.19403605 0.35630274]
    [0.18071976 0.         0.58263206 0.32468423]
    [0.36123228 0.         0.88897204 0.450244  ]
    [0.         0.         0.8013462  0.        ]], shape=(5, 4), dtype=float32)

DEMO
----

We recommend you to get started with some demo.

Node Classification
^^^^^^^^^^^^^^^^^^^


* `Graph Convolutional Network (GCN) <demo/demo_gcn.py>`_
* `Multi-head Graph Attention Network (GAT) <demo/demo_gat.py>`_
* `Approximate Personalized Propagation of Neural Predictions (APPNP) <demo/demo_appnp.py>`_
* `Inductive Representation Learning on Large Graphs (GraphSAGE) <demo/demo_graph_sage.py>`_
* `Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering (ChebyNet) <demo/demo_chebynet.py>`_
* `Simple Graph Convolution (SGC) <demo/demo_sgc.py>`_
* `Topology Adaptive Graph Convolutional Network (TAGCN) <demo/demo_tagcn.py>`_
* `Deep Graph Infomax (DGI) <demo/demo_dgi.py>`_
* `DropEdge: Towards Deep Graph Convolutional Networks on Node Classification (DropEdge) <demo/demo_drop_edge_gcn.py>`_
* `Graph Convolutional Networks for Text Classification (TextGCN) <https://github.com/CrawlScript/TensorFlow-TextGCN>`_
* `Simple Spectral Graph Convolution (SSGC/S^2GC) <demo/demo_ssgc.py>`_

Graph Classification
^^^^^^^^^^^^^^^^^^^^


* `MeanPooling <demo/demo_mean_pool.py>`_
* `Graph Isomorphism Network (GIN) <demo/demo_gin.py>`_
* `Self-Attention Graph Pooling (SAGPooling) <demo/demo_sag_pool_h.py>`_
* `Hierarchical Graph Representation Learning with Differentiable Pooling (DiffPool) <demo/demo_diff_pool.py>`_
* `Order Matters: Sequence to Sequence for Sets (Set2Set) <demo/demo_set2set.py>`_
* `ASAP: Adaptive Structure Aware Pooling for Learning Hierarchical Graph Representations (ASAP) <demo/demo_asap.py>`_
* `An End-to-End Deep Learning Architecture for Graph Classification (SortPool) <demo/demo_sort_pool.py>`_
* `Spectral Clustering with Graph Neural Networks for Graph Pooling (MinCutPool) <demo/demo_min_cut_pool.py>`_

Link Prediction
^^^^^^^^^^^^^^^


* `Graph Auto-Encoder (GAE) <demo/demo_gae.py>`_

Save and Load Models
^^^^^^^^^^^^^^^^^^^^


* `Save and Load Models <demo/demo_save_and_load_model.py>`_
* `Save and Load Models with tf.train.Checkpoint <demo/demo_checkpoint.py>`_

Distributed Training
^^^^^^^^^^^^^^^^^^^^


* `Distributed GCN for Node Classification <demo/demo_distributed_gcn.py>`_
* `Distributed MeanPooling for Graph Classification <demo/demo_distributed_mean_pool.py>`_

Sparse
^^^^^^


* `Sparse Node Features <demo/demo_sparse_node_features.py>`_

Installation
------------

Requirements:


* Operation System: Windows / Linux / Mac OS
* Python: version >= 3.5 and version != 3.6
* Python Packages:

  * tensorflow/tensorflow-gpu: >= 1.15.0 or >= 2.3.0
  * tf_sparse
  * numpy >= 1.17.4
  * networkx >= 2.1
  * scipy >= 1.1.0

Use one of the following commands below:

.. code-block:: bash

   pip install -U tf_geometric # this will not install the tensorflow/tensorflow-gpu package

   pip install -U tf_geometric[tf1-cpu] # this will install TensorFlow 1.x CPU version

   pip install -U tf_geometric[tf1-gpu] # this will install TensorFlow 1.x GPU version

   pip install -U tf_geometric[tf2-cpu] # this will install TensorFlow 2.x CPU version

   pip install -U tf_geometric[tf2-gpu] # this will install TensorFlow 2.x GPU version

OOP and Functional API
----------------------

We provide both OOP and Functional API, with which you can make some cool things.

.. code-block:: python

   # coding=utf-8
   import os
   # Enable GPU 0
   os.environ["CUDA_VISIBLE_DEVICES"] = "0"

   import tf_geometric as tfg
   import tensorflow as tf
   import numpy as np

   # ==================================== Graph Data Structure ====================================
   # In tf_geometric, the data of a graph can be represented by either a collections of
   # tensors (numpy.ndarray or tf.Tensor) or a tfg.Graph object.
   # A graph usually consists of x(node features), edge_index and edge_weight(optional)

   # Node Features => (num_nodes, num_features)
   x = np.random.randn(5, 20).astype(np.float32)  # 5 nodes, 20 features

   # Edge Index => (2, num_edges)
   # Each column of edge_index (u, v) represents an directed edge from u to v.
   # Note that it does not cover the edge from v to u. You should provide (v, u) to cover it.
   # This is not convenient for users.
   # Thus, we allow users to provide edge_index in undirected form and convert it later.
   # That is, we can only provide (u, v) and convert it to (u, v) and (v, u) with `convert_edge_to_directed` method.
   edge_index = np.array([
       [0, 0, 1, 3],
       [1, 2, 2, 1]
   ])

   # Edge Weight => (num_edges)
   edge_weight = np.array([0.9, 0.8, 0.1, 0.2]).astype(np.float32)


   # Usually, we use a graph object to manager these information
   # edge_weight is optional, we can set it to None if you don't need it
   # Using 'to_directed' to obtain a graph with directed edges such that we can use it as the input of GCN
   graph = tfg.Graph(x=x, edge_index=edge_index, edge_weight=edge_weight).to_directed()


   # Define a Graph Convolutional Layer (GCN)
   gcn_layer = tfg.layers.GCN(4, activation=tf.nn.relu)
   # Perform GCN on the graph
   h = gcn_layer([graph.x, graph.edge_index, graph.edge_weight])
   print("Node Representations (GCN on a Graph): \n", h)

   for _ in range(10):
       # Using Graph.cache can avoid recomputation of GCN's normalized adjacency matrix,
       # which can dramatically improve the efficiency of GCN.
       h = gcn_layer([graph.x, graph.edge_index, graph.edge_weight], cache=graph.cache)


   # For algorithms that deal with batches of graphs, we can pack a batch of graph into a BatchGraph object
   # Batch graph wrap a batch of graphs into a single graph, where each nodes has an unique index and a graph index.
   # The node_graph_index is the index of the corresponding graph for each node in the batch.
   # The edge_graph_index is the index of the corresponding edge for each node in the batch.
   batch_graph = tfg.BatchGraph.from_graphs([graph, graph, graph, graph, graph])

   # We can reversely split a BatchGraph object into Graphs objects
   graphs = batch_graph.to_graphs()

   # Define a Graph Convolutional Layer (GCN)
   batch_gcn_layer = tfg.layers.GCN(4, activation=tf.nn.relu)
   # Perform GCN on the BatchGraph
   batch_h = gcn_layer([batch_graph.x, batch_graph.edge_index, batch_graph.edge_weight])
   print("Node Representations (GCN on a BatchGraph): \n", batch_h)

   # Graph Pooling algorithms often rely on such batch data structure
   # Most of them accept a BatchGraph's data as input and output a feature vector for each graph in the batch
   graph_h = tfg.nn.mean_pool(batch_h, batch_graph.node_graph_index, num_graphs=batch_graph.num_graphs)
   print("Graph Representations (Mean Pooling on a BatchGraph): \n", batch_h)


   # Define a Graph Convolutional Layer (GCN) for scoring each node
   gcn_score_layer = tfg.layers.GCN(1)
   # We provide some advanced graph pooling operations such as topk_pool
   node_score = gcn_score_layer([batch_graph.x, batch_graph.edge_index, batch_graph.edge_weight])
   node_score = tf.reshape(node_score, [-1])
   print("Score of Each Node: \n", node_score)
   topk_node_index = tfg.nn.topk_pool(batch_graph.node_graph_index, node_score, ratio=0.6)
   print("Top-k Node Index (Top-k Pooling): \n", topk_node_index)




   # ==================================== Built-in Datasets ====================================
   # all graph data are in numpy format

   # Cora Dataset
   graph, (train_index, valid_index, test_index) = tfg.datasets.CoraDataset().load_data()

   # PPI Dataset
   train_data, valid_data, test_data = tfg.datasets.PPIDataset().load_data()

   # TU Datasets
   # TU Datasets: https://ls11-www.cs.tu-dortmund.de/staff/morris/graphkerneldatasets
   graph_dicts = tfg.datasets.TUDataset("NCI1").load_data()


   # ==================================== Basic OOP API ====================================
   # OOP Style GCN (Graph Convolutional Network)
   gcn_layer = tfg.layers.GCN(units=20, activation=tf.nn.relu)

   for graph in test_data:
       # Cache can speed-up GCN by caching the normed edge information
       outputs = gcn_layer([graph.x, graph.edge_index, graph.edge_weight], cache=graph.cache)
       print(outputs)


   # OOP Style GAT (Multi-head Graph Attention Network)
   gat_layer = tfg.layers.GAT(units=20, activation=tf.nn.relu, num_heads=4)
   for graph in test_data:
       outputs = gat_layer([graph.x, graph.edge_index])
       print(outputs)


   # OOP Style Multi-layer GCN Model
   class GCNModel(tf.keras.Model):

       def __init__(self, *args, **kwargs):
           super().__init__(*args, **kwargs)
           self.gcn0 = tfg.layers.GCN(16, activation=tf.nn.relu)
           self.gcn1 = tfg.layers.GCN(7)
           self.dropout = tf.keras.layers.Dropout(0.5)

       def call(self, inputs, training=None, mask=None, cache=None):
           x, edge_index, edge_weight = inputs
           h = self.dropout(x, training=training)
           h = self.gcn0([h, edge_index, edge_weight], cache=cache)
           h = self.dropout(h, training=training)
           h = self.gcn1([h, edge_index, edge_weight], cache=cache)
           return h


   gcn_model = GCNModel()
   for graph in test_data:
       outputs = gcn_model([graph.x, graph.edge_index, graph.edge_weight], cache=graph.cache)
       print(outputs)


   # ==================================== Basic Functional API ====================================
   # Functional Style GCN
   # Functional API is more flexible for advanced algorithms
   # You can pass both data and parameters to functional APIs

   gcn_w = tf.Variable(tf.random.truncated_normal([test_data[0].num_features, 20]))
   for graph in test_data:
       outputs = tfg.nn.gcn(graph.x, graph.adj(), gcn_w, activation=tf.nn.relu)
       print(outputs)


   # ==================================== Advanced Functional API ====================================
   # Most APIs are implemented with Map-Reduce Style
   # This is a gcn without without weight normalization and transformation
   # Just pass the mapper/reducer/updater functions to the Functional API

   for graph in test_data:
       outputs = tfg.nn.aggregate_neighbors(
           x=graph.x,
           edge_index=graph.edge_index,
           edge_weight=graph.edge_weight,
           mapper=tfg.nn.identity_mapper,
           reducer=tfg.nn.sum_reducer,
           updater=tfg.nn.sum_updater
       )
       print(outputs)

Cite
----

If you use tf_geometric in a scientific publication, we would appreciate citations to the following paper:

.. code-block:: html

   @inproceedings{DBLP:conf/mm/HuQFWZZX21,
     author    = {Jun Hu and
                  Shengsheng Qian and
                  Quan Fang and
                  Youze Wang and
                  Quan Zhao and
                  Huaiwen Zhang and
                  Changsheng Xu},
     editor    = {Heng Tao Shen and
                  Yueting Zhuang and
                  John R. Smith and
                  Yang Yang and
                  Pablo Cesar and
                  Florian Metze and
                  Balakrishnan Prabhakaran},
     title     = {Efficient Graph Deep Learning in TensorFlow with tf{\_}geometric},
     booktitle = {{MM} '21: {ACM} Multimedia Conference, Virtual Event, China, October
                  20 - 24, 2021},
     pages     = {3775--3778},
     publisher = {{ACM}},
     year      = {2021},
     url       = {https://doi.org/10.1145/3474085.3478322},
     doi       = {10.1145/3474085.3478322},
     timestamp = {Wed, 20 Oct 2021 12:40:01 +0200},
     biburl    = {https://dblp.org/rec/conf/mm/HuQFWZZX21.bib},
     bibsource = {dblp computer science bibliography, https://dblp.org}
   }

Related Projects
----------------


* **tf_sparse:** We develop `TensorFlow Sparse (tf_sparse) <https://github.com/CrawlScript/tf_sparse>`_ to implement efficient and elegant 
  sparse TensorFlow operations for tf_geometric. URL: `https://github.com/CrawlScript/tf_sparse <https://github.com/CrawlScript/tf_sparse>`_.
* **GRecX:** `GRecX <https://github.com/maenzhier/GRecX>`_ is an efficient and unified benchmark for GNN-based recommendation. URL: `https://github.com/maenzhier/GRecX <https://github.com/maenzhier/GRecX>`_.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/CrawlScript/tf_geometric",
    "name": "tf-geometric",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">3.5.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Jun Hu",
    "author_email": "hujunxianligong@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/be/65/4194f9c75ccebe479a8faa161ce28456e5d7cf1766c803944336e6bce8ed/tf_geometric-0.1.6.tar.gz",
    "platform": null,
    "description": "tf_geometric\n============\n\nEfficient and Friendly Graph Neural Network Library for TensorFlow 1.x and 2.x.\n\nInspired by **rusty1s/pytorch_geometric**\\ , we build a GNN library for TensorFlow.\n\nHomepage and Documentation\n--------------------------\n\n\n* Homepage: `https://github.com/CrawlScript/tf_geometric <https://github.com/CrawlScript/tf_geometric>`_\n* Documentation: `https://tf-geometric.readthedocs.io <https://tf-geometric.readthedocs.io>`_ (\\ `\u4e2d\u6587\u7248 <https://tf-geometric.readthedocs.io/en/latest/index_cn.html>`_\\ )\n* Paper: `Efficient Graph Deep Learning in TensorFlow with tf_geometric <https://arxiv.org/abs/2101.11552>`_\n\nEfficient and Friendly\n----------------------\n\nWe use Message Passing mechanism to implement graph neural networks, which is way efficient than the dense matrix based implementations and more friendly than the sparse matrix based ones.\nIn addition, we provide easy and elegant APIs for complex GNN operations.\nThe following example constructs a graph and applies a Multi-head Graph Attention Network (GAT) on it:\n\n.. code-block:: python\n\n   # coding=utf-8\n   import numpy as np\n   import tf_geometric as tfg\n   import tensorflow as tf\n\n   graph = tfg.Graph(\n       x=np.random.randn(5, 20),  # 5 nodes, 20 features,\n       edge_index=[[0, 0, 1, 3],\n                   [1, 2, 2, 1]]  # 4 undirected edges\n   )\n\n   print(\"Graph Desc: \\n\", graph)\n\n   graph = graph.to_directed()  # pre-process edges\n   print(\"Processed Graph Desc: \\n\", graph)\n   print(\"Processed Edge Index:\\n\", graph.edge_index)\n\n   # Multi-head Graph Attention Network (GAT)\n   gat_layer = tfg.layers.GAT(units=4, num_heads=4, activation=tf.nn.relu)\n   output = gat_layer([graph.x, graph.edge_index])\n   print(\"Output of GAT: \\n\", output)\n\nOutput:\n\n.. code-block:: html\n\n   Graph Desc:\n    Graph Shape: x => (5, 20)  edge_index => (2, 4)    y => None\n\n   Processed Graph Desc:\n    Graph Shape: x => (5, 20)  edge_index => (2, 8)    y => None\n\n   Processed Edge Index:\n    [[0 0 1 1 1 2 2 3]\n    [1 2 0 2 3 0 1 1]]\n\n   Output of GAT:\n    tf.Tensor(\n   [[0.22443159 0.         0.58263206 0.32468423]\n    [0.29810357 0.         0.19403605 0.35630274]\n    [0.18071976 0.         0.58263206 0.32468423]\n    [0.36123228 0.         0.88897204 0.450244  ]\n    [0.         0.         0.8013462  0.        ]], shape=(5, 4), dtype=float32)\n\nDEMO\n----\n\nWe recommend you to get started with some demo.\n\nNode Classification\n^^^^^^^^^^^^^^^^^^^\n\n\n* `Graph Convolutional Network (GCN) <demo/demo_gcn.py>`_\n* `Multi-head Graph Attention Network (GAT) <demo/demo_gat.py>`_\n* `Approximate Personalized Propagation of Neural Predictions (APPNP) <demo/demo_appnp.py>`_\n* `Inductive Representation Learning on Large Graphs (GraphSAGE) <demo/demo_graph_sage.py>`_\n* `Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering (ChebyNet) <demo/demo_chebynet.py>`_\n* `Simple Graph Convolution (SGC) <demo/demo_sgc.py>`_\n* `Topology Adaptive Graph Convolutional Network (TAGCN) <demo/demo_tagcn.py>`_\n* `Deep Graph Infomax (DGI) <demo/demo_dgi.py>`_\n* `DropEdge: Towards Deep Graph Convolutional Networks on Node Classification (DropEdge) <demo/demo_drop_edge_gcn.py>`_\n* `Graph Convolutional Networks for Text Classification (TextGCN) <https://github.com/CrawlScript/TensorFlow-TextGCN>`_\n* `Simple Spectral Graph Convolution (SSGC/S^2GC) <demo/demo_ssgc.py>`_\n\nGraph Classification\n^^^^^^^^^^^^^^^^^^^^\n\n\n* `MeanPooling <demo/demo_mean_pool.py>`_\n* `Graph Isomorphism Network (GIN) <demo/demo_gin.py>`_\n* `Self-Attention Graph Pooling (SAGPooling) <demo/demo_sag_pool_h.py>`_\n* `Hierarchical Graph Representation Learning with Differentiable Pooling (DiffPool) <demo/demo_diff_pool.py>`_\n* `Order Matters: Sequence to Sequence for Sets (Set2Set) <demo/demo_set2set.py>`_\n* `ASAP: Adaptive Structure Aware Pooling for Learning Hierarchical Graph Representations (ASAP) <demo/demo_asap.py>`_\n* `An End-to-End Deep Learning Architecture for Graph Classification (SortPool) <demo/demo_sort_pool.py>`_\n* `Spectral Clustering with Graph Neural Networks for Graph Pooling (MinCutPool) <demo/demo_min_cut_pool.py>`_\n\nLink Prediction\n^^^^^^^^^^^^^^^\n\n\n* `Graph Auto-Encoder (GAE) <demo/demo_gae.py>`_\n\nSave and Load Models\n^^^^^^^^^^^^^^^^^^^^\n\n\n* `Save and Load Models <demo/demo_save_and_load_model.py>`_\n* `Save and Load Models with tf.train.Checkpoint <demo/demo_checkpoint.py>`_\n\nDistributed Training\n^^^^^^^^^^^^^^^^^^^^\n\n\n* `Distributed GCN for Node Classification <demo/demo_distributed_gcn.py>`_\n* `Distributed MeanPooling for Graph Classification <demo/demo_distributed_mean_pool.py>`_\n\nSparse\n^^^^^^\n\n\n* `Sparse Node Features <demo/demo_sparse_node_features.py>`_\n\nInstallation\n------------\n\nRequirements:\n\n\n* Operation System: Windows / Linux / Mac OS\n* Python: version >= 3.5 and version != 3.6\n* Python Packages:\n\n  * tensorflow/tensorflow-gpu: >= 1.15.0 or >= 2.3.0\n  * tf_sparse\n  * numpy >= 1.17.4\n  * networkx >= 2.1\n  * scipy >= 1.1.0\n\nUse one of the following commands below:\n\n.. code-block:: bash\n\n   pip install -U tf_geometric # this will not install the tensorflow/tensorflow-gpu package\n\n   pip install -U tf_geometric[tf1-cpu] # this will install TensorFlow 1.x CPU version\n\n   pip install -U tf_geometric[tf1-gpu] # this will install TensorFlow 1.x GPU version\n\n   pip install -U tf_geometric[tf2-cpu] # this will install TensorFlow 2.x CPU version\n\n   pip install -U tf_geometric[tf2-gpu] # this will install TensorFlow 2.x GPU version\n\nOOP and Functional API\n----------------------\n\nWe provide both OOP and Functional API, with which you can make some cool things.\n\n.. code-block:: python\n\n   # coding=utf-8\n   import os\n   # Enable GPU 0\n   os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n\n   import tf_geometric as tfg\n   import tensorflow as tf\n   import numpy as np\n\n   # ==================================== Graph Data Structure ====================================\n   # In tf_geometric, the data of a graph can be represented by either a collections of\n   # tensors (numpy.ndarray or tf.Tensor) or a tfg.Graph object.\n   # A graph usually consists of x(node features), edge_index and edge_weight(optional)\n\n   # Node Features => (num_nodes, num_features)\n   x = np.random.randn(5, 20).astype(np.float32)  # 5 nodes, 20 features\n\n   # Edge Index => (2, num_edges)\n   # Each column of edge_index (u, v) represents an directed edge from u to v.\n   # Note that it does not cover the edge from v to u. You should provide (v, u) to cover it.\n   # This is not convenient for users.\n   # Thus, we allow users to provide edge_index in undirected form and convert it later.\n   # That is, we can only provide (u, v) and convert it to (u, v) and (v, u) with `convert_edge_to_directed` method.\n   edge_index = np.array([\n       [0, 0, 1, 3],\n       [1, 2, 2, 1]\n   ])\n\n   # Edge Weight => (num_edges)\n   edge_weight = np.array([0.9, 0.8, 0.1, 0.2]).astype(np.float32)\n\n\n   # Usually, we use a graph object to manager these information\n   # edge_weight is optional, we can set it to None if you don't need it\n   # Using 'to_directed' to obtain a graph with directed edges such that we can use it as the input of GCN\n   graph = tfg.Graph(x=x, edge_index=edge_index, edge_weight=edge_weight).to_directed()\n\n\n   # Define a Graph Convolutional Layer (GCN)\n   gcn_layer = tfg.layers.GCN(4, activation=tf.nn.relu)\n   # Perform GCN on the graph\n   h = gcn_layer([graph.x, graph.edge_index, graph.edge_weight])\n   print(\"Node Representations (GCN on a Graph): \\n\", h)\n\n   for _ in range(10):\n       # Using Graph.cache can avoid recomputation of GCN's normalized adjacency matrix,\n       # which can dramatically improve the efficiency of GCN.\n       h = gcn_layer([graph.x, graph.edge_index, graph.edge_weight], cache=graph.cache)\n\n\n   # For algorithms that deal with batches of graphs, we can pack a batch of graph into a BatchGraph object\n   # Batch graph wrap a batch of graphs into a single graph, where each nodes has an unique index and a graph index.\n   # The node_graph_index is the index of the corresponding graph for each node in the batch.\n   # The edge_graph_index is the index of the corresponding edge for each node in the batch.\n   batch_graph = tfg.BatchGraph.from_graphs([graph, graph, graph, graph, graph])\n\n   # We can reversely split a BatchGraph object into Graphs objects\n   graphs = batch_graph.to_graphs()\n\n   # Define a Graph Convolutional Layer (GCN)\n   batch_gcn_layer = tfg.layers.GCN(4, activation=tf.nn.relu)\n   # Perform GCN on the BatchGraph\n   batch_h = gcn_layer([batch_graph.x, batch_graph.edge_index, batch_graph.edge_weight])\n   print(\"Node Representations (GCN on a BatchGraph): \\n\", batch_h)\n\n   # Graph Pooling algorithms often rely on such batch data structure\n   # Most of them accept a BatchGraph's data as input and output a feature vector for each graph in the batch\n   graph_h = tfg.nn.mean_pool(batch_h, batch_graph.node_graph_index, num_graphs=batch_graph.num_graphs)\n   print(\"Graph Representations (Mean Pooling on a BatchGraph): \\n\", batch_h)\n\n\n   # Define a Graph Convolutional Layer (GCN) for scoring each node\n   gcn_score_layer = tfg.layers.GCN(1)\n   # We provide some advanced graph pooling operations such as topk_pool\n   node_score = gcn_score_layer([batch_graph.x, batch_graph.edge_index, batch_graph.edge_weight])\n   node_score = tf.reshape(node_score, [-1])\n   print(\"Score of Each Node: \\n\", node_score)\n   topk_node_index = tfg.nn.topk_pool(batch_graph.node_graph_index, node_score, ratio=0.6)\n   print(\"Top-k Node Index (Top-k Pooling): \\n\", topk_node_index)\n\n\n\n\n   # ==================================== Built-in Datasets ====================================\n   # all graph data are in numpy format\n\n   # Cora Dataset\n   graph, (train_index, valid_index, test_index) = tfg.datasets.CoraDataset().load_data()\n\n   # PPI Dataset\n   train_data, valid_data, test_data = tfg.datasets.PPIDataset().load_data()\n\n   # TU Datasets\n   # TU Datasets: https://ls11-www.cs.tu-dortmund.de/staff/morris/graphkerneldatasets\n   graph_dicts = tfg.datasets.TUDataset(\"NCI1\").load_data()\n\n\n   # ==================================== Basic OOP API ====================================\n   # OOP Style GCN (Graph Convolutional Network)\n   gcn_layer = tfg.layers.GCN(units=20, activation=tf.nn.relu)\n\n   for graph in test_data:\n       # Cache can speed-up GCN by caching the normed edge information\n       outputs = gcn_layer([graph.x, graph.edge_index, graph.edge_weight], cache=graph.cache)\n       print(outputs)\n\n\n   # OOP Style GAT (Multi-head Graph Attention Network)\n   gat_layer = tfg.layers.GAT(units=20, activation=tf.nn.relu, num_heads=4)\n   for graph in test_data:\n       outputs = gat_layer([graph.x, graph.edge_index])\n       print(outputs)\n\n\n   # OOP Style Multi-layer GCN Model\n   class GCNModel(tf.keras.Model):\n\n       def __init__(self, *args, **kwargs):\n           super().__init__(*args, **kwargs)\n           self.gcn0 = tfg.layers.GCN(16, activation=tf.nn.relu)\n           self.gcn1 = tfg.layers.GCN(7)\n           self.dropout = tf.keras.layers.Dropout(0.5)\n\n       def call(self, inputs, training=None, mask=None, cache=None):\n           x, edge_index, edge_weight = inputs\n           h = self.dropout(x, training=training)\n           h = self.gcn0([h, edge_index, edge_weight], cache=cache)\n           h = self.dropout(h, training=training)\n           h = self.gcn1([h, edge_index, edge_weight], cache=cache)\n           return h\n\n\n   gcn_model = GCNModel()\n   for graph in test_data:\n       outputs = gcn_model([graph.x, graph.edge_index, graph.edge_weight], cache=graph.cache)\n       print(outputs)\n\n\n   # ==================================== Basic Functional API ====================================\n   # Functional Style GCN\n   # Functional API is more flexible for advanced algorithms\n   # You can pass both data and parameters to functional APIs\n\n   gcn_w = tf.Variable(tf.random.truncated_normal([test_data[0].num_features, 20]))\n   for graph in test_data:\n       outputs = tfg.nn.gcn(graph.x, graph.adj(), gcn_w, activation=tf.nn.relu)\n       print(outputs)\n\n\n   # ==================================== Advanced Functional API ====================================\n   # Most APIs are implemented with Map-Reduce Style\n   # This is a gcn without without weight normalization and transformation\n   # Just pass the mapper/reducer/updater functions to the Functional API\n\n   for graph in test_data:\n       outputs = tfg.nn.aggregate_neighbors(\n           x=graph.x,\n           edge_index=graph.edge_index,\n           edge_weight=graph.edge_weight,\n           mapper=tfg.nn.identity_mapper,\n           reducer=tfg.nn.sum_reducer,\n           updater=tfg.nn.sum_updater\n       )\n       print(outputs)\n\nCite\n----\n\nIf you use tf_geometric in a scientific publication, we would appreciate citations to the following paper:\n\n.. code-block:: html\n\n   @inproceedings{DBLP:conf/mm/HuQFWZZX21,\n     author    = {Jun Hu and\n                  Shengsheng Qian and\n                  Quan Fang and\n                  Youze Wang and\n                  Quan Zhao and\n                  Huaiwen Zhang and\n                  Changsheng Xu},\n     editor    = {Heng Tao Shen and\n                  Yueting Zhuang and\n                  John R. Smith and\n                  Yang Yang and\n                  Pablo Cesar and\n                  Florian Metze and\n                  Balakrishnan Prabhakaran},\n     title     = {Efficient Graph Deep Learning in TensorFlow with tf{\\_}geometric},\n     booktitle = {{MM} '21: {ACM} Multimedia Conference, Virtual Event, China, October\n                  20 - 24, 2021},\n     pages     = {3775--3778},\n     publisher = {{ACM}},\n     year      = {2021},\n     url       = {https://doi.org/10.1145/3474085.3478322},\n     doi       = {10.1145/3474085.3478322},\n     timestamp = {Wed, 20 Oct 2021 12:40:01 +0200},\n     biburl    = {https://dblp.org/rec/conf/mm/HuQFWZZX21.bib},\n     bibsource = {dblp computer science bibliography, https://dblp.org}\n   }\n\nRelated Projects\n----------------\n\n\n* **tf_sparse:** We develop `TensorFlow Sparse (tf_sparse) <https://github.com/CrawlScript/tf_sparse>`_ to implement efficient and elegant \n  sparse TensorFlow operations for tf_geometric. URL: `https://github.com/CrawlScript/tf_sparse <https://github.com/CrawlScript/tf_sparse>`_.\n* **GRecX:** `GRecX <https://github.com/maenzhier/GRecX>`_ is an efficient and unified benchmark for GNN-based recommendation. URL: `https://github.com/maenzhier/GRecX <https://github.com/maenzhier/GRecX>`_.\n",
    "bugtrack_url": null,
    "license": "GNU General Public License v3.0 (See LICENSE)",
    "summary": "Efficient and Friendly Graph Neural Network Library for TensorFlow 1.x and 2.x.",
    "version": "0.1.6",
    "project_urls": {
        "Homepage": "https://github.com/CrawlScript/tf_geometric"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "be654194f9c75ccebe479a8faa161ce28456e5d7cf1766c803944336e6bce8ed",
                "md5": "615f37349d5256f06da2d2a3d18aa391",
                "sha256": "0c3308a825b28cceeacf5162d1f1f60375c89b62f63258472e3f439817ee1e16"
            },
            "downloads": -1,
            "filename": "tf_geometric-0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "615f37349d5256f06da2d2a3d18aa391",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">3.5.0",
            "size": 71217,
            "upload_time": "2023-05-26T09:14:21",
            "upload_time_iso_8601": "2023-05-26T09:14:21.560440Z",
            "url": "https://files.pythonhosted.org/packages/be/65/4194f9c75ccebe479a8faa161ce28456e5d7cf1766c803944336e6bce8ed/tf_geometric-0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-26 09:14:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "CrawlScript",
    "github_project": "tf_geometric",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "tf-geometric"
}
        
Elapsed time: 0.07270s