中文入门资源推荐 Recommendation for Getting Started in China

Zhiqing Xiao
PyTorch
Published in
6 min readDec 14, 2020

作者:肖智清,著有PyTorch 1教程书《神经网络与PyTorch实战》
Author: Zhiqing Xiao, author of PyTorch 1 tutorial book “Application of Neural Network and PyTorch” (in Chinese)

我是肖智清,是一名来自中国内地的PyTorch爱好者。我在2019年8月出版了教程书籍《神经网络与PyTorch实战》,这是最早的线上线下均有销售的PyTorch 1教程书。在研发社区中,我注意到不同国家和地区间网络差异有时会引起环境设置和资料获取上的差别。本文推荐一些在中国内地进行研发时可以使用的资源,包括环境安装的技巧和中文参考资料。

I’m Zhiqing Xiao, a PyTorch enthusiast from Chinese mainland. I published the tutorial book “Application of Neural Network and PyTorch” in August 2019, which is the earliest PyTorch 1 tutorial book available both online and offline. In the R&D community, I’ve noticed that network differences between Chinese mainland and other countries and regions could sometimes lead to frustration for some local developers. This article recommends some resources for developers in Chinese mainland, including installation tricks and documentations in Chinese language.

安装 Installation

历史经验表明,初学者使用Anaconda能显著提高安装成功率。本节介绍采用中国本地化网络的用户如何借助Anaconda来安装PyTorch开发环境。

Historical evidences has shown that using Anaconda for beginners can significantly increase the success rate of installations. This section introduces this approach step by step, with tricks that are particularly useful for users in Chinese mainland who can only access localized networks.

给Windows XP用户的警告:在Windows XP下无法借助Anaconda安装PyTorch。原因在于,支持Windows XP的最后版本是Anaconda 2.3,它默认使用Python 3.4。但是PyTorch需要配合Python 3.8、3.7或是3.6。

Warning for Windows XP users: The approach in this section will not work. The latest version of Anaconda that supports Windows XP is Anaconda 2.3, which uses Python 3.4 by default. PyTorch works best with Python 3.8, 3.7, and 3.6.

第1步:下载并安装Anaconda
Step 1: Download & install the latest version of Anaconda

Anaconda是用来管理Python包的软件。我们可以免费使用其个人版。本地化网络用户推荐从清华开源软件站(TUNA)下载安装包。TUNA是Anaconda官方授权的Anaconda镜像站。下载的地址为:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=D,其中URL最后的C=M&O=D表示将最近的更新显示在最前面。请根据文件名选择安装包,双击直接下载。如果网速还是太慢,可以考虑借助百度网盘的离线下载工具或是迅雷来加速下载。建议您下载后检查Hash值(特别是用迅雷的情况下),因为错误时有发生。

Anaconda is a software that manages Python packages, and its “individual” version is provided for free. Users of localized networks in Chinese mainland are suggested to download the installer from the website of Tsinghua Open Source Mirror (TUNA), which provides a mirror of Anaconda that has been formally authorized by Anaconda Inc. The download links for installers can be found here, where the suffix C=M&O=D puts the latest updates first. You can download it directly. If the network is still too slow, you may resort to Baidu NetDisk (Offline Download) or Xunlei to accelerate. It is also recommended that you check the hash value after downloading (especially if you use Xunlei), as errors can occur from time to time.

下载好安装包后,双击安装包就可以安装。图形化小白安装,一直按“下一步”即可。Windows用户可以不安装在C盘。安装之后还需要几个GB的空间进行后续安装。

After the installer is downloaded, double click the installer to launch the wizard. You may want to install it in other disks than C disk if your C disk is almost full. You will also need to reserve several GBs worth of free space on that disk to proceed to further steps.

第2步:设置清华源
Step 2: Configure “Tsinghua Source” for Anaconda

采用本地化网络的用户推荐为Anaconda设置“清华源”。清华源是TUNA维护的通道镜像。值得一提的是,Anaconda的软件源实际上可以有不同的通道。有默认通道,但是PyTorch需要从pytorch通道下载。清华源对默认通道有镜像,也提供了pytorch等第三方通道的镜像。清华源与原始的源相比,有一定时间的延迟。不过延迟很有限,不会超过1天。设置清华源的方法:创建 ~/.condarc文件,写入下面的内容。

Developers with localized network in Chinese mainland are recommended to configure Anaconda channels to “Tsinghua Source”, which is a vulgar for package mirrors of conda channels maintained by TUNA. In conda system, packages can be placed in different channels. Besides the default channel, “Tsinghua Source” also maintains channels such as PyTorch. “Tsinghua Source” has limited delay (<1 day) compared to the original source. In order to switch to “Tsinghua Source”, we can create the file ~/.condarc, and put the following configurations in the file.

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/free
custom_channels:
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

修改好 .condarc后,可以在Anaconda Prompt中用下列命令查看是否设置成功。

