实验环境配置

基础安装配置

Anaconda(或Miniconda)

按官网文档下载安装1

以下是安装命令

# Linux / Windows WSL
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
# Intel Mac
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
sh Miniconda3-latest-MacOSX-x86_64.sh
# M1 Mac
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh

创建新的python环境

打开命令行终端 Anaconda Powershell Prompt,并输入以下代码:

# conda create --name ai
conda create --name ai python=3.8
conda activate ai
# GPU, Windows/Linux
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
# CPU, Windows/Linux
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# CPU, Mac
conda install pytorch torchvision torchaudio -c pytorch
python -c 'import torch; print(f"CUDA available? {torch.cuda.is_available()}"); print(torch.rand(5, 3));'

安装其他可选包

《动手学深度学习》中的辅助函数包:

pip install d2l

Jupyter Lab

如果不使用IDE,建议安装Jupyter Lab

# install jupyterlab
conda install -c conda-forge jupyterlab -y

验证安装是否成功:首先打开 Jupyter

cd code-data-folder
# cd /mnt/d/teach
jupyter lab

程序会自动跳转到默认浏览器,并访问网址http://localhost:8088

下面测试pytorch是否可用(in JupyterLab):

import torch
print(torch.rand(5, 3))

Amazon SageMaker Studio Lab

Amazon SageMaker Studio Lab2是《动手学深度学习》推荐的云服务平台。

  1. 申请账号,等待同意后注册。
  2. 创建CPU(限12小时)或GPU(限4小时)实例。
  3. 将项目添加到工作区。
  4. 开始动手做实验。

Google Colab

需要能够访问Google服务,详见《深度学习21F》文档。

FAQ

Good Practices

注意:安装路径不使用带有中文或空格的目录

PyTorch

Q: Python complained: “No module named ‘torch’”, but I followed exactly the official instructions

Usually a false alarm: restart the terminal. If the error persists: try lower python versions conda create --name ai python=3.6, or write to the official maintainers.

Quickstart

See: Quickstart @ pytorch.org3.


  1. https://docs.anaconda.com/anaconda/install↩︎

  2. https://studiolab.sagemaker.aws/↩︎

  3. https://pytorch.org/tutorials/beginner/basics/quickstart_tutorial.html↩︎