conda安装pytorch(gpu)版本


前期工作

下载anaconda

conda的介绍

conda就是一个管理器,可以让你一台机器上同时有几个python的版本,而且可以让你轻松的切换你想要的版本。

你可以理解为conda是一个总的大文件管理器。

开始下载

添加下载源

打开anaconda prompt,在Windows右下角搜索栏搜到
然后先在conda里面创建一个新的环境(创建新环境之前先加下面几个语句)

(在创建新环境的时候需要下载你所需要的python版本,但是下载的话conda默认是从官网下载,但是官网下载会非常的慢。所以需要添加下载源,也就是国内的镜像

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/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ 
conda config --set show_channel_urls yes
// 可能加了https会出问题,要把https删掉
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ 
conda config --set show_channel_urls yes

清华源经常会挂掉,可以换成中科大源(另行搜索)

注意每次切换源之前都切要运行下面这句话先切回默认源在运行上面的设置源

conda config --remove-key channels

初始化环境

现在conda里面create一个新环境

conda create -n pytorch_gpu pip python=3.6
# -n 后面的是你取的名字,你也可以取其他名字,例如my_pytorch
# python=3.6 是你指定安装的python版本

然后activate这个新环境

activate pytorch_gpu
# 如果你取名了 my_pytorch
# 那就执行activate my_pytorch

开始安装

conda安装

先去官网查看自己需要的版本

可以选择操作系统(Windows,Linux,Mac)

然后选择是pip, conda还是其他的

然后选择是带有cuda的还是没有cuda的

带有cuda的就是有gpu的版本

如何查看cuda的版本呢?https://blog.csdn.net/qq_38295511/article/details/8922316

conda install pytorch torchvision torchaudio cudatoolkit=11.0
# 我是安装11.0的这个需要你自己选择

pip安装

pip --default-timeout=100 install tensorflow==2.0.0 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
# tensorflow为要安装的库,==2.0.0为指定版本

安装完成

安装完成后在conda里面输入python

然后import torch没有反应的话就是成功了

后续安装

安装完了之后,一般在jupterbook里面写代码,所以还要下载

conda install pandas
conda install jupyter notebook

然后到anaconda里面把base换成你自己create的那个虚拟环境

换成其他的虚拟环境

deactivate
activate other_envs

常见问题

PackagesNotFoundError: torchaudio torchvision

在下载源里面找不到你想要的版本,一般是下载源写的不详细。

越详细越好,比如说有些第三方的包只需要

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/

但是安装pytorch这两个不够,还需要加下面的

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

同理,如果还不够还需要继续加

CondaError: Downloaded bytes did not match Content-Length

如上所示,在用conda安装包的时候下载包的长度不够导致安装包不成功。原因一般是在下载的时候速度较慢,导致下载timeout而终止。解决办法如下。

conda config --set remote_read_timeout_secs 600.0

网上的解决方法是添加清华源的镜像,但我的已经是清华源镜像了,所以解决方法是把timeout放大,600还是不行,把时间延长到1000.0就成功安装了。命令如下:

conda config --set remote_read_timeout_secs 1000.0

参考网站

https://bingxiong.vip/?p=18438

conda remove -n nlp --all


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM