目录
一、安装cuda和cudnn
(1)什么是cuda
CUDA(ComputeUnified Device Architecture),是显卡厂商NVIDIA推出的运算平台。
CUDA是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。
(2) 什么是cudnn
NVIDIA cuDNN是用于深度神经网络的GPU加速库。它强调性能、易用性和低内存开销。NVIDIA cuDNN可以集成到更高级别的机器学习框架中,如谷歌的Tensorflow、加州大学伯克利分校的流行caffe软件。简单的插入式设计可以让开发人员专注于设计和实现神经网络模型,而不是简单调整性能,同时还可以在GPU上实现高性能现代并行计算。
具体安装方式,参考我的另一篇博客:CSDN
二、在线安装方式
根据cuda版本和python版本下载相对应的pytorch版本。
pip3 install torch==1.9.0+cu102 torchvision==0.10.0+cu102 torchaudio===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
conda命令:
1)由于官方下载太慢,需要更换为清华下载源:
# 添加Anaconda的清华镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
# 设置搜索时显示通道地址
config --set show_channel_urls yes
2)conda安装命令
conda install pytorch torchvision cudatoolkit=10.0
(ps: 如果网络没问题,是可以安装成功的,但是我失败了,所有只能选择离线安装方式)
三、离线安装pytorch
3.1 下载源介绍及安装
(1)官方下载源:
https://anaconda.org/pytorch/pytorch/files?sort=basename&sort_order=desc&version=1.5.0&type=conda
(2) 清华源
清华源pytorch: Index of /anaconda/cloud/pytorch/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
清华源cuda: Index of /anaconda/pkgs/free/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
(3)安装离线包
我从这里下的:Index of /anaconda/pkgs/main/win-64/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
下载了3个比较大的包
conda install --offline 包名
命令:
conda install pytorch-1.2.0-py3.6_cuda100_cudnn7_1.tar.bz2
conda install cudatoolkit-10.0.130-0.tar.bz2
conda install mkl-2021.3.0-haa95532_524.tar.bz2
安装完后可以重新执行一次:
conda install pytorch torchvision cudatoolkit=10.0
会将其他的包进行安装。
(4)其他方式补充
torch-1.2.0-cp36-cp36m-win_amd64.whl
torchvision-0.4.0-cp36-cp36m-win_amd64.whl
进入https://download.pytorch.org/whl/torch_stable.html
补充:
在使用python时经常可以发现某个lib有whl、tar、tar.gz等格式的包。
whl包:已经编译的包,类似于exe文件;
tar包:源文件,只是打包在一起,还没有编译;
tar.gz包:源文件,压缩并打包在一起,还没有编译。
如果环境挺充足,可以用tar包或者tar.gz包;
如果环境欠缺,比如缺少某些编译环境,或者想要快速且稳定,可以考虑whl包;
一般网络好的情况下,直接pip在线安装了… 但是有些时候没有编译环境,就得用whl包。
3.2 测试
进入python环境
import torch
torch.cuda.is_available()
返回结果为true,证明安装成功。