Installation tutorial of PyTorch development environment under Windows

  • 2021-07-24 12:02:09
  • OfStack

Anaconda Installation

Anaconda is a software package established for the convenience of using python, which contains more than 250 commonly used toolkits, multi-version python interpreters and powerful virtual environment management tools, so Anaconda is named python Family Bucket. Anaconda makes it easier to install, run, and upgrade your environment, so it is recommended for installation.

Installation steps:

Official website downloads the installation package https://www.anaconda.com/distribution/# download-section Run the installation package Select the installation path: Usually select the default path. Be sure to check Add Anaconda to the system PATH environment variable (add Anaconda to the environment variable) and wait for the installation to complete Verify successful installation: Shortcut key win+R, open cmd, enter conda, enter, if all kinds of relevant information appear, the installation is successful.

PyCharm installation

PyCharm is a powerful Python IDE, with debugging, syntax highlighting, Project management, code jump, intelligent prompt, version control and other functions.

Installation steps:

Official website downloaded the installation package https://www.jetbrains.com/pycharm/, which is divided into professional version (charged) and community version (free). Run the installation package. Select the path, check Add launchers dir to the PATH, check. py, and wait for the installation to complete.

CUDA and CuDNN installations (not required)

Check whether there is a suitable GPU. If there is, install CUDA and CuDNN. Only N cards support cuda. You can view the supported cuda versions by doing the following: NVIDIA Control Panel → System Information → Components → 3D Settings/NVCUDA. DLL Enter PyTorch official website https://pytorch.org/, and click GetStarted to see what CUDA versions are supported. Enter CUDA official website https://developer.nvidia.com/cuda-toolkit-archive, select the corresponding version of CUDA, select the corresponding operating system, and select local for Installer Type. Click to download the first file. Run the installation package without creating a shortcut when the installation is complete. Verify that CUDA was installed successfully: Enter the bin folder of the installation path, copy the path, and switch to the command line under that path (for example, cd C:\ Program Files\ NVIDIA GPU Computing Toolkit\ CUDA\ v10.1\ bin), then execute nvcc-V, enter, and if the relevant version information appears, indicate the correct installation. Enter cuDNN official website https://developer.nvidia.com/rdp/cudnn-download, register and log in to the account, and select the corresponding version to download. Unzip the installation package and copy the three folders inside to the CUDA installation path (for example, C:\ Program Files\ NVIDIA GPU Computing Toolkit\ CUDA\ v10.1), and cuDNN is installed. Verify that cuDNN was installed successfully: Switch to the extras\ demo_suite folder in the installation path from the command line, execute bandwidthTest. exe, enter, Result = PASS to indicate successful installation. Continue with deviceQuery. exe, enter to show GPU model, Result = PASS, indicating that both CUDA and cuDNN have been installed successfully.

Replace pip source to domestic image

When installing the python toolkit with pip, it is recommended to switch to domestic mirroring because the default source network speed is extremely slow.
Default source

https://pypi.org/

Domestic mirror image

Alibaba Cloud https://mirrors.aliyun.com/pypi/simple/

University of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/

Douban (douban) https://pypi.douban.com/simple/

Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/

Huazhong University of Science and Technology https://pypi.hustunique.com/

Temporary use
Installation command followed by-i url, such as: download pandas with Tsinghua image
pip install pandas-i https://pypi. tuna. tsinghua. edu. cn/simple
Permanent modification
Modify the% HOMEPATH%\ pip\ pip. ini file
% HOMEPATH% Usually C:\ Users\ xx
Usually, you need to create a new folder pip, and then create a new file pip. ini. The ini file can be opened and edited with Notepad. Enter the following contents in the pip. ini file (take Douban image as an example):

[global]
index-url = https://pypi.douban.com/simple
[install]
trusted-host = pypi. douban. com

PyTorch Installation

1. Download the whl file (not required)

Enter PyTorch official website https://pytorch.org/, click GetStarted, select the corresponding version, select pip for package, and you can see the latest version numbers of torch and torchvision below, as well as a website https://download. pytorch. org/whl/torch_stable. html. Copy the website and enter. You can see the installation files of each version of torch. In this way, it is faster than directly executing the installation command. File naming is regular, such as:

cu101/torch-1. 4.0-cp37-cp37m-win_amd64. whl,
cu101/torchvision-0. 5.0-cp37-cp37m-win_amd64. whl

cuda after cu, torch after torch, torchvision after torchvision, python after cp, and windows 64 bits at the back.

You can use the shortcut key ctrl+F to search for the latest version of torch and torchvision files (for example, search for cu101/torch-1. 4.0), and then select the corresponding python version and platform to download whl files of pytorch and torchvision. If the python version is compatible with System 1, you can enter python on the command line to view the version of python in the system.

2. Create a new project with PyCharm

Create New Project → Pure Python → Naming → Create
Create a new script: File → New → Python file → Naming → Enter
Enter the following code in the script → Right-click → Run 'Project Name' → Report an error. torch cannot be found because we do not have PyTorch installed in the current environment.


import torch
print("hello pytorch{}".format(torch.__version__))
print(torch.cuda.is_available())

3. Create an python virtual environment

Click Terminal below → Enter conda create-n virtual environment name python= version number (e.g. conda create-n pytorch_gpu python=3. 7) → Enter → Wait for completion

Enter Virtual Environment: Enter conda activate Virtual Environment Name → Enter

Step 4 Install

Enter the directory of whl file: Enter the directory of cd whl file → Enter
Installation: Enter pip install torch → Press tab to complete automatically → Enter → Wait for successful installation
Enter pip install torchvision → Press tab to complete automatically → Enter → Wait for successful installation

Note: If the whl file is not downloaded in step 1, install it directly with pip or conda command, and the installation command will be displayed after selecting the corresponding version in PyTorch official website.

5. Associate the current project with the newly created virtual environment, that is, select the python interpreter

File → Setting → Project: Project Name/Project Interpreter → Setting Button → Add → Conda Environment → Existing environment → interpreter Select anaconda Installation Path/envs/Virtual Environment Name/python. exe → OK → OK → OK → Wait a moment to initialize

Step 6 Verify

Right-click to run, and successfully output PyTorch version. If True is output, it proves that GPU is available.

Summarize


Related articles: