Pytorch配置笔记
Pytorch配置笔记
PyTorch1.12.0
由于是跟着李沐的《深度学习 PyTorch版》学习,因此参照此书所使用的版本进行配置。(在线版在此)
安装Miniconda
下载Miniconda安装包
在官网下载即可,此处选择Windows系统对应的Python3.9版本64位
安装
需要注意的是,由于本来已安装原生的Python3.7版本,为了防止冲突,暂时不添加环境变量,此处仅勾选第一项即可。
可通过 conda info 命令验证安装是否成功。
conda 换源
由于 miniconda 下载文件/依赖库等默认的采用国外的服务器,你懂的,此处改清华源。
在Anaconda Powershell Prompt (Miniconda3)中依次运行以下两条命令:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
在 C:\Users\AncilunKiang 文件夹找到 .condarc 文件替换成清华源Anaconda 镜像使用帮助中的内容
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- 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/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
创建d2l环境
使用以下指令创建环境
conda create --name d2l python=3.9 -y
使用以下指令激活d2l环境
conda activate d2l
关于版本选择
总体来说,版本选择需要使PyTorch版本与CUDA版本对应,又要求GPU驱动支持上述CUDA版本。
由于PyTorch库的下载组件内部含有cudatoolkit,它是CUDA的子集,已经足够PyTorch使用,因此无需再单独安装CUDA和CUDNN,直接使用PyTorch官网的安装命令即可。
可在 NVIDIA控制面板->帮助->系统信息 中查看驱动版本以及最高支持的CUDA版本:


也可通过 nvidia-smi 命令查看
如需更高版本的CUDA则需要更新驱动,驱动程序与CUDA版本对应关系可在文档中查询
在PyTorch官网中查看对应版本推荐的CUDA版本,PyTorch1.12.0推荐CUDA10.2、11.3以及11.6。
综合上述信息,看似选择CUDA10.2版本比较合适。
安装PyTorch
但是!使用conda安装发现不是GPU版本,还是用不了GPU。查阅教程发现似乎是因为conda使用清华源总是会下载CPU版本。
那就换pip安装,结果直接报 ERROR: Could not find a version that satisfies the requirement。
试图在对应的下载地址手动下载时,PyTorch+CUDA10.2+Python3.9根本没有Windows版本的。
但是我又实在不想和书上的PyTorch版本不一样,也不想升级驱动。遍寻教程后发现其实不用考虑PyTorch内置CUDA与计算机显卡CUDA版本之间的关系,此处参考此B站视频。也就是说我直接装CUDA 11.3的即可。
使用对应pip指令安装成功:
# CUDA 11.3
pip install torch==1.12.0+cu113 torchvision==0.13.0+cu113 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu113
这几个whl文件可以在命令对应的网站下载下来以备不时之需,重装用本地文件也可。



安装d2l包 下载书本代码
在d2l环境下运行以下指令:
pip install d2l==0.17.6
此包带Jupyter Notebook,直接使用 jupyter notebook 命令即可打开,测试无误。
ps:用VSCode看着比用Web顺眼

浙公网安备 33010602011771号