从代码角度深入浅出图神经网络系列笔记(一)

前言

整个系列笔记来源于up主思凡的视频,链接如下:

https://space.bilibili.com/630192628

整个笔记系列目的是为了将从视频收获的知识点整理成文本,方便随时查取,部分地方文字描述不清的地方,请配合视频理解。部分地方也补充了一些我的理解,欢迎大家指正交流

开发环境

1、pytorch的安装

开发环境主要是从 链 接 直 达 链接直达 进行选择,截图如下:
在这里插入图片描述
根据你的电脑环境,点击不同的配置,获取安装命令即可。mac的本子如果没有英伟达的显卡,是无法完成安装的,有的话虽然可以安装,但是配置起来比较麻烦,建议还是windows最好。
在这里插入图片描述
补充一个参数:

--proxy=http://代理ip:端口

要不然一个G的文件要下载好久好久

2、安装相关库

https://pytorch-geometric.readthedocs.io/en/latest/notes/installation.html

在这里插入图片描述
我的环境是PyTorch 1.6.0CUDA 10.2,安装命令如下:

pip install torch-scatter==latest+cu102 -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-sparse==latest+cu102 -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-cluster==latest+cu102 -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-spline-conv==latest+cu102 -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-geometric

实例解读

1、Data类

PARAMETERS

  • x (Tensor, optional) – Node feature matrix with shape [num_nodes, num_node_features]. (default: None) //节点的特征矩阵

  • edge_index (LongTensor, optional) – Graph connectivity in COO format with shape [2, num_edges]. (default: None) //COO格式的长张量表示邻接矩阵

  • edge_attr (Tensor, optional) – Edge feature matrix with shape [num_edges, num_edge_features]. (default: None) //边的属性

  • y (Tensor, optional) – Graph or node targets with arbitrary shape. (default: None) //节点的分类表示

  • pos (Tensor, optional) – Node position matrix with shape [num_nodes, num_dimensions]. (default: None) //特征矩阵

  • norm (Tensor, optional) – Normal vector matrix with shape [num_nodes, num_dimensions]. (default: None)

  • face (LongTensor, optional) – Face adjacency matrix with shape [3, num_faces]. (default: None) //邻接矩阵

ps:这块看视频感觉还是有点小问题,下面几个参数感觉解释的不是很清楚,暂时也没查到什么例子,之后用到了再过来改这个笔记吧

2、实例代码

在这里插入图片描述

import torch
from torch_geometric.data import Data # 首先导入库函数

x = torch.tensor([[2,1],[5,6],[3,7],[12,0]], dtype=torch.float) # 节点值的矩阵 顺序都是按照ABCD排序的

y = torch.tensor([0,1,0,1], dtype=torch.float) # 类型的分类

edge_index = torch.tensor([[0,0,1,1,3],
                          [1,3,0,2,2]], dtype=torch.long) # 邻接矩阵来描述图的信息 COO格式

# edge_attr = [] 这个特征矩阵不需要定义,因为边没有属性

data = Data(x=x, y=y, edge_index=edge_index)

例子补充

Hands-on Graph Neural Networks with PyTorch & PyTorch Geometric
原文现在打不开了,看微博上有人保存了pdf,大家也可以下载来看,国内也有不少翻译的,不过最好还是看原文吧,有助于理解

链接:https://pan.baidu.com/s/1dMRV7SDchTX6C3vetf3uRw 
提取码:3sfd

该文章对于Data类举的例子就是up主用到的例子,所以这里我没有继续去翻译解读了

猜你喜欢

转载自blog.csdn.net/wy_97/article/details/108489943