You can verify the configuration by typing following commands in Anaconda Prompt:

conda config –show channels
conda config –show custom_channels

确认设置成功后,用下列命令更新缓存。

After the changes, execute the following command in Anaconda Prompt to flush the cache.

conda clean -i

第3步:在conda环境中安装PyTorch
Step 3: Install PyTorch in a conda environment

为了安全起见,我们会在一个全新的conda环境中安装PyTorch。我们可以用管理员身份在Anaconda Prompt中用以下命令新建一个名为pytorch1的环境。

I recommend installing PyTorch in a new conda environment to avoid any issues. We can execute the following commands in Anaconda Prompt as an Administrator to create a new conda environment called pytorch1.

conda create --name pytorch1 --clone base
conda activate pytorch1

接下来就正式安装pytorch包啦。我们可以安装CPU版本或是GPU版本。要安装CPU版本,只需要以管理员身份在Anaconda Prompt中运行以下命令:

Finally, we install PyTorch within the conda environment. We can choose between either the CPU version or GPU version of PyTorch. To install the CPU version, run the following command in Anaconda Prompt as an Administrator:

conda install pytorch cpuonly -c pytorch

如果想安装GPU版本,需要先安装CUDA和cuDNN。在Windows 10系统上安装CUDA和cuDNN,建议采用以下步骤:

You may also want to leverage the GPUs in your computer. In that case, we need to first install CUDA and cuDNN. The recommended steps to install CUDA and cuDNN in Windows 7/10 are as follows:

(1) 确认机器上NVIDIA独立显卡的CUDA计算能力。首先要确定GPU型号,方法为:右击“我的电脑”->在上下文菜单中选择“管理”->在窗口左边栏中选择“设备管理器”->选择“显卡”查看GPU型号。然后在网页https://developer.nvidia.com/cuda-gpus中根据GPU型号查找对应的CUDA计算能力。通过conda安装的版本往往对CUDA计算能力有要求(例如,在2020年12月发布的PyTorch 1.7.1要求CUDA计算能力至少为3.5)。如果显卡过于古老导致CUDA计算能力不满足要求,自行编译PyTorch也许能解决,不过事倍功半建议放弃。

(1) Check the NVIDIA GPU and its compute capability on the computer. Method: right click “This PC” -> choose “Manage” -> in the left column of “Computer Management”, choose “Device Manager” -> choose “Display adapters” to see what GPU has been installed in the current PC. Then check the compute capability in the webpage https://developer.nvidia.com/cuda-gpus. PyTorch installed in Anaconda has requirement on compute capability. For example, PyTorch 1.7.1 (released in Dec. 2020) requires compute capability of at least 3.5. For GPU older than that, building from source may resolve the problem, but it is not recommended.

(2) 更新显卡驱动。推荐使用NVIDIA GeForce Experience更新最新显卡驱动。NVIDIA GeForce Experience可以在https://www.nvidia.cn/geforce/geforce-experience/找到。安装NVIDIA GeForce Experience后,首次运行需要登录NVIDIA账号。如果没有NVIDIA账号要先注册。登录过程会涉及reCAPTCHA验证,而中国内地本地化网络一般无法成功加载reCAPTCHA。绕过reCAPTCHA的方法:先选择第三方登录,再链接到已有NVIDIA账号。登录成功后,在软件界面里更新驱动(下载的驱动大小约为数百MB)。

(2) Update the driver for the GPU. I recommend using NVIDIA GeForce Experience to update the driver to the latest version. You can download the installer of NVIDIA GeForce Experience from here. Login is required after installation, which involves verification using reCAPTCHA. Since localized networks in Chinese mainland may not load reCAPTCHA, we may bypass reCAPTCHA by selecting 3rd-party login and linking the 3rd-party account with your existing NVIDIA account. After login, please update the driver to the latest version.

(3) 确定CUDA版本。一方面我们要确定显卡支持的CUDA版本。方法:在桌面空白处右击,在弹出的上下文菜单中选择“NVIDIA控制面板”。(如果没找到NVIDIA控制面板,可能是没有在NVIDIA GeForce Experience里更新驱动。)在“NVIDIA控制面板”窗口菜单栏里的“帮助”->“系统信息”,弹出“系统信息”窗口,选择“组件”选项卡,里面有“NVCUDA.DLL”这个条目。条目对应的值会显示推荐安装的CUDA版本号。另一方面,我们也要注意PyTorch支持的CUDA版本号。PyTorch不一定会支持最新版本的CUDA。例如,在2020年12月的PyTorch 1.7.1支持的CUDA版本包括10.1、10.2、11.0。至此我们就确定了要安装的CUDA版本。

