一、查看 cuda 版本号
打开终端,输入 nvidia-smi,查看系统的 cuda 版本号,命令如下:
nvidia-smi
如图所示,该 cuda 版本号是12.2:

二、查看 pytorch 版本
首先激活虚拟环境,命令如下:
# xxx 是虚拟环境名
conda activate xxx
然后在终端输入命令进行查看 pytorch 的版本,命令如下:
python -c "import torch; print(torch.__version__, torch.version.cuda)"
注意:pytorch 的版本需要与 cuda 版本号一致,如何安装 pytorch 可以查看之前的帖子
如图所示,该环境是 torch 1.10.1 + cu113:

三、安装 torch_geometric
可查看官网安装指引,步骤分别如下:
1. 卸载之前的 PyG 相关包,防止冲突(可选)
如果之前安装过相关包,可先进行卸载,命令如下:
pip uninstall -y pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv torch_geometric
2. 安装底层依赖
安装 torch_geometric 前,首先需要安装相关的依赖包
(1)在线安装
下面官网安装命令(其中 ${TORCH} 和 ${CUDA} 需要替换为相应的 pytorch 版本号 和 cuda 版本号)任选其一进行安装即可:
官网安装命令一:data.pyg.org 官方域名,安装命令如下所示:
# ${TORCH} 替换为 pytorch 版本号
# ${CUDA} 替换为 cuda 版本号
pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-${TORCH}+${CUDA}.html
官网安装命令二:PyG 官方也把下载链接搬到了 pytorch-geometric.com 域名下,安装命令如下所示:
# ${TORCH} 替换为 pytorch 版本号
# ${CUDA} 替换为 cuda 版本号
pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://pytorch-geometric.com/whl/torch-${TORCH}+${CUDA}.html
例如,我的 pytorch 版本号是 torch 1.10.1,cuda 版本号是 cu113,则官网安装命令一如下所示:
# pytorch 版本号 torch 1.10.1
# cuda 版本号 cu113
pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-1.10.1+cu113.html
官网安装命令二如下所示:
# pytorch 版本号 torch 1.10.1
# cuda 版本号 cu113
pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://pytorch-geometric.com/whl/torch-1.10.1+cu113.html
注意:上面官网安装命令,可能会显示网络不可达错误
官网安装过程中可能会出现网络不可达的错误(改为方法2 本地安装),如图所示:

(2)本地安装
点击打开安装命令中的网址(二选一)或者点击打开官网网址选择对应版本,打开网页,根据自己的服务器和虚拟环境对应的 python 版本选择对应的 .whl 文件进行下载:


注意:其中 cp3x 表示 python 版本 3.x。例如我的服务器是 linux,虚拟环境 python 版本是 3.8,则所需下载的 .whl 文件如图所示:

然后将下载的 .whl 文件上传到服务器,进入上传目录下,依次进行安装,命令如下:
# ... 是所下载的 .whl 文件版本
pip install pyg_lib-...-cp38-cp38-linux_x86_64.whl
pip install torch_scatter-...-cp38-cp38-linux_x86_64.whl
pip install torch_sparse-...-cp38-cp38-linux_x86_64.whl
pip install torch_cluster-...-cp38-cp38-linux_x86_64.whl
pip install torch_spline_conv-...-cp38-cp38-linux_x86_64.whl
例如,我所需下载的 .whl 文件的安装命令如下:
# 刚所下载的环境对应的 .whl 文件
pip install torch_scatter-2.0.9-cp38-cp38-linux_x86_64.whl
pip install torch_sparse-0.6.13-cp38-cp38-linux_x86_64.whl
pip install torch_cluster-1.6.0-cp38-cp38-linux_x86_64.whl
pip install torch_spline_conv-1.2.1-cp38-cp38-linux_x86_64.whl
(3)安装 torch_geometric
最后安装主包,命令如下:
pip install torch-geometric
如果上面的官网安装命令 显示网络连接超时,则可以改成清华源进行安装,命令如下:
pip install -U -i https://pypi.tuna.tsinghua.edu.cn/simple torch_geometric
如果清华源也显示网络不可达,则尝试直接从 GitHub 上把 torch_geometric 源码拉下来,然后在本地安装,不再经过 PyPI,命令如下:
# 1. 先确保在激活的虚拟环境里
which python # 应该指向所激活的虚拟环境
# 2. 克隆 PyG 的主仓库
git clone https://github.com/pyg-team/pytorch_geometric.git
cd pytorch_geometric
# 3. 安装(使用 --no-deps,避免再去 PyPI 安装已经装好的依赖)
pip install --no-dependencies .

5077

被折叠的 条评论
为什么被折叠?



