上海制作网站的网站,网站建设的目的及目标,网站建设就选,个人建站平台请支持原创#xff0c;认准DannisTang#xff08;tangweixuan1995foxmail.com#xff09; 文章目录 第〇章 阅读前提示第一章 准备工作第一节 Python下载第二节 Python安装第三节 Python配置第四节 Pycharm下载第五节 Pycharm安装第六节 CUDA的安装 第二章 Anaconda安装与配…请支持原创认准DannisTangtangweixuan1995foxmail.com 文章目录 第〇章 阅读前提示第一章 准备工作第一节 Python下载第二节 Python安装第三节 Python配置第四节 Pycharm下载第五节 Pycharm安装第六节 CUDA的安装 第二章 Anaconda安装与配置第一节 Anaconda下载与环境变量配置1、Anaconda下载与安装2、Anaconda配置环境变量 第二节 Anaconda镜像源配置1、查看2、配置命令行1直接输入命令行配置2文件夹进行输入配置Windows版 3、配置界面配置4、删除命令行5、修改配置源Linux版6、常用命令1环境类2包类 第三节 Pytorch安装与配置1、创建Pytorch环境2、激活Pytorch环境3、关闭Pytorch环境可选4、找到pytorch命令5、验证pytorch安装附节一、报错的场景和方法1、创建环境报错2、创建pytorch报错3、下载时间超时4、报错信息 第四节 进入pycharm1、添加环境2、小试牛刀 第〇章 阅读前提示
本文重点放在深度学习上因此对于Python部分的内容会稍显不足。如果本文的读者想重点了解Python相关的知识请查看其他的文章。但是为了方便入门本文在前面依然会描述Python配置相关的内容以及一些需要用到的Python相关的命令等。
同时值得注意的是本文如果没有特殊说明均是在Windows平台下进行开发和调试等。
并且为了保证兼容性硬件设备即你的电脑主机最好为英特尔的CPU与英伟达的GPU通俗理解为显卡。
第一章 准备工作
本章将讲解深度学习的准备工作因为不是本文的主要目的所以不会花费很大的篇幅去讲解 如果在本章的过程中出现问题可以在网络中搜索下出现的问题。
本章讲解在windows下安装和配置python和pycharm以及英伟达的cuda的安装。
第一节 Python下载
Python官网下载下载版本为3.11.5。下载地址链接Windows版本
选择自己的版本进行下载不同Python的版本可能会有兼容性的问题
版本兼容性通常情况来说大版本不同不兼容性较大小版本不同兼容性较小。版本号大的会兼容版本号小的。但不排除有特殊情况
第二节 Python安装
如果选择exe的安装包的方式的话直接下一步下一步的安装即可
第三节 Python配置
exe安装包的方法进行默认的配置
第四节 Pycharm下载
在jetbrain的官网下载Pycharm下载地址链接Windows版本
第五节 Pycharm安装
因为是exe安装包所以直接下一步即可。安装完成之后需要license请自行解决license。
第六节 CUDA的安装
在cmd命令窗口下面执行命令
nvidia-smi查看自己的cuda版本如果版本太低的话可以下载Nvidia GeForce Experience进行更新驱动
注这里我更新过英伟达的显卡驱动用的是Nvidia GeForce Experience直接自动更新所以前后的版本不一致第一次是516.94cuda是11.7第二次是546.17cuda是12.3。
并且如果使用Nvidia GeForce Experience的话需要登录Nvidia的账号自行注册一个即可这里不再赘述。
Nvidia GeForce Experience下载地址下载安装后自动更新驱动含CUDA
第二章 Anaconda安装与配置
第一节 Anaconda下载与环境变量配置
1、Anaconda下载与安装
先下载anaconda因为是免费的所以官网下载即可 下载地址链接Windows版本
下载完成后进行安装安装后即可使用安装过程中直接点下一步即可。安装的路径为需要记录这两个路径后面在pycharm需要用到如果是你自己安装的并且是默认的路径将用户名替换成你自己的用户名即可
C:\Users\用户名\AppData\Local\anaconda3
C:\Users\用户名\AppData\Local\anaconda3\Scripts\conda.exe安装完成后如果是默认安装的话可以在开始菜单中看到这些选项其中用的最多的就是这个Anaconda Prompt后面也会重点用到。
2、Anaconda配置环境变量
进入控制面板然后输入环境变量编辑Path即可
然后添加以下三个路径即可
点击确定即可完成添加。
第二节 Anaconda镜像源配置
1、查看
在Anaconda prompt中输入以下的命令以查看当前的镜像源
# 查看镜像源
conda config --show channels# 查看默认镜像源
conda config --show default_channels2、配置命令行
1直接输入命令行配置
在Anaconda prompt中输入以下的命令选择性添加
# 添加阿里源
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/r/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/msys2/# 添加清华源不建议用
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 --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/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/# 这几条是删除清华源的命令
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/# 添加中科大源
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/# 如果是使用命令行进行镜像源配置这里需要补充一条命令在后续使用会有帮助
# 设置搜索时显示通道地址
conda config --set show_channel_urls yes2文件夹进行输入配置Windows版
直接打开文件夹窗口输入
C:/user/你的用户/.condarc这里将你的用户换成你的Windows的电脑的用户即可直接回车会弹出打开方式采用记事本或是你常用笔记软件打开都行但是建议用记事本因为常用的软件比如UE或是notepad会修改编码然后用不了。
然后换成以下的配置全量替换直接保存关闭就行可以自行备份之前的配置信息
# 以下为阿里源推荐使用
channels:- defaults
show_channel_urls: true
default_channels:- https://mirrors.aliyun.com/anaconda/pkgs/main/- https://mirrors.aliyun.com/anaconda/pkgs/free/- https://mirrors.aliyun.com/anaconda/pkgs/r/- https://mirrors.aliyun.com/anaconda/pkgs/msys2/
custom_channels:conda-forge: https://mirrors.aliyun.com/anaconda/cloudmsys2: https://mirrors.aliyun.com/anaconda/cloudbioconda: https://mirrors.aliyun.com/anaconda/cloudmenpo: https://mirrors.aliyun.com/anaconda/cloudpytorch: https://mirrors.aliyun.com/anaconda/cloudpytorch-lts: https://mirrors.aliyun.com/anaconda/cloudsimpleitk: https://mirrors.aliyun.com/anaconda/cloud
remote_read_timeout_secs: 10000.0# 以下为清华源不建议用不太行可能网络会有问题
channels:- defaults
show_channel_urls: true
default_channels:- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmsys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudbioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmenpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudpytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudpytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudsimpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
remote_read_timeout_secs: 10000.03、配置界面配置
该步骤与上步骤步骤2能实现同样的配置因此该步骤和上步骤二选一进行操作即可。 在Anaconda navigator中操作 1点击environment点击channels点击添加 2输入以下的配置源可以按需选择输入完成后按回车确认
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
https://mirrors.aliyun.com/anaconda/pkgs/free/
https://mirrors.aliyun.com/anaconda/pkgs/main/3删除默认的配置源defaults 4点击update channels进行更新
4、删除命令行
在Anaconda prompt中输入以下的命令
# 以下为几个例子如有需要则进行替换源即可
conda config --remove channels defaults
conda config --remove channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pro/conda config --remove default_channels https://mirrors.aliyun.com/anaconda/pkgs/free/这里采用界面操作形式删除也可以具体方法参照上步骤步骤3
5、修改配置源Linux版
1使用vim修改的配置文件
vim ~/.condarc2清空内容然后复制下午并保存
#以下是阿里源推荐使用
channels:- defaults
show_channel_urls: true
default_channels:- https://mirrors.aliyun.com/anaconda/pkgs/main/- https://mirrors.aliyun.com/anaconda/pkgs/free/- https://mirrors.aliyun.com/anaconda/pkgs/r/- https://mirrors.aliyun.com/anaconda/pkgs/msys2/
custom_channels:conda-forge: https://mirrors.aliyun.com/anaconda/cloudmsys2: https://mirrors.aliyun.com/anaconda/cloudbioconda: https://mirrors.aliyun.com/anaconda/cloudmenpo: https://mirrors.aliyun.com/anaconda/cloudpytorch: https://mirrors.aliyun.com/anaconda/cloudpytorch-lts: https://mirrors.aliyun.com/anaconda/cloudsimpleitk: https://mirrors.aliyun.com/anaconda/cloud
remote_read_timeout_secs: 10000.0# 以下是清华源不建议使用
channels:- defaults
show_channel_urls: true
default_channels:- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmsys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudbioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmenpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudpytorch: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudsimpleitk: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud6、常用命令
1环境类
# 进入激活环境将命令中的中文替换对应的英文名
activate 环境名# 退出关闭环境将命令中的中文替换对应的英文名
deactivate 环境名#列出所有环境三条命令皆可
conda env list
conda info --envs
conda info -e# 删除环境及下属所有包将命令中的中文替换对应的英文名
conda remove -n 环境名 --all # 删除本环境下的所有包不删除环境
conda clean -all# 复制环境将命令中的中文替换对应的英文名
conda create --name 新环境名 --clone 旧环境名2包类
# 查看conda的版本号通常都是查看版本号以验证某软件是否安装
conda --version# 列出当前环境的所有包
conda list# 查看当前环境已安装包
conda list# 查找可安装的包将命令中的中文替换对应的英文名
conda search 包名# 在当前环境安装包将命令中的中文替换对应的英文名
conda install 包名# 在指定环境安装包将命令中的中文替换对应的英文名
conda install --name 环境名 包名# 在当前环境更新包将命令中的中文替换对应的英文名
conda update 包名# 在当前环境更新所有包以下两条命令皆可
conda update --all
conda upgrade --all# 在当前环境卸载包将命令中的中文替换对应的英文名
conda remove 包名# 在指定环境卸载包将命令中的中文替换对应的英文名
conda remove --name 环境名 包名# 精确查找包
conda search --full-name 精确包名
# 例子conda search --full-name python# 模糊查找包
conda search 模糊包名
# 例子conda search py第三节 Pytorch安装与配置
1、创建Pytorch环境
使用 Anaconda Prompt 菜单栏中选项如果不记得了查看第二章第一节的第1步进入命令行创建一个pytorch环境
# 命令模板
conda create -n 环境名称 python实际安装Python版本# 实际执行
conda create -n pytorchDemoProject python3.11.5我这里创建的环境名称为pytorchDemoProject实际的Python的版本为3.11.5
注如果不指定python版本则会安装anaconda的相应的版本的。如anaconda是第二版则会安装python2的版本如anaconda是第三版则会安装python3的版本
在安装过程中先会找到对应的包然后过程中提提示是否安装Y/N这时候输入y并回车即可。 如图第一张图为更新升级conda。再执行一次即为安装pytorch。 作者注这里在安装过程中实际上出现了一些问题作者解决了之后即可成功安装。但因如此没能成功截取到图片。因此这里少了一张安装的图片但安装过程如上文所述只需在过程中按y并回车以确认安装即可。
2、激活Pytorch环境
然后激活刚刚创建的pytorchDemoProject 环境这里的激活也可以理解为进入的意思
conda activate pytorchDemoProject 如图
3、关闭Pytorch环境可选
这一步是可选的有激活的命令也就有对应的关闭命令关闭刚刚创建的pytorchDemoProject环境
conda deactivate pytorchDemoProject 4、找到pytorch命令
去官网找到安装命令
然后根据图示的方法选择命令
复制这里生成的命令即Run this Command中的命令也就是下面这条然后执行生成的命令以安装pytorch
conda install pytorch torchvision torchaudio pytorch-cuda12.1 -c pytorch -c nvidia安装过程中还要输入一次y进行确认在安装过程中会遇到很多问题请查看下面的附节一进行排查和解决
5、验证pytorch安装
使用命令先查看是否安装成功
conda list如图显示如下即表示该环境下有这些包了
安装完了之后输入python命令进入python界面 然后输入import torch进行导入 然后输入torch.cuda.is_available()进行验证是否成功如图
如图即表示安装成功
附节一、报错的场景和方法
1、创建环境报错
报错提示如下
CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://conda.anaconda.org/pytorch/win-64/pytorch-2.1.1-py3.11_cuda12.1_cudnn8_0.tar.bz2
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.没有更新配置镜像源所致需要更新一下镜像源
2、创建pytorch报错
报错提示如下
配置了错误的镜像源所致需要更新一下配置的镜像源
3、下载时间超时
有时候也会报超时的错误如图
CondaError: Downloaded bytes did not match Content-Length
url: https://conda.anaconda.org/pytorch/win-64/pytorch-2.1.1-py3.11_cuda12.1_cudnn8_0.tar.bz2
target_path: C:\Users\tangweixuan\AppData\Local\anaconda3\pkgs\pytorch-2.1.1-py3.11_cuda12.1_cudnn8_0.tar.bz2
Content-Length: 1339118426
downloaded bytes: 26179998解决方法1 直接命令行
# 设置100000.0秒的超时时长
conda config --set remote_read_timeout_secs 100000.0解决方法2 找到.condarc配置文件在配置文件最后添加
remote_read_timeout_secs: 100000.04、报错信息
Downloading and Extracting Packages
Preparing transaction: done
Verifying transaction: failed
CondaVerificationError: The package for libcurand-dev located at C:\Users\tangweixuan\AppData\Local\anaconda3\pkgs\libcurand-dev-10.3.4.101-0
appears to be corrupted. The path bin/curand64_10.dll
specified in the package manifest cannot be found.
CondaVerificationError: The package for libcurand-dev located at C:\Users\tangweixuan\AppData\Local\anaconda3\pkgs\libcurand-dev-10.3.4.101-0
appears to be corrupted. The path include/curand_precalc.h
specified in the package manifest cannot be found.
ClobberError: This transaction has incompatible packages due to a shared path.
packages: nvidia/win-64::cuda-cupti-12.1.105-0, nvidia/win-64::cuda-nvtx-12.1.105-0, nvidia/win-64::cuda-profiler-api-12.3.101-0
path: license
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/bin/cjpeg.exe
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/bin/djpeg.exe
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/bin/jpegtran.exe
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/bin/rdjpgcom.exe
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/bin/wrjpgcom.exe
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/include/jconfig.h
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/include/jerror.h
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/include/jmorecfg.h
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/include/jpeglib.h
ClobberError: This transaction has incompatible packages due to a shared path.
packages: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::jpeg-9e-h2bbff1b_1, https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64::libjpeg-turbo-2.0.0-h196d8e1_0
path: library/lib/jpeg.lib暂时没找到这个报错的原因直接从头开始来一遍了
第四节 进入pycharm
1、添加环境
直接使用命令行的形式来操作不好操作这个时候用pycharm进行操作与开发。使用pycharm可以把刚刚创建好的anaconda的环境添加进去就无需使用命令行进行开发了。 打开pycharm任意创建一个pure python的project为了方便看我这里创建的是一个名叫pytorchProject的项目你也可以随意命名。然后进入setting设置然后添加接口
然后选择路径
使用我们刚刚的路径
# 注意这里是我的电脑用户tangweixuan你自己的电脑用户不一定是这个请注意替换
C:\Users\tangweixuan\AppData\Local\anaconda3\Scripts\conda.exe然后加载一下并且选择我们在Anaconda中创建pytorchDemoProject此刻教育完成了闭环了。请给自己鼓掌打打气你基本已经完成了安装和配置了并且点击OK即可
可以看到我们刚刚安装的环境里面的包都有了
然后点击ok回到主界面在main.py中输入
import torch
print(torch.cuda.is_available())点击右上的播放按键进行执行
如上图在下方控制台上打印出了True表示成功了。
2、小试牛刀
在刚刚的pycharm中使用和尝试一些基本的pytorch语法来小试牛刀吧
# 创建一个未初始化的5x3矩阵
x1 torch.empty(5, 3)
print(x1的结果是)
print(x1)# 创建一个随机初始化的5x3矩阵
x2 torch.rand(5, 3)
print(x2的结果是)
print(x2)# 创建一个5x3的零矩阵类型为long
x3 torch.zeros(5, 3, dtypetorch.long)
print(x3的结果是)
print(x3)# 直接从数据创建tensor
x4 torch.tensor([5.5, 3])
print(x4的结果是)
print(x4)这时输入到main.py中注意import torch这句话需要一直保留即使在后面的开发中也需要保留可以在下方的控制台看到输出的结果 控制台的具体结果如下图
到这里你就基本完成pytorch的安装与配置了接下来可以大展拳脚了你也来试试看吧完结撒花。
本文完全免费且公开如果你觉得不错的话请扫描下方二维码进行赞赏吧你的支持就是我最大的动力感谢
请支持原创认准DannisTangtangweixuan1995foxmail.com