MindSpore图学习模块

技术背景

MindSpore Graph Learning是一个基于MindSpore的高效易用的图学习框架。得益于MindSpore的图算融合能力,MindSpore Graph Learning能够针对图模型特有的执行模式进行编译优化,帮助开发者缩短训练时间。 MindSpore Graph Learning 还创新提出了以点为中心编程范式,提供更原生的图神经网络表达方式,并内置覆盖了大部分应用场景的模型,使开发者能够轻松搭建图神经网络。

这是一个关于mindspore-gl的官方介绍,其定位非常接近于dgl,而且从文章(参考链接3)中的数据来看,mindspore-gl的运算效率还要高于dgl。

在传统的机器学习中,我们可以对各种Tensor进行高效的运算、卷积等。但是如果是一个图结构的网络,除了把图结构转换成Tensor数据,再对Tensor进行处理之外,有没有可能用一种更加便捷的运算方式,能够直接在图的基础上去计算呢?在这里mindSpore-gl也给出了自己的答案。我们可以一起来看一下mindspore-gl是如何安装和使用的。

mindspore-gl的安装

虽然官方有提供pip的安装方法,但是在库中能够提供的软件版本是非常有限的,这里我们推荐使用源码编译安装,这样也可以跟自己本地的MindSpore的版本更好的对应上。首先把仓库clone下来,并进入到graphlearning目录下:

$ git clone https://gitee.com/mindspore/graphlearning.git
正克隆到 'graphlearning'...

remote: Enumerating objects: 1275, done.

remote: Counting objects: 100% (221/221), done.

remote: Compressing objects: 100% (152/152), done.

remote: Total 1275 (delta 116), reused 127 (delta 68), pack-reused 1054
接收对象中: 100% (1275/1275), 1.41 MiB | 316.00 KiB/s, 完成.

处理 delta 中: 100% (715/715), 完成.

$ cd graphlearning/
$ ll
总用量 112
drwxrwxr-x 12 dechin dechin  4096 11月  9 17:19 ./
drwxrwxr-x 10 dechin dechin  4096 11月  9 17:19 ../
-rwxrwxr-x  1 dechin dechin  1429 11月  9 17:19 build.sh*
drwxrwxr-x  2 dechin dechin  4096 11月  9 17:19 examples/
-rwxrwxr-x  1 dechin dechin  3148 11月  9 17:19 FAQ_CN.md*
-rwxrwxr-x  1 dechin dechin  4148 11月  9 17:19 faq.md*
drwxrwxr-x  8 dechin dechin  4096 11月  9 17:19 .git/
-rwxrwxr-x  1 dechin dechin  1844 11月  9 17:19 .gitignore*
drwxrwxr-x  2 dechin dechin  4096 11月  9 17:19 images/
drwxrwxr-x  3 dechin dechin  4096 11月  9 17:19 .jenkins/
-rw-rw-r--  1 dechin dechin 11357 11月  9 17:19 LICENSE
drwxrwxr-x 11 dechin dechin  4096 11月  9 17:19 mindspore_gl/
drwxrwxr-x 11 dechin dechin  4096 11月  9 17:19 model_zoo/
-rwxrwxr-x  1 dechin dechin    52 11月  9 17:19 OWNERS*
-rwxrwxr-x  1 dechin dechin  3648 11月  9 17:19 README_CN.md*
-rwxrwxr-x  1 dechin dechin  4570 11月  9 17:19 README.md*
drwxrwxr-x  4 dechin dechin  4096 11月  9 17:19 recommendation/
-rwxrwxr-x  1 dechin dechin   922 11月  9 17:19 RELEASE.md*
-rwxrwxr-x  1 dechin dechin   108 11月  9 17:19 requirements.txt*
drwxrwxr-x  2 dechin dechin  4096 11月  9 17:19 scripts/
-rwxrwxr-x  1 dechin dechin  4164 11月  9 17:19 setup.py*
drwxrwxr-x  5 dechin dechin  4096 11月  9 17:19 tests/
drwxrwxr-x  5 dechin dechin  4096 11月  9 17:19 tools/

然后执行官方提供的编译构建的脚本:

