使用python库pytorch-geometric(pyG)进行图神经网络的构建,该库以pytorch为基础。
因此,搭建pytorch+pyG+jupyter notebook的实验环境。
提前安装好anaconda和jupyter notebook。
一、用anaconda创建一个虚拟环境
conda create --name=pyg python=3.8
conda activate pyg
二、下载pytorch相关库
pip下载对应版本的pytorch和cuda,参考官方网站Start Locally | PyTorch;需要查看电脑显卡的cuda版本,选择<=的cuda版本。
# CUDA 10.2
conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cudatoolkit=10.2 -c pytorch
# CPU Only
conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cpuonly -c pytorch
在命令行中测试,不报错:
python
import torch
torch.cuda.is_available()
三、下载pyG相关库
下载pytorch-geometric,在https://data.pyg.org/whl/中选择torch-1.10.0+cu102版本,根据不同配置下载相应的四个包,cp38是python3.8的意思
切换目录到文件位置,pip安装
pip install torch_cluster-1.5.9-cp38-cp38-win_amd64.whl
pip install torch_scatter-2.0.9-cp38-cp38-win_amd64.whl
pip install torch_sparse-0.6.12-cp38-cp38-win_amd64.whl
pip install torch-geometric
打开jupyter notebook,进行测试:
jupyter notebook
import torch_geometric.nn
遇到错误:
解决方法:下载ipykernel包,把pyg环境在notebook中关联起来
conda install ipykernel
python -m ipykernel install --name pyg2 --display-name "pyg2"
切换内核为pyg2
不再报错,安装环境成功