PyG安装
1.GPU安装
pyg是torch-geometric的简称,类似于numpy,pyg已经成为了图神经网络的一大杀器。安装pyg需要预先安装torch,如需使用GPU,则还需要安装cuda。这两者的安装很简单,不再赘述。初学者建议只安装cpu版本,会简单一些。最好再安装一个Anaconda来管理虚拟环境。
我的演示环境:win10+conda+notebook,显卡是MX150
Linux下则默认你已经会安装了。
相关URL:
cuda官方网站
torch官方网站
pyg官方文档
1.1查看torch,CUDA版本
#$表示窗口命令
$ python -c "import torch; print(torch.__version__)"
>>> 1.6.0
$ python -c "import torch; print(torch.version.cuda)"
>>> 10.2
1.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
如果你的版本是cuda92+torch1.5.0,则你应该使用如下安装语句:
$ pip install torch-scatter==latest+cu92 -f https://pytorch-geometric.com/whl/torch-1.5.0.html
其它的以此推类。如果遇到找不到对应包的错误,则建议换一个torch或者cuda的版本。
我一开始是cuda92,但是安装后出现了问题。推荐版本:cuda:10.2 torch:1.5/1.6
2.CPU安装(推荐新手)
若使用CPU安装,则需要安装cpu版本的pytorch,为了不破坏原有环境,建议新建一个虚拟环境。
2.1 新建虚拟环境(若是初次安装,此步骤也可跳过)
$ conda create -n pyg_cpu
#激活虚拟环境
$ conda activate pyg_cpu
2.2 安装cpu版torch并查看版本
$ conda install pytorch torchvision cpuonly -c pytorch
$ python -c "import torch; print(torch.__version__)"
>>> 1.6.0
2.3 根据版本安装支持库
pip install torch-scatter==latest+cpu -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-sparse==latest+cpu -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-cluster==latest+cpu -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-spline-conv==latest+cpu -f https://pytorch-geometric.com/whl/torch-1.6.0.html
pip install torch-geometric
3.检查安装结果
import torch
import torch_geometric
若无报错,则说明安装成功,若有报错,首先检查一下安装步骤,是否做到版本一致。
另外cuda的版本有可能显示的不一定是真实安装的版本。建议将cuda全部删除,然后再安装一个新的。