(3) Determine the version of CUDA. On the one hand, CUDA version needs to be supported by GPU and its driver. To determine the supported CUDA version, right click the desktop and choose “NVIDIA Control Panel”. (If you do not see NVIDIA Control Panel, you probably have not updated the driver to the latest using NVIDIA GeForce Experience.) In NVIDIA Control Panel, choose “Help”->”System Information”, and then choose the “Component” tab in the popup window, and find an entry called “NVCUDA.DLL”. This entry shows the recommended version of CUDA. On the other hand, the CUDA version needs to be supported by PyTorch. Note that PyTorch may not support the latest version of CUDA. For example, PyTorch 1.7.1 (released in Dec. 2020) supports CUDA 10.1, 10.2, and 11.0 best.

(4) 下载并安装CUDA。根据选定的CUDA版本,到NVIDIA网站下载CUDA。安装文件约2.5GB。然后安装CUDA,CUDA默认安装在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA,占用约2.5GB空间。成功安装后,我们可以在普通的命令行中输入nvcc -V查看安装情况。

(4) Download and install CUDA. We can download the CUDA with the correct version from the NVIDIA website. The size of the installation file is about 2.5GB. The default installation location is: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA, and the installation occupies about 2.5GB space. We can use the command nvcc -V in the Command Prompt to verify the installation of CUDA.

(5) 下载并解压cuDNN。官网推荐到https://developer.nvidia.com/cudnn下载cuDNN(需要登录NVDIA账号)。不过本地化网络下的网速常常过慢以致下载失败,需要另找办法。下载的zip文件大概0.3GB。解压后大概0.6GB。解压后,观察解压得到的目录的结构,把文件复制到CUDA安装目录的对应地方。

(5) Download and unzip cuDNN. The simplest way to install cuDNN is from here, but a workaround is usually required since the localized network connection is often too slow for successful downloading. The downloaded zip file is about 0.3GB. Unzip it, and the unzipped file is about 0.6GB. Investigate the unzip directory, and copy files into corresponding directory in CUDA installation directory.

至此,我们已经安装好了CUDA和cuDNN。接下来就正式安装PyTorch。安装PyTorch只需要在Anaconda Prompt的环境里用管理员权限执行下列命令(其中10.2应当换为合适的CUDA版本号):

After the aforementioned steps, we can now finish installing CUDA and cuDNN. Now we can install pytorch-gpu using the following command in Anaconda Prompt, where 10.2 should be changed to suitable CUDA version:

conda install pytorch cudatoolkit=10.2 -c pytorch

第4步:确认安装成功
Step 4: Verify the Installation

我们可以在Anaconda Prompt中运行python命令进入Python模式,测试PyTorch是否正确安装。

We can try PyTorch within python to verify our installation. Type the command python in Anaconda prompt to go to Python mode, and then execute the following codes to see whether it can output normally.

import torch
x = torch.rand(2, 2)
print(x)

如果安装了GPU版本,可以用下面的代码确认是否安装成功。

You can use the following codes to verify whether GPU can be used.

import torch
print(torch.cuda.is_available())
print(torch.rand(2, 2).cuda())

文档和教程 Documents and Tutorials

最权威的文档和教程就是官方网站:

The best documentation and tutorials are the official website:

https://pytorch.org/tutorials
https://pytorch.org/docs

开源社区iBooker自发提供了中文教程和文档的中文翻译(不断更新中):

The open-source organization iBooker provides the translation of the documentation and tutorials in Chinese language:

https://pytorch.apachecn.org/

除了官网以外,GitHub和电商上等也有许多教程。有些教程是针对较早的PyTorch版本,和目前最新的PyTorch API并不兼容。也有一些和目前最新PyTorch API完全兼容的教程,例如《神经网络与PyTorch实战》,它的部分正文可以在这个链接免费在线阅读:

Besides the official documentation and tutorials, there are some resources in GitHub and e-business platform. Unfortunately, some tutorial books were written for earlier versions of PyTorch, and may not be fully compatible with the latest PyTorch APIs. I personally recommend the book “Application of Neural Network and PyTorch” (in Chinese), which is fully compatible with the latest PyTorch APIs. You can read some of its contents for free here:

http://www.hzmedia.com.cn/books11119345

PyTorch在Facebook、Twitter、Medium、YouTube上会时不时发布一些新消息。非官方的社区自愿者会在某些中国内地的本地平台上的转发那些消息。

The PyTorch team publishes news in Facebook, Twitter, Medium, YouTube, and so on. Unofficial volunteers may forward the news in localized social networks so users of localized network can access them more easily.

(Opinions in this article are those of Zhiqing, and do not necessarily reflect the view of any organizations or institutions.)

参考资料 Reference
[1] PyTorch. Start Locally. https://pytorch.org/get-started/locally
[2] TUNA. Help on Anaconda Mirror. https://mirror.tuna.tsinghua.edu.cn/help/anaconda/

--

--

Zhiqing Xiao
PyTorch
Writer for

AI researcher. Author of the book “Application of Neural Network and PyTorch” and the book “Reinforcement Learning: Theory and Python Implementation”.