本文已参与「新人创作礼」活动,一起开启掘金创作之路。
一、参考资料
PyTorch 中文手册(pytorch handbook)
二、重要说明
- 注意事项:pytorch版本,CUDA版本,python版本要匹配
- 查看 pytorch 版本匹配 PyTorch Version
- 查看 torchvision 版本匹配 Torchvision Version
- 查到对应版本之后,在清华源镜像下载对应的安装包 清华源镜像 其他镜像
- 本地安装whl安装包
博主的环境
windows10
64位系统
cuda11.1
python3.8
下载对应的 pytorch、torchvision 版本
其他镜像
torch-1.8.1+cu111-cp38-cp38-win_amd64.whl
torchvision-0.9.1+cu111-cp38-cp38-win_amd64.whl
清华源镜像
pytorch-1.7.1-py3.7_cuda11.0.221_cudnn8.0.5_0.tar.bz2
torchvision-0.8.0-py37_cu110.tar.bz2
三、关键步骤
- 安装CPU版本的pytorch
安装cpu版本的pytorch
pip install torch
pip install torchvision
卸载
pip uninstall torch
pip uninstall torchvision
- 本地安装GPU版本的pytorch
本地安装GPU版本的pytorch
安装其他镜像
pip install torch-1.8.1+cu111-cp38-cp38-win_amd64.whl
pip install torchvision-0.9.1+cu111-cp38-cp38-win_amd64.whl
或者
安装清华源镜像
conda install --offline pytorch-1.7.1-py3.7_cuda11.0.221_cudnn8.0.5_0.tar.bz2
conda install --offline torchvision-0.8.0-py37_cu110.tar.bz2
- 测试是否安装成功
测试是否安装成功
import torch
print(torch.cuda.is_available())
device = torch.device("cuda:0" if (torch.cuda.is_available() and True > 0) else "cpu")
print(torch.cuda.get_device_name(0))
四、pytorch支持的gpu算力
import torch
torch.cuda.get_arch_list()
# 输出
['sm_37', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'compute_37']
五、可能出现的问题
- “libmkl_intel_lp64.so 不能被加载”的错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/yichao/miniconda3/envs/tensorRT-pytorch/lib/python3.7/site-packages/torch/__init__.py", line 189, in <module>
_load_global_deps()
File "/home/yichao/miniconda3/envs/tensorRT-pytorch/lib/python3.7/site-packages/torch/__init__.py", line 142, in _load_global_deps
ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
File "/home/yichao/miniconda3/envs/tensorRT-pytorch/lib/python3.7/ctypes/__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libmkl_intel_lp64.so: cannot open shared object file: No such file or directory
解决办法:
方法一:
[libmkl_intel_lp64.so](https://www.cnblogs.com/denny402/p/10848506.html)
# 1. 搜索路径
sudo find /home -name libmkl_intel_lp64.so
显示路径为:
/home/yichao/anaconda3/lib/libmkl_intel_lp64.so
# 2. 添加环境变量
sudo gedit ~/.bashrc
# 2.1 添加一行
export LD_LIBRARY_PATH=/home/yichao/anaconda3/lib:$LD_LIBRARY_PATH
# 3. 更新配置,显示配置
source ~/.bashrc
echo $LD_LIBRARY_PATH
注意:如果是其他的动态链接库文件也找不到,解决方法是一样的
方法二:
如果方法一没有找到 libmkl_intel_lp64.so 文件,尝试用方法二
[Python在Ubuntu下老是报libmkl_intel_lp64.so 不能被加载的解决办法](https://blog.csdn.net/qq_22704577/article/details/53928059)
# 1. debug一下mkl
conda install --debug mkl
# 2. 重新安装mkl包
conda install mkl
- torchvision 版本错误
RuntimeError: Couldn't load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible, or if you had errors while compiling torchvision from source. For further information on the compatible versions, check https://github.com/pytorch/vision#installation for the compatibility matrix. Please check your PyTorch version with torch.__version__ and your torchvision version with torchvision.__version__ and verify if they are compatible, and if not please reinstall torchvision so that it matches your PyTorch install.
错误原因:
pytorch版本与torchvision版本不匹配
解决办法:
torchvision版本对齐
[torchvision](https://github.com/pytorch/vision#installation)