本文已加入 🚀 Python AI 计划,从一个Python小白到一个AI大神,你所需要的所有知识都在 这里 了。
本文定位是:图神经网络(GNN)教程,后续实战案例文章将加入 《深度学习100例》
tf_geometric 是一个高效且友好的图神经网络库,同时支持TensorFlow 1.x 和 2.x。
受到 usty1s/pytorch_geometric 项目的启发,我们为TensorFlow构建了一个图神经网络(GNN)库。 tf_geometric 同时提供面向对象接口(OOP API)和函数式接口(Functional API),你可以用它们来构建有趣的模型。
tf_geometric使用消息传递机制来实现图神经网络:相比于基于稠密矩阵的实现,它具有更高的效率;相比于基于稀疏矩阵的实现,它具有更友好的API。 除此之外,tf_geometric还为复杂的图神经网络操作提供了简易优雅的API。 下面的示例展现了使用tf_geometric构建一个图结构的数据,并使用多头图注意力网络(Multi-head GAT)对图数据进行处理的流程:
# 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个节点, 20维特征
edge_index=[[0, 0, 1, 3],
[1, 2, 2, 1]] # 4个无向边
)
print("Graph Desc: \n", graph)
graph.convert_edge_to_directed() # 预处理边数据,将无向边表示转换为有向边表示
print("Processed Graph Desc: \n", graph)
print("Processed Edge Index:\n", graph.edge_index)
# 多头图注意力网络(Multi-head 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)
输出:
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)
强烈建议您通过下面的示例代码来快速入门tf_geometric:
- 图卷积网络 Graph Convolutional Network (GCN)
- 多头图注意力网络 Multi-head Graph Attention Network (GAT)
- Approximate Personalized Propagation of Neural Predictions (APPNP)
- Inductive Representation Learning on Large Graphs (GraphSAGE)
- 切比雪夫网络 Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering (ChebyNet)
- Simple Graph Convolution (SGC)
- Topology Adaptive Graph Convolutional Network (TAGCN)
- Deep Graph Infomax (DGI)
- DropEdge: Towards Deep Graph Convolutional Networks on Node Classification (DropEdge)
- 基于图卷积网络的文本分类 Graph Convolutional Networks for Text Classification (TextGCN)
- Simple Spectral Graph Convolution (SSGC/S^2GC)
- 平均池化 MeanPooling
- Graph Isomorphism Network (GIN)
- 自注意力图池化 Self-Attention Graph Pooling (SAGPooling)
- 可微池化 Hierarchical Graph Representation Learning with Differentiable Pooling (DiffPool)
- Order Matters: Sequence to Sequence for Sets (Set2Set)
- ASAP: Adaptive Structure Aware Pooling for Learning Hierarchical Graph Representations (ASAP)
- An End-to-End Deep Learning Architecture for Graph Classification (SortPool)
- 最小割池化 Spectral Clustering with Graph Neural Networks for Graph Pooling (MinCutPool)
申明: 本文中部分文字、案例源于官网,将在后期的更新中不断丰富文中内容以及本文链接所指向的相关文章,如果侵犯了您的权益,可以联系我微.信(mtyjkh_)。