pytorch的源在国外,下载速度会比较慢
为了加快安装速度,添加清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
或者可以使用下面命令打开condarc手动添加源
gedit ~/.condarc
结果
auto_activate_base: false
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- defaults
show_channel_urls: true
下一步是安装pytorch,首先到官网查看要求,和使用的安装命令
官方的命令是下面这个样子
conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch
-c pytorch 表示从pytorch源安装,这里执行命令的时候不要添加-c pytorch
运行下面的命令
conda install pytorch torchvision torchaudio cudatoolkit=11.0
这样就会自动安装了,会自动安装几个包,cuda工具包和pytorch包比较大,所以上面不要加 -c pytorch才会从清华源下载。
测试安装成功与否
import torch
print(torch.__version__) # 返回pytorch的版本
print(torch.cuda.is_available()) # 当CUDA可用时返回True