[Pytorch] Installation of deep learning framework pytorch

0. Check the version of pytorch, test whether cuda is available and its quantity, and check the code of the GPU version

import torch
if torch.cuda.is_available():
    torch.set_default_tensor_type(torch. cuda. FloatTensor)
    print ("using cuda:",torch.cuda.get_device_name(0))
    pass
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)

print(torch.cuda.device_count())



# 测试CUDA是否可用
print(torch.cuda.is_available())
print(torch.__version__)

# nvcc --version   查看cuda的版本   此命令在终端运行

View the running results of the cuda version command:

# nvcc: NVIDIA (R) Cuda compiler driver
# Copyright (c) 2005-2021 NVIDIA Corporation
# Built on Fri_Dec_17_18:28:54_Pacific_Standard_Time_2021
# Cuda compilation tools, release 11.6, V11.6.55
# Build cuda_11.6.r11.6/compiler.30794723_0

1. pytorch official website

pytorch official website installation interface
insert image description here
Click the place marked by the white box in the above picture to enter the installation interface.

insert image description here
As shown in the above interface, select the version you need.

If you want to use the GPU version of pytorch, you must first ensure that your computer has an NVIDIA graphics card.

2. Install the GPU version of pytorch process

(1) Install the cuda version that matches the graphics card model of your computer hardware.

Check the graphics card model of your computer:
insert image description here

Open the interface as shown above on your computer to view it. As shown in the picture above, my computer has an N card, and its model is NVIDIA GeForch GTX 1660Ti, which matches the CUDA 11.6 version! ! !

(2) Check the version of cuda installed by yourself

The win+R shortcut key opens the "Run" box, and the cmd command opens the terminal. Enter the following command in the terminal:

nvcc -V

(3) Check whether cudnn is installed successfully,

In the terminal, first enter the directory C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\extras\demo_suite, the command is as follows:

cd C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\extras\demo_suite

Then run the following command.

bandwidthTest.exe

The running result is marked in the red box in the figure below, indicating that the installation is successful.
insert image description here

(4) Install anaconda

(5) Create a virtual environment in the anaconda prompt in the anaconda software

Enter the following command in the base environment:

conda create -n pytorch python=3.8

Note: pytorch in the command is the name of the created virtual environment, which can be set by yourself. 3.8 refers to the version of python installed in the virtual environment, and you can also choose a different version according to your needs.
After creating the virtual environment, activate the virtual environment with the following command:

conda activate pytorch

After activating the virtual environment, you can install pytorch (why it should be installed in the virtual environment is to avoid installation package conflicts and protect the base environment).
There are two ways to install pytorch:
(1) The first is to go to the official website and copy the installation and running command of the pytorch version you have selected to the activated virtual environment to run (ensure that the network is good) .
(2) If the first method is too slow, you can go to the address specified on the official website and download the three packages of torch to your computer, and then, in the activated virtual environment, enter the installation of the newly downloaded torch In the address directory of the package, you can install them one by one .

One thing to note : the official website installation interface displays the latest version, and the previous version is shown in the figure below:
insert image description here

Guess you like

Origin blog.csdn.net/lingchen1906/article/details/128903237