conda 安装pytorch

本文详细介绍了如何为PyTorch配置合适的CUDA版本,包括修改conda源,创建虚拟环境,以及解决因版本不匹配导致的错误。同时,通过升级torchvision解决了与CUDA版本兼容的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先为conda 添加源

编辑home目录下的.condarc文件

如果是windows, 在C:\Users\Administrator

新建一个.conda文件夹,在.conda文件夹新建.condarc文件

或在C:\Users\Administrator这个目录下直接新建.condarc

可以都弄

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true
remote_read_timeout_secs: 600.0

注意要把https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

写在第一行,因为像https://mirrors.ustc.edu.cn/anaconda/pkgs/main/这个链接或者其他链接

也有pytorch,但有时候会中断,所以把最稳定最快的链接写前面,因为你不写在前面conda在https://mirrors.ustc.edu.cn/anaconda/pkgs/main/这个链接搜索到了pytorch,就不会去https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/这个链接搜,导致中断

换好源之后,然后最好先建一个虚拟环境

conda create -n torch1.2 python=3.6

再执行:

conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0

 

相装1.1的话

conda install pytorch==1.1.0 torchvision==0.3.0 cudatoolkit=9.0

它会自动把其他所有依赖比如cudnn也装,记住后面不要加-c pytorch,要不然就不会走中科大的源

这种用conda装pytorch,就不用手动装cuda,

  中科大这两句也要加,因为cudatoolkit我下载的时候都是在中科大这两个源下载的

- https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/

 

 

然后我又想改装pytorch 1.3.1

我就直接conda install pytorch==1.3.1

后续测试

In [1]: import torch                                                                                                             

In [2]: torch.__version__                                                                                                        
Out[2]: '1.3.1'

In [3]:                                                                                                                          

In [3]: torch.cuda.is_available()                                                                                                
Out[3]: True

下面是装的时候自动装的一些包:

(pytorch) wosai@wosai:~$ conda list
# packages in environment at /home/wosai/anaconda3/envs/pytorch:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
backcall                  0.1.0                     <pip>
blas                      1.0                         mkl    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
certifi                   2016.2.28                py36_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
cffi                      1.10.0                   py36_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
cudatoolkit               10.0.130                      0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
decorator                 4.4.1                     <pip>
freetype                  2.5.5                         2    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
intel-openmp              2019.4                      243    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
ipython                   7.10.1                    <pip>
ipython-genutils          0.2.0                     <pip>
jbig                      2.1                           0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
jedi                      0.15.1                    <pip>
jpeg                      9b                            0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
libffi                    3.2.1                         1    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
libgcc-ng                 9.1.0                hdf63c60_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
libgfortran-ng            7.3.0                hdf63c60_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
libpng                    1.6.30                        1    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
libstdcxx-ng              9.1.0                hdf63c60_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
libtiff                   4.0.6                         3    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
mkl                       2019.4                      243    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
mkl-service               2.3.0            py36he904b0f_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
mkl_fft                   1.0.15           py36ha843d7b_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
mkl_random                1.1.0            py36hd6b4f25_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
ninja                     1.7.2                         0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
numpy                     1.17.4           py36hc1035e2_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
numpy-base                1.17.4           py36hde5b4d6_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/main
olefile                   0.44                     py36_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
openssl                   1.0.2l                        0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
parso                     0.5.1                     <pip>
pexpect                   4.7.0                     <pip>
pickleshare               0.7.5                     <pip>
pillow                    4.2.1                    py36_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
pip                       9.0.1                    py36_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
prompt-toolkit            3.0.2                     <pip>
ptyprocess                0.6.0                     <pip>
pycparser                 2.18                     py36_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
Pygments                  2.5.2                     <pip>
python                    3.6.2                         0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
pytorch                   1.3.1           py3.6_cuda10.0.130_cudnn7.6.3_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
readline                  6.2                           2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
setuptools                36.4.0                   py36_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
six                       1.10.0                   py36_0    https://mirrors.ustc.edu.cn/anaconda/pkgs/free
sqlite                    3.13.0                        0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
tk                        8.5.18                        0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
traitlets                 4.3.3                     <pip>
wcwidth                   0.1.7                     <pip>
wheel                     0.29.0                   py36_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
xz                        5.2.3                         0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
zlib                      1.2.11                        0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

之后运行pytorch版的yolo3之后,报错:

python detect.py 
Traceback (most recent call last):
  File "detect.py", line 5, in <module>
    from utils.datasets import *
  File "/home/wosai/2020/PyTorch-YOLOv3/utils/datasets.py", line 12, in <module>
    import torchvision.transforms as transforms
  File "/home/wosai/anaconda3/envs/pytorch/lib/python3.6/site-packages/torchvision/__init__.py", line 1, in <module>
    from torchvision import models
  File "/home/wosai/anaconda3/envs/pytorch/lib/python3.6/site-packages/torchvision/models/__init__.py", line 11, in <module>
    from . import detection
  File "/home/wosai/anaconda3/envs/pytorch/lib/python3.6/site-packages/torchvision/models/detection/__init__.py", line 1, in <module>
    from .faster_rcnn import *
  File "/home/wosai/anaconda3/envs/pytorch/lib/python3.6/site-packages/torchvision/models/detection/faster_rcnn.py", line 7, in <module>
    from torchvision.ops import misc as misc_nn_ops
  File "/home/wosai/anaconda3/envs/pytorch/lib/python3.6/site-packages/torchvision/ops/__init__.py", line 1, in <module>
    from .boxes import nms, box_iou
  File "/home/wosai/anaconda3/envs/pytorch/lib/python3.6/site-packages/torchvision/ops/boxes.py", line 2, in <module>
    from torchvision import _C
ImportError: libcudart.so.9.0: cannot open shared object file: No such file or directory

竟然提示要cuda9,我明明我装了cuda10,测试了cuda是可用的,后来想想可能是torchvision版本的原因,因为我装的是torchvision0.3

于是pip install --upgrade torchvision

升级到了最新版本的0.4.2

再测试,成功!所以我在想是不是torch0.3要和cuda9对应,torchvision要和cuda10对应

ubuntu18对cuda10支持的比较好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值