Anaconda使用清华源安装不同版本的Pytorch

Pytorch版本的选择

借鉴了这位博主的文章: 解决安装pytorch速度慢的问题.

1、 如果你要下载安装最新版本的Pytorch,可以在以下链接中使用命令进行安装。
传送门: Pytorch最新版本安装页面.

在这里插入图片描述

2、如果你要下载安装旧版本的Pytorch,你可以在以下连接中使用命令进行安装。
传送门: Pytorch过去版本安装页面.

在这里插入图片描述

在这里插入图片描述

设置清华源安装Pytorch

但是直接使用官网给的命令在anaconda虚拟环境中,会下载速度极其地慢。所以我们要设置清华源来避免这种情况发生。

第一步:我们先打开Anaconda中的虚拟环境。
1、 可以直接找到**Anaconda Navigator(Anaconda)**打开。

在这里插入图片描述
2、.稍等片刻,进入界面,选择虚拟环境。并选择Open Terminal,打开对应的虚拟环境的终端。
3、 另一种方法,也可以直接在win+r,cmd进入终端base环境。接着conda activate virtual_environment_name

扫描二维码关注公众号,回复: 14618644 查看本文章

第二步:我们首先查看当前channels并使用代码恢复conda的官方默认源,以免其他出错的channels影响我们的工作进行。

conda -V  # 安装完成后,查看conda版本
conda list  # 在当前的虚拟环境中查看安装了哪些包
conda env list  # 查看虚拟环境列表
conda create -n env_name python=3.8.10  # 创建虚拟环境并指定python版本,其中ns3_1为虚拟环境名称。
conda remove -n env_name --all  # 删除名称为env_name的虚拟环境
conda activate env_name  # 激活名为env_name的虚拟环境
conda deactivate  # 关闭当前虚拟环境
conda config --show channels  # 查看当前的channels
conda config --remove-key channels  # 恢复conda的官方默认源

第三步:设置channels
依次设置三条默认的清华源channels

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r

然后再设置清华镜像源关于pytorch的channels

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

第四步:找到从pytorch官网获取的command,将这条下载命令最后面的-c pytorch删除,-c pytorch参数指定了conda从pytorch的channel获取文件,所以在这里是不能用的。删除-c pytorch后,将命令输入命令行,直接回车。此时,软件就将从清华镜像源获取文件了,速度要快上很多。

conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cudatoolkit=10.2

验证我们安装的Pytorch并查看GPU\CPU个数

在我们虚拟环境的终端中输入python进入python环境,然后输入import torch 回车。如果不报错说明pytorch已经成功安装。若要想查看版本可以再输入print(torch._version_)。我们如果想要查看和我们的cuda版本和当前的pytorch版本是否适配,我们输入print(torch.cuda.is_available()),如果返回True,那么cuda版本和当前的pytorch版本适配,我们可以正常使用GPU加速!

python
import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.cuda.device_count())  # 查看GPU个数
print(os.cpu_count())  # 查看CPU个数

卸载我们安装的Pytorch

pip uninstall torch
pip uninstall torchvision
pip uninstall torchaudio

其他注意事项

1、目前,pytorch1.11.0版本和pytorch1.10.0版本已经不适用于cuda10.2版本。我们要想使用cuda10.2,需要使用pytorch1.9.1版本。

2、30系列英伟达显卡最低要求就是cuda11版本。而要使用cuda11版本,我们需要配套pytorch1.7.0以上版本的,但cuda11配合pytorch1.8有问题。

希望这篇文章可以有些帮助!

猜你喜欢

转载自blog.csdn.net/qq_53273581/article/details/123624718