"CNN 已老, GNN 当立!"
当科学家们发现, 图神经网络 (GNN) 能搞定传统 CNN 处理不了的非欧数据, 从前深度学习解不开的许多问题都找到了钥匙.
如今, 有个图网络 PyTorch 库, 已在 GitHub 摘下 2000 多星, 还被 CNN 的爸爸 Yann LeCun 翻了牌:
它叫 PyTorch Geometric, 简称 PyG, 聚集了 26 项图网络研究的代码实现.
这个库还很快, 比起前辈 DGL 图网络库, PyG 最高可以达到它的 15 倍速度.
应有尽有的库
要跑结构不规则的数据, 就用 PyG 吧. 不管是图形 (Graphs), 点云 (Point Clouds) 还是流形(Manifolds) .
△ 右边是不规则的, 非欧空间
这是一个丰盛的库: 许多模型的 PyTorch 实现, 各种有用的转换 (Transforms) , 以及大量常见的 benchmark 数据集, 应有尽有.
说到实现, 包括 Kipf 等人的图卷积网络 (GCN) 和 Bengio 实验室的图注意力网络 (GAT) 在内, 2017-2019 年各大顶会的 (至少) 26 项图网络研究, 这里都能找到快速实现.
到底能多快? PyG 的两位作者用英伟达 GTX 1080Ti 做了实验.
对手 DGL, 也是图网络库:
在四个数据集里, PyG 全部比 DGL 跑得快. 最悬殊的一场比赛, 是在 Cora 数据集上运行 GAT 模型: 跑 200 个 epoch, 对手耗时 33.4 秒, PyG 只要 2.2 秒, 相当于对方速度的 15 倍.
每个算法的实现, 都支持了 CPU 计算和 GPU 计算.
食用方法
库的作者, 是两位德国少年, 来自多特蒙德工业大学.
△ 其中一位
他们说, 有了 PyG, 做起图网络就像一阵微风.
你看, 实现一个边缘卷积层 (Edge Convolution Layer) 只要这样而已:
- import torch
- from torch.nn import Sequential as Seq, Linear as Lin, ReLU
- from torch_geometric.nn import MessagePassing
- class EdgeConv(MessagePassing):
- def __init__(self, F_in, F_out):
- super(EdgeConv, self).__init__()
- self.mlp = Seq(Lin(2 * F_in, F_out), ReLU(), Lin(F_out, F_out))
- def forward(self, x, edge_index):
- # x has shape [N, F_in]
- # edge_index has shape [2, E]
- return self.propagate(aggr='max', edge_index=edge_index, x=x) # shape [N, F_out]
- def message(self, x_i, x_j):
- # x_i has shape [E, F_in]
- # x_j has shape [E, F_in]
- edge_features = torch.cat([x_i, x_j - x_i], dim=1) # shape [E, 2 * F_in]
- return self.mlp(edge_features) # shape [E, F_out]
安装之前确认一下, 至少要有 PyTorch 1.0.0; 再确认一下 cuda/bin 在 $PATH 里, cuda/include 在 $CPATH 里:
$ python -c "import torch; print(torch.__version__)" >>> 1.0.0 $ echo $PATH >>> /usr/local/cuda/bin:... $ echo $CPATH >>> /usr/local/cuda/include:...
然后, 就开始各种 pip install 吧.
PyG 项目传送门:
https://github.com/rusty1s/pytorch_geometric
PyG 主页传送门:
https://rusty1s.github.io/pytorch_geometric/build/html/index.html
PyG 论文传送门:
https://arxiv.org/pdf/1903.02428.pdf
来源: http://news.51cto.com/art/201903/593411.htm