Via 《动手学深度学习》
《深度学习pytorch》
Release 2.0.0
Aston Zhang, Zachary C. Lipton, Mu Li, and Alexander J. Smola
Aug 18, 2023
Chap 1 安装
在 Windows 上配置 Miniconda
安装完毕 Miniconda 后,你会得到开始菜单的两个快捷方式:
Anaconda PowerShell Prompt.lnk
Anaconda Prompt.lnk
打开 Anaconda Prompt.lnk :
分别 键入下列内容,索引出 conda 和 python 的位置
where conda
如果没有修改路径,miniconda 应该显示的内容为:
C:\ProgramData\miniconda3\Library\binconda.bat
C:\ProgramData\miniconda3\Scripts\conda.exe
C:\ProgramData\miniconda3\condabin\conda.bat
where python
如果没有修改路径或者提前安装python,miniconda 应该显示的内容为:
C:\ProgramData\miniconda3\python.exe
随后可继续按照书中步骤继续,执行下列命令:
conda create --name d2l python=3.9 -y
激活 d2l 环境:
特别注意:每次使用时都需要激活 d2l
conda activate d2l
安装深度学习框架和d2l软件包
此处与教程有出入。访问 PyTorch 官网的离线 whl 下载界面:
https://download.pytorch.org/whl/torch_stable.html
依据你的需求下载对应的 whl 文件:(使用 Ctrl+F 进行搜索)
cpu/torch-1.12.0%2Bcpu-cp39-cp39-win_amd64.whl
- CPU / CUxxx:CPU 代表 CPU 版本的 pytorch,cu121 代表 CUDA 版本号 12.1(GPU版本)的 pytorch。
- torch 后面的数字代表 pytorch 的版本号,书中为 1.12.0。
- cp39 代表 python 的版本为 3.9,python 版本可在 cmd 使用 python -v 命令查看。
- win_amd64 / linux_x86_64 / macosx:分别代表不同操作系统。
点击对应 whl 文件进行下载,建议下载至全英文路径中。下载好 whl 文件后,使用下列指令进行安装:
进入 conda 的 d2l 环境:
conda activite d2l
使用 cd 命令,进入下载的 whl 文件位置,如下载至:C:\Users\Public\Downloads 中
cd C:\Users\Public\Downloads
使用 pip 命令进行安装 whl 文件,如文件名为 torch-1.12.0+cpu-cp39-cp39-win_amd64.whl :
pip install torch-1.12.0+cpu-cp39-cp39-win_amd64.whl
这时候会报错 ERROR: torch-1.12.0+cpu-cp39-cp39-win_amd64.whl is not a supported wheel on this platform.
报错属于正常现象,因为 pip 会校验文件名称,此文件名称不符合版本要求,输入:
pip debug --verbose
pip 会列举所支持的全部版本,显示在 Compatible tags 之后。例如:cp39-abi3-win_amd64
将 whl 的文件名进行修改为 torch-1.12.0+cpu-cp39-abi3-win_amd64.whl
重新使用 pip 命令进行安装,此刻便可安装成功。
pip install torch-1.12.0+cpu-cp39-abi3-win_amd64.whl
保持 d2l 环境。输入下列代码可检索 torch 和 torchvision 的版本:
conda list
正常显示版本即可,但是某些设备会提示:EnvironmentLocationNotFound: Not a conda environment
这是由于路径中存在中文,往往由于用户名为中文,导致 C:\Users\<此处出现中文用户名>\.condaenvsd2l 中存在中文名称。
这里介绍一下环境的相关指令:
conda env list # 显示所有已创建的环境和对应路径
conda create -n <ENV_NAME> python=3.9 # <ENV_NAME> 为你创建的环境名称,python 后接版本号
conda remove -n <ENV_NAME> --all # 移除名称为 <ENV_NAME> 的环境
访问 C:\Users 进入你的用户名称文件夹,检索是否存在一个 .condarc 的文件。若没有,输入下方指令:
conda config --set show_channel_urls yes
此时会在你的用户文件夹中创建一个 .condarc 的文件。使用记事本、VSCODE 等打开,原始内容应该为:
channels:
- https://repo.anaconda.com/pkgs/main
- https://repo.anaconda.com/pkgs/r
- https://repo.anaconda.com/pkgs/msys2
show_channel_urls: true
(可选,将源修改为清华镜像以提高速度)你可以修改为:
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/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
(必要)在最后加入以下内容,将 conda 的默认环境目录修改为无中文地址,以公用用户文件夹下的 condaEnv 举例:
envs_dirs:
- C:\Users\Public\condaEnv
部分用户由于文件夹的权限问题可能仍旧无法新建,此时需要右键环境目录的文件夹 > 属性 > “安全”选项卡 > 点击 User 用户组后点击【编辑】按钮 > 将 User 用户组的全部点亮的权限均打钩为【允许】即可。
至此,torch 便已安装完毕。后续的 torchvision、d2l 等内容均可依据书中内容进行安装。
安装 torchvision:
pip install torchvision==0.13.0
安装 d2l:
pip install d2l==0.17.6
安装 D2L Notebook 下载本书代码:
注意:
① 此处 cd 到了先前修改的默认环境目录,若未修改,请不要进行 cd
② 没有安装 unzip,可以运行 sudo apt install unzip
进行安装,也可以直接去对应文件夹直接进行解压缩。(特别地:直接将压缩文件全部解压缩到 zip 所在的文件夹,而非解压到新建一个与 zip 文件相同名称的文件夹下。)
cd C:\Users\Public\condaEnv
mkdir d2l-zh && cd d2l-zh
curl https://zh-v2.d2l.ai/d2l-zh-2.0.0.zip -o d2l-zh.zip
unzip d2l-zh.zip && rm d2l-zh.zip
cd pytorch
jupyter notebook
依次运行直至最后一个命令后,cmd 中会提示:To access the notebook, open this file in a browser: 以及 Or copy and paste one of these URLs: 内容。复制其中一个链接并使用浏览器打开,便可访问本书代码。
在Web浏览器中打开 http://localhost:8888 可能会要求输入密码,因此请复制 cmd 中包含 token 的链接。