`bash
$ bash build.sh
mkdir: 已创建目录 ‘/home/dechin/projects/mindspore/graphlearning/output’
Collecting Cython>=0.29.24
Downloading Cython-0.29.32-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (2.0 MB)
|████████████████████████████████| 2.0 MB 823 kB/s
Collecting ast-decompiler>=0.6.0
Downloading ast_decompiler-0.7.0-py3-none-any.whl (13 kB)
Collecting astpretty>=2.1.0
Downloading astpretty-3.0.0-py2.py3-none-any.whl (4.9 kB)
Collecting scikit-learn>=0.24.2
Downloading scikit_learn-1.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.8 MB)
|████████████████████████████████| 30.8 MB 2.6 MB/s
Requirement already satisfied: numpy>=1.21.2 in /home/dechin/anaconda3/envs/mindspore16/lib/python3.9/site-packages (from -r /home/dechin/projects/mindspore/graphlearning/requirements.txt (line 5)) (1.23.2)
Collecting networkx>=2.6.3
Downloading networkx-2.8.8-py3-none-any.whl (2.0 MB)
|████████████████████████████████| 2.0 MB 4.6 MB/s
Requirement already satisfied: scipy>=1.3.2 in /home/dechin/anaconda3/envs/mindspore16/lib/python3.9/site-packages (from scikit-learn>=0.24.2->-r /home/dechin/projects/mindspore/graphlearning/requirements.txt (line 4)) (1.5.3)
Collecting threadpoolctl>=2.0.0
Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Collecting joblib>=1.0.0
Downloading joblib-1.2.0-py3-none-any.whl (297 kB)
|████████████████████████████████| 297 kB 2.2 MB/s
Installing collected packages: threadpoolctl, joblib, scikit-learn, networkx, Cython, astpretty, ast-decompiler
Successfully installed Cython-0.29.32 ast-decompiler-0.7.0 astpretty-3.0.0 joblib-1.2.0 networkx-2.8.8 scikit-learn-1.1.3 threadpoolctl-3.1.0
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.9

removing build/bdist.linux-x86_64/wheel
mindspore_gl_gpu-0.1-cp39-cp39-linux_x86_64.whl
| def construct(self, x, y, g: Graph): 1 || 1 def construct( |
| || self, |
| || x, |
| || y, |
| || src_idx, |
| || dst_idx, |
| || n_nodes, |
| || n_edges, |
| || UNUSED_0=None, |
| || UNUSED_1=None, |
| || UNUSED_2=None |
| || ): |
| || 2 SCATTER_ADD = ms.ops.TensorScatterAdd() |
| || 3 SCATTER_MAX = ms.ops.TensorScatterMax() |
| || 4 SCATTER_MIN = ms.ops.TensorScatterMin() |
| || 5 GATHER = ms.ops.Gather() |
| || 6 ZEROS = ms.ops.Zeros() |
| || 7 FILL = ms.ops.Fill() |
| || 8 MASKED_FILL = ms.ops.MaskedFill() |
| || 9 IS_INF = ms.ops.IsInf() |
| || 10 SHAPE = ms.ops.Shape() |
| || 11 RESHAPE = ms.ops.Reshape() |
| || 12 scatter_src_idx = RESHAPE(src_idx, (SHAPE(src_idx)[0], 1)) |
| || 13 scatter_dst_idx = RESHAPE(dst_idx, (SHAPE(dst_idx)[0], 1)) |
| g.set_src_attr({‘hs’: x}) 2 || 14 hs, = [x] |
| g.set_dst_attr({‘hd’: y}) 3 || 15 hd, = [y] |
| return [v.hd for v in g.dst_vertex] * [u.hs for u in g.src_vertex] 4 || 16 return hd * hs |
| def construct(self, x, y, g: Graph): 1 || 1 def construct( |
| || self, |
| || x, |
| || y, |
| || src_idx, |
| || dst_idx, |
| || n_nodes, |
| || n_edges, |
| || UNUSED_0=None, |
| || UNUSED_1=None, |
| || UNUSED_2=None |
| || ): |
| || 2 SCATTER_ADD = ms.ops.TensorScatterAdd() |
| || 3 SCATTER_MAX = ms.ops.TensorScatterMax() |
| || 4 SCATTER_MIN = ms.ops.TensorScatterMin() |
| || 5 GATHER = ms.ops.Gather() |
| || 6 ZEROS = ms.ops.Zeros() |
| || 7 FILL = ms.ops.Fill() |
| || 8 MASKED_FILL = ms.ops.MaskedFill() |
| || 9 IS_INF = ms.ops.IsInf() |
| || 10 SHAPE = ms.ops.Shape() |
| || 11 RESHAPE = ms.ops.Reshape() |
| || 12 scatter_src_idx = RESHAPE(src_idx, (SHAPE(src_idx)[0], 1)) |
| || 13 scatter_dst_idx = RESHAPE(dst_idx, (SHAPE(dst_idx)[0], 1)) |
| g.set_src_attr({‘hs’: x}) 2 || 14 hs, = [x] |
| g.set_dst_attr({‘hd’: y}) 3 || 15 hd, = [y] |
| return [g.sum([u.hs for u in v.innbs]) for v in g.dst_vertex] 4 || 16 SCATTER_INPUT_SNAPSHOT1 = GATHER(hs, src_idx, 0) |
| || 17 return SCATTER_ADD( |
| || ZEROS( |
| || (n_nodes,) + SHAPE(SCATTER_INPUT_SNAPSHOT1)[1:], |
| || SCATTER_INPUT_SNAPSHOT1.dtype |
| || ), |
| || scatter_dst_idx, |
| || SCATTER_INPUT_SNAPSHOT1 |
| || ) |

Original: https://www.cnblogs.com/dechinphy/p/mindspore_gl.html
Author: DECHIN
Title: MindSpore图学习模块

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/713875/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球