2023 latest pytorch installation tutorial, simple and easy to understand, for beginners (Anaconda+GPU)

I. Introduction

It is currently 2023.1.27. In view of the fact that I stepped on the pit during the installation process, I will remind everyone who is about to install pytorch before the installation. There are the following points to pay attention to

1. Determine whether your computer has a GPU

It is important to note that this tutorial is for computers with NVIDA graphics cards. If your computer does not have a GPU or uses an AMD graphics card, please install the CPU version of pytorch. I don't know how to operate the AMD graphics card, so I won't repeat it here.

2. Select the appropriate pytorch version, the specific method will be described later

3. Update the graphics card driver, preferably a newer version, so that it is not easy to cause version mismatch problems and cause unnecessary troubles

2. Download and install Anaconda

1. Official website download

The download speed is slow, the official website address: Anaconda
select Products -> Anaconda Distribution
click Download, you can download the latest version

insert image description here

2. Mirror download

Use an open source mirror website to download, the speed can be faster, and students with poor network speed are recommended to use this method.
Link: Open source mirror
Select the appropriate version to download according to your needs

3. Install Anaconda

Double-click to run the installation package, select all users, and
it is recommended to install the C drive. If there is not enough space, you can install other drives. At present, I have no problem with using it.
The installation path can be kept in English.
For the environment variable, you can check to add it automatically, or you can add it manually.
The method of adding is cumbersome and will not be repeated here.
After the installation is complete, call the command line and enter

python

Check the python environment, the normal is as follows:
insert image description here
input

conda --version

Check whether the installation is successful.
The display is as follows:
insert image description here
It means that the environment variable configuration is successful.

3. Use conda to download pytorch

1. Create a virtual environment

(1) Use the conda create command to create a new virtual environment
. Find the Anaconda folder in the application list, click the anconda prompt, enter the command line, and enter the following code to create a virtual environment.

conda create –n 虚拟环境名字 python=版本

My is python3.8, so the specific code is

conda create -n pytorch python=3.8

insert image description here
(2) Enter y to download
(3) After the installation is complete, enter

conda info --envs

Verify whether the installation is successful, if appears,
insert image description here
it means success.

(3) If the download speed of some students is slow, you can use the mirror to download.
The code format is

conda create –n 虚拟环境名字 python=版本 –c 镜像地址

This is a method of image download. It is recommended that you use the method followed by -c image address for image download. Try not to modify the configuration file. Some tutorials I read at the beginning have been unable to download after modifying the file. It is recommended not to use it. lead to some weird errors. If you want to modify, it is recommended to back up the original configuration code first.
Below are a few mirror sources.
Link: Tsinghua Mirror
Link: Ali Mirror

I remember that the c in -c means channel, which means the download channel, that is, the download URL. -c python, -c address, etc. refer to downloading from the following channel. If the source behind is a foreign URL, just It may be very slow, this varies from person to person, and I have not been affected by it too much.

Note that if you want to modify the configuration file, the file is in the c drive, user, the folder of your user name, specifically called .condarc,
insert image description here
just open it with Notepad and modify it.

2. Enter the virtual environment

enter

conda activate pytorch

Enter the virtual environment we just created and install pytorch in it.

3. Use conda to install PyTorch

This article mainly introduces the conda installation of PyTorch installation, and the pip method will not be repeated.
We need to install PyTorch in the virtual environment created in the previous step.
Mainly need to install pytorch, torchvision, torchaudio three packages.

3.1 Official website download:

Link: pytorch
insert image description here
chooses the appropriate CUDA version according to its own computer configuration.
(1) First determine your graphics card model and computing power
(2) Determine your CUDA Driver version, view method: command line input

nvidia-smi

insert image description here

(3) Determine the CUDA version that you can use, that is, CUDA Runtime Version, which is the version in the above picture. To ensure that the CUDA Driver version >= CUDA Runtime version, like my CUDA is version 11.6, I chose version 11.3 when installing. My graphics card is RTX3060, and the 11.x version is fine for the 30 series graphics card. For the 10 series, you can choose 10.x and the like, and the specific situation will be analyzed in detail.
Specific method:
Click
insert image description here
to enter the following page:
insert image description here
The above is my code for installing version 11.3. The operation method is as follows:
1) Enter in the anaconda prompt

conda activate pytorch

Enter the virtual environment we just created and install pytorch in it.
insert image description here
2) input

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

Enter y according to the prompt to install pytorch (you may need to wait, but the official website may have been stuck in the package search stage). Note that after searching the package, you must carefully check whether it is the GPU version, and check whether the important packages are there. Generally, there is no problem with the official website, mainly because there may be problems with the image download later.
I saw some tutorials saying that removing -c will make pytorch download faster, but it is actually not necessary. This is the official website download. Only when the image is downloaded and the file has been configured, can it be removed to have an actual effect. Because -c is equivalent to specifying the download address, it is of course slow to download after -c followed by a foreign website, and it is not recommended to change the file, it is best to download directly from the official website.

3) Verify
After the download is complete, enter the pytorch virtual environment and enter

conda list

Check if there is pytorch or torch, if there is a successful installation
insert image description here

Enter python, and then enter

import torch

enter

torch.cuda.is_available()

If the return value is True, it means success. Congratulations, the installation is complete.
insert image description here

3.2 Mirror download:

The previous operation is the same as the previous method, but the code is different during installation. The method I introduced does not need to modify the configuration file.

conda install pytorch torchvision torchaudio –c 镜像地址
Conda install cudatoolkit=版本 –c 镜像地址

Tsinghua University: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
Ali: http://mirrors.aliyun.com/anaconda/cloud/pytorch/win-64/
verification method Ditto.

ps: The method in this article is not original. I am also a research monk who is studying. The method in this article is borrowed from station b. I hope that more people will stop worrying about installing pytorch. Finally, I would like to express my thanks again to Mr. Tudui, and attach the address of the video tutorial:
Link: The most detailed introduction to PyTorch under Windows. Deep learning environment installation and configuration CPU GPU version| Tudui If there are mistakes or omissions in the tutorial
, everyone is welcome to criticize and correct.

Guess you like

Origin blog.csdn.net/weixin_45334223/article/details/128772572