安装Paddle和Pytorch深度学习的配置问题

关键词:换源问题、清华源、Paddle、Pytorch、Anaconda

参考:https://blog.csdn/didiaopao/article/details/119787139
Anaconda网址:https://www.anaconda/products/distribution#Downloads

首先,我们要安装好Anaconda软件,安装Anaconda的过程,可以看参考文章来完成,这篇博客的安装过程是比较详细。这里要注意,Anaconda软件尽量安装在D盘或者非系统盘,因为一个深度学习环境就比较大。我在我的电脑上配置了pytorch、paddle、torch三个环境,总大小为35GB。

Pytorch环境安装

此过程可参考:https://blog.csdn/didiaopao/article/details/119787139中的Pytroch环境安装部分
安装好Anaconda后,进行Pytorch环境安装,在开始菜单中打开Anaconda Prompt,在这里进行创建环境。
创建命令:conda create -n 环境名(英文) python=x.x(版本)
例如创建一个名为pytorch的环境,并且python版本为3.9。

conda create -n pytorch python=3.9

创建环境这条语句在base环境下执行,执行后,如果顺利就会给新环境安装一些基础的包。(该过程参考链接文章)。

报错Collecting package metadata (current_repodata.json): failed······json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 41 (char 40)

如果报这个错误,可以参考:https://blog.csdn/weixin_43571647/article/details/127179442

并重新执行:

conda create -n pytorch python=3.9

换源

如果没有gpu,是否可以安装gpu?应该是可以安装,但是使用会受到限制,可能会报错。
安装pytorch-gup版的环境,由于pytorch的官网在国外,下载相关的环境包是比较慢的,所以需要还原。
上面的参考文章(https://blog.csdn/didiaopao/article/details/119787139)中使用如下办法来进行还原:

conda config --add channels https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes

如果,按照这种方式进行换源后能够正常安装pytorch,那么就没有关系。
我曾使用这种方式,并成功安装Pytorch。但是后来我在另一台电脑上安装时,这种方式就没有成功。
如果你遇到报错:
PackagesNotFoundError: The following packages are not available from current channels
或者:

Collecting package metadata (current_repodata.json): failed

UnavailableInvalidChannel: The channel is not accessible or is invalid.
  channel name:
  channel url: https://mirrors.tuna.tsinghua.edu
  error code: 404

You will need to adjust your conda configuration to proceed.
Use `conda config --show channels` to view your configuration's current state,
and use `conda config --show-sources` to view config file locations.

或者:

Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.


PackagesNotFoundError: The following packages are not available from current channels:

  - igraph

Current channels:

  - https://repo.anaconda/pkgs/main/win-64
  - https://repo.anaconda/pkgs/main/noarch
  - https://repo.anaconda/pkgs/r/win-64
  - https://repo.anaconda/pkgs/r/noarch
  - https://repo.anaconda/pkgs/msys2/win-64
  - https://repo.anaconda/pkgs/msys2/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda

and use the search bar at the top of the page.

总之类似这样的报错,说明上面的换源并不是很成功。可以采用以下的换源方式。
1)查找.condrac文件的位置
通常.condrac文件在C盘/User(用户)/Username(用户名)这个目录下,注意这是一个隐藏文件,所以你要在资源管理器中开启查看隐藏文件。

如果上述方法没有找到.condrac文件,也可以使用命令conda config --show-sources来查看文件位置。
找到该文件后,点击查看,并在删掉里面的内容写入以下内容:

channels:
  - defaults
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu/anaconda/cloud
  paddle: https://mirrors.tuna.tsinghua.edu/anaconda/cloud
  
default_channels:
  - https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/msys2
show_channel_urls: True

然后,我们去安装Pytorch(官网https://pytorch/)

conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge

这段命令不需要做任何改变,直接输入到Anaconda Prompt中即可。这时候,应该就是从清华源镜像进行下载,而不需要从外网进行下载了。
这里,其原理我推测是这样的:如果我们不像上述那样对源进行配置,对于命令中-c pytorch -c conda-forge会从外网下载,原因是Anaconda自己有默认的https://anaconda网站,其中就包括pytroch和conda-forge这几个目录,如果没有写清楚源,会从默认的源进行下载。而我们像上述的方式进行换源后,就告诉程序,对于conda-forge参数,要到https://mirrors.tuna.tsinghua.edu/anaconda/cloud进行寻找并下载,对于pytorch也是一样。

Paddle环境安装

和Pytorch环境安装一样,我们也需要创建一个新的环境来进行安装。conda create -n paddle python=3.9,创建好后进入到这个环境conda activate paddle。然后我们进入paddle官网(https://www.paddlepaddle/)选择对应的cuda版本,并输入相应的安装命令即可。
conda install paddlepaddle-gpu==2.3.2 cudatoolkit=11.6 -c https://mirrors.tuna.tsinghua.edu/anaconda/cloud/Paddle/ -c conda-forge

介绍一些其他知识

查看虚拟环境列表
可以通过cmd或Anaconda Prompt来查看虚拟环境列表:

conda env list
conda info --envs

删除环境

conda remove -n 环境名(名字) --all

解释:remove表示删除,-n是name的缩写,–all,是删除环境下的所有库或包,加上–all才是删除一整个环境。

更多推荐

安装Paddle和Pytorch深度学习的配置问题