安装pytorch_geometric

有耀
2023-12-01

前些时候了解了python下的 dgl库来进行图谱的计算, 最近看到pytorch_geometric 比dgl快很多。 于是打起了pytorch_geometric的主意, 然而pytorch_geometric 并没有dgl 安装这么方便。 大体思路就是 git源码, 编译源码, 安装, 测试。


我来先吧坑填了
第一个坑不填,会报如下错误:

ImportError while importing test module './pytorch_geometric/test/utils/test_to_dense_batch.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/utils/test_to_dense_batch.py:2: in <module>
    from torch_geometric.utils import to_dense_batch
torch_geometric/utils/__init__.py:2: in <module>
    from .scatter import scatter_
torch_geometric/utils/scatter.py:1: in <module>
    import torch_scatter
E   ModuleNotFoundError: No module named 'torch_scatter'

填空参考这个博客No module named ‘torch_scatter’, 解决方案如下:

pip install --verbose --no-cache-dir torch-scatter  torch-sparse torch-cluster torch-spline-conv 
pip install torch-geometric

第二个坑不填,会报下面错误:

ImportError while importing test module './pytorch_geometric/test/visualization/test_influence.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/visualization/test_influence.py:2: in <module>
    from torch_geometric.datasets import KarateClub
torch_geometric/datasets/__init__.py:1: in <module>
    from .karate import KarateClub
torch_geometric/datasets/karate.py:3: in <module>
    from torch_geometric.data import InMemoryDataset, Data
torch_geometric/data/__init__.py:1: in <module>
    from .data import Data
torch_geometric/data/data.py:5: in <module>
    from torch_geometric.utils import (contains_isolated_nodes,
torch_geometric/utils/__init__.py:2: in <module>
    from .scatter import scatter_
torch_geometric/utils/scatter.py:1: in <module>
    import torch_scatter
/home/lhpc04/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch_scatter/__init__.py:3: in <module>
    from .mul import scatter_mul
/home/lhpc04/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch_scatter/mul.py:3: in <module>
    from torch_scatter.utils.ext import get_func
/home/lhpc04/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch_scatter/utils/ext.py:5: in <module>
    import torch_scatter.scatter_cuda
E   ImportError: ~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch_scatter/scatter_cuda.cpython-36m-x86_64-linux-gnu.so: undefined symbol: __cudaPopCallConfiguration

其实也是我自己挖的坑,自己跳的那种。

# 输入命令:
$ conda install pytorch torchvision cudatoolkit=9.0 -c pytorch

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    pytorch-1.1.0              |py3.7_cuda9.0.176_cudnn7.5.1_0       376.8 MB  pytorch
    ------------------------------------------------------------
                                           Total:       376.8 MB

目前官方给出了两个pytorch 的cuda版本, 一个cuda9.0, 一个cuda10.0, 我的系统上的一直是9.2, 保存情况如上, cuda中的符号未定义。不是大问题,但是耗时间,我反正是将cuda9.0下载下来重装了, 然后就解决了。

好像还有一个cuda 报的requests的错误

RemoveError: 'requests' is a dependency of conda and cannot be removed from conda's operating environment

解决方法:

conda install conda

好了,现在就可以看看怎么安装了。

  1. 如果想不影响其他的环境,可以用anaconda create一个独立的环境。

  2. 安装pytorch

#安装
conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
#测试下
python -c "import torch; print(torch.__version__)"
# 我的输出为:
# 	1.1.0
  1. git pytorch_geometric
git clone git@github.com:rusty1s/pytorch_geometric.git
  1. 编译和测试
# 环境
pip install h5py networkx pandas plyfile rdflib scikit-learn scipy
pip install mock pytest-cov
# 安装
python setup.py install
# 测试
python setup.py test

更新pyg的时候,也需要相应的pytorch,会引发一系列的以来pytorch的包的版本问题,解决方法是先将这些包卸载了,安装最新的版本:

# 假设pytorch版本是最新的了,要更新其他依赖库
pip uninstall  torch-scatter  torch-sparse torch-cluster torch-spline-conv 
pip install --verbose --no-cache-dir torch-scatter  torch-sparse torch-cluster torch-spline-conv 
pip install torch-geometric
 类似资料: