How to call the GPU training model [detailed tutorial 2] - PyTorch installation and configuration

Table of contents

PyTorch installation

The pit stepped on during installation

Ways to increase installation speed

Verify installation results


PyTorch installation

In the previous article, CUDA and cuDNN were installed. If you have not installed it, you can refer to: How to call the GPU training model [Detailed Tutorial 1] - CUDA and cuDNN installation https://blog.csdn.net/weixin_45206129/article/details/130319783 ?spm=1001.2014.3001.5501

At this point, the last step is the installation of PyTorch. Do you still remember the Pytorch website that was opened at the beginning: PyTorch 

Select the CUDA version (11.8) you just installed, and the corresponding installation command is as follows:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
or
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

The installation command needs to be run in the Anaconda virtual environment. The above two commands can be used, but they may fail. Try a few more times, and replace them if they fail.


The pit stepped on during installation

Here comes the point! When I tried to install PyTorch of cuda11.8, no matter whether I used conda or pip, it was unsuccessful. It was prompted that the resources of the corresponding version could not be found, and various mirrors and official sources were not available. Later, it was successfully replaced with cuda11.7, ( My personal guess is that cuda11.8 of the official library is missing)

The following are the installation instructions for cuda11.7:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
or
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

After installing Pytorch of 11.7cuda, downgrade CUDA (reinstall version 11.7 and copy into cuDNN again).


Ways to increase installation speed

Let me share another installation method here. I installed it using pip. Although I can communicate with the official library, the download speed is very fast (20kb/s). You can analyze the above installation instructions:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

  1.  pip3 install: the meaning of the installation command
  2. torch torchvision torchaudio: three packages to be installed respectively
  3. --index-url https://download.pytorch.org/whl/cu117: Find the path of the installation package

It can be seen that when we install a complete pytorch, we need to install three packages [torch, torchvision, torchaudio]

Therefore, we can open the command line page and perform the installation of a package individually each time, for example:

pip3 install torch --index-url https://download.pytorch.org/whl/cu117

If the connection is normal and the download starts, the full name of the download package will be displayed, but the download speed is very slow. At this time, record the package name, end the process, and go to the website behind --index-url above to find the corresponding package. , download manually, the speed will be much faster.

After manually downloading, put it in a directory that you can find, open the anaconda prompt, activate the virtual environment, cd into the drive letter where the package is stored, cd into the directory, use pip install xxxxxxxx.whl to install, here you need to replace it with yours Package names.

From my personal experience, among the three packages, only the installation of torch is relatively slow, because it is relatively large, and can be downloaded and installed manually; the other two can be installed directly with the pip command. After the installation is complete, pip list can see the following three packages (torchcrf is not installed this time):


Verify installation results

After all the three packages are installed and there is no problem, you can conduct an inspection. The inspection mainly includes two aspects:

(1) Whether PyTorch can be referenced (2) Whether it is possible to call the graphics card for calculation

First, check whether PyTorch can be referenced. Enter import torch in the Python Console and press Enter. If no error is reported, it is a normal reference:

Then enter print(torch.__version__) and print(torch.cuda.is_available()) respectively to check the Pytorch version and whether the GPU can be called:

It can be seen that 1.13.1+cu117 and True are displayed respectively, which means that the version is 1.13.1, and it is cuda11.7 version. If it is the cpu version, cu117 will not be displayed here but cpu; True means that the GPU can be called, if it is False, it may be because the CUDA installation is not normal or PyTorch is not downloaded correctly, push back layer by layer to find the reason. I'm pasting the code below so you can copy it

import torch

print(torch.__version__)

print(torch.cuda.is_available())

 At this point, the installation of CUDA+cuDNN and PyTorch has all been completed. In theory, model training can be performed. Later, I will post another article on how to call GPU for training by modifying the code.

hope it is of help to you!

Guess you like

Origin blog.csdn.net/weixin_45206129/article/details/130330944