Contents
  1. Anaconda
  2. CUDA
  3. Pytorch
  4. Testing

Anaconda

Anaconda is a tool for managing various Python packages. Here, we mainly use NumPy and some other commonly used packages.

Anaconda official website: https://www.anaconda.com/

You can directly download and install the Anaconda installer for your operating system. For instructions on how to use it, refer to this article

CUDA

  1. First, check the CUDA version supported by your computer’s GPU. As shown in the figure, open NVIDIA Control Panel -> Help -> System Information -> Components (you can open it from Control Panel or the notification area in the lower-right corner). The CUDA version supported by my GPU is 11.6.106, so the CUDA version I install cannot exceed this version.

CUDA

Alternatively, enter nvidia-smi in cmd to check the CUDA version.

  1. Go to the CUDA Toolkit Archive and select a CUDA Toolkit version lower than the version you just found. This article uses CUDA Toolkit 11.0 Update 3. Download the installer, which is about 2.7G.

Note: The CUDA version selected here determines the cuDNN and tensorflow-gpu versions installed later. These three versions are related, and they will not work if the versions do not match. For specific compatible versions, refer to https://tensorflow.google.cn/install/source_windows. The table below is an excerpt.

VersionPython VersionCompilerBuild ToolcuDNNCUDA
tensorflow_gpu-2.6.03.6-3.9MSVC 2019Bazel 3.7.28.111.2
tensorflow_gpu-2.5.03.6-3.9MSVC 2019Bazel 3.7.28.111.2
tensorflow_gpu-2.4.03.6-3.8MSVC 2019Bazel 3.1.08.011.0
tensorflow_gpu-2.3.03.5-3.8MSVC 2019Bazel 3.1.07.610.1
tensorflow_gpu-2.2.03.5-3.8MSVC 2019Bazel 2.0.07.610.1
tensorflow_gpu-2.1.03.5-3.7MSVC 2019Bazel 0.27.1-0.29.17.610.1
tensorflow_gpu-2.0.03.5-3.7MSVC 2017Bazel 0.26.17.410
tensorflow_gpu-1.15.03.5-3.7MSVC 2017Bazel 0.26.17.410
tensorflow_gpu-1.14.03.5-3.7MSVC 2017Bazel 0.24.1-0.25.27.410
tensorflow_gpu-1.13.03.5-3.7MSVC 2015 update 3Bazel 0.19.0-0.21.07.410

CUDA (2)

  1. Double-click the downloaded exe installer, select a location for temporary extraction, and then click ok. Extraction takes about two minutes.

CUDA (3)

  1. Accept the license agreement, select the custom installation option, and then click Next.

CUDA (4)

CUDA (5)

  1. Select the driver components. Be sure to check CUDA, and then click Next.

CUDA (6)

  1. Choose an installation location on your computer, and then start the installation.

CUDA (7)

  1. Wait for the installation to finish. On this ‘18 laptop with a mechanical hard drive, the installation took about 5 minutes.

  2. Check the environment variables. They are generally configured automatically after installation. Open Settings - System - System Information - Advanced System Settings - Environment Variables to check them. If they are not present, you need to add them yourself (remember to change them to your own installation path when adding them).

  • Check whether the system variables contain the two environment-variable groups CUDA and NVCUDASAMPLES.

CUDA (8)

  • Open Path under system variables and check whether it contains the following environment variables.

CUDA (9)

  1. CUDA installation is complete

Pytorch

(1) Create a PyTorch environment

Use Anaconda to create a pytorch virtual environment, mainly to separate different projects. For example, TensorFlow and PyTorch use different package environments, and creating a new environment can handle incompatibility issues between projects.

  1. Open Anaconda Prompt

  2. Create a new virtual environment

conda create -n pytorch python=3.8

Here we create a pytorch environment. In the command, pytorch is the environment name — you can call it whatever you like. For the Python version, either 3.8 or 3.9 works here.

Use the conda info --envs command to see all environments, and use conda activate pytorch to activate the pytorch environment you just created.

When the prompt changes from (base) to (pytorch), it means you have switched to the pytorch environment.

Pytorch

(2) Install PyTorch

Go to the PyTorch official website: https://pytorch.org/, where you can see the PyTorch installation page.

By default, some information has already been selected based on your computer. You only need to select the CUDA version. I just checked that my computer’s CUDA version is 11.6.106, so I select CUDA 11.6 here.

Pytorch (2)

It notes that cudatoolkit needs to be installed on the computer before installation. We already installed it in the previous section, so you can run the install command shown below directly in the conda pytorch environment.

Enter the virtual environment

conda activate pytorch

Install PyTorch

conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge

If the download fails with an error, make sure to turn off your VPN, then go to the Tsinghua University mirror site and add the index by following the steps there. If it still fails and you can use a VPN, you can use the command conda config --remove-key channels to remove the Tsinghua channel, delete the C:/用户/xxx/.condarc file, and download using a VPN with the default channels.

Testing

Open the Anaconda Prompt command line and enter the pytorch environment.

conda activate pytorch

Enter python to enter the Python interactive interface

Enter the following commands to test:

import torch 
print(torch.__version__) # pytorch版本
print(torch.version.cuda) # cuda版本
print(torch.cuda.is_available()) # 查看cuda是否可用

Testing