Article directory
1 Configure the python environment
1.1 Install Anaconda
Enter the official website of anaconda: https://www.anaconda.com/Click
download to download the file, here I am Anaconda3-2022.10-Windows-x86_64.exe
(subsequent update version exe file will be different)
After downloading, open .exe
the file to download anaconda:
Select the installation path (you can also use the default path):
Choose both here:
Then install it.
1.2 Check the environment installation is successful
Open cmd and enter conda (if it is as shown in the figure below, it means that anaconda is installed successfully):
enter python, here you can check the version of Python:
1.3 Create a virtual environment
Enter in cmd conda create -n 环境名 python==版本号
, such as: conda create -n pytorch python==3.9.13
(here my custom environment is called pytorch, and the python version is 3.9.13)
entery
This is how it is created:
1.4 Enter/exit the environment just created
Enter the environment: enter conda activate 环境名
, such as conda activate pytorch
Exit the environment: enterconda deactivate
1.5 Other operations
1.5.1 View all created environments on the computer
conda info --env
1.5.2 Delete the created environment
conda remove -n 环境名 --all
2 Install CUDA and CUDNN
2.1 Check the CUDA version supported by your computer
You can refer to: How to check the highest version compatible with the current version of CUDA on your computer
This means that the CUDA version that can be installed on my computer can be 11.7.1 or below.
2.2 Install CUDA
It is recommended to go to the pytorch official website to see the CUDA version that can be installed directly with instructions (mainly for the convenience of subsequent operations). In addition, https://pytorch.org/get-started/previous-versions/ also provides installation of previous versions instruction:
Looking at Compute Platform, there are CUDA 11.6
and CUDA 11.7
, and the highest CUDA version supported by my computer is 11.7.1, so CUDA version 11.6 or 11.7 will be installed in subsequent installations.
Go to CUDA official website: https://developer.nvidia.com/cuda-toolkit-archive
Here we take version 11.7.0 as an example:
Choose as follows (Installer Type can be both, because it is a foreign website, download from the external network, if the speed is slow, choose exe(network)
):
Execute the .exe file after downloading.
Choose whether to change the installation path according to your own needs:
Choose Custom:
Don't worry about this, 下一步
just click:
Select the installation location (here I changed the path, you can use the default one, remember this path, you will use it later ):
So far the CUDA installation is over.
2.3 Install CUDNN
Enter the official website: https://developer.nvidia.com/rdp/cudnn-download
First log in/register an account, and then choose as follows:
Unzip the downloaded zip file, the contents are as follows:
Find the installation location you selected when installing CUDA just now:
Then copy all the files in bin
the folder .../CUDA/v11.7/bin
to ; copy all the files in the folder in
CUDNN to ; copy all the files in the folder in CUDNN to .include
.../CUDA/v11.7/include
lib/x64
.../CUDA/v11.7/lib/x64
So far the installation of CUDNN is over.
2.4 Check that CUDA is successfully installed (check GPU usage, video memory usage)
Enter in cmd nvidia-smi
, and this command can also view some information of the GPU. If the following interface appears, it means that the CUDA installation is successful, which is the best (but it seems that some computers will report an error, which does not necessarily mean that the CUDA installation has failed 'nvidia-smi'不是内部或外部命令
. , you can search for the solution, or ignore it for the time being, and continue to do it later, even if the installation fails, it will not affect subsequent operations. After installing pytorch, you can use other codes to check whether CUDA is available).
3 Install PyTorch
3.1 Install PyTorch
Enter the official website: https://pytorch.org/
Click Install:
Select as follows (the CUDA version should correspond), and the instructions Run this Command
in will be used later:
Open cmd, enter the previously created Python environment, and enter conda activate 环境名
, such as:conda activate pytorch
Enter the command after entering the environment (from the picture above): conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
(Here you need to hang up the external network, otherwise the installation will be very slow. If you have no conditions, please move to [3.3 Other methods].Note, do not use Tsinghua source like some tutorials, pytorch installed with Tsinghua source does not have a GPU version, it all depends on the CPU, the computing power of the GPU and the CPU is very different, and the CPU can slow down when running deep learning code )
Enter y
:
Then just wait for the installation, the installation is like this:
3.2 Check whether the installation is successful
You can enter the following commands:
import torch
print(torch.version.cuda) # 查看 CUDA 版本
print(torch.cuda.is_available()) # 查看 CUDA 是否可用(即训练时是否可用 GPU)
print(torch.cuda.device_count()) # 查看可行的 CUDA 数目
3.3 Other methods
If there is no accelerator and cannot connect to the external network, the operation will be more troublesome. Here again! ! ! Don't use Tsinghua source like some tutorials. The PyTorch installed with Tsinghua source does not have a GPU version, and it all depends on the CPU. It is very slow to use the CPU when running deep learning code.
Select the Pip method on the PyTorch official website and find the following URL:
Then we enter and open this URL, which is https://download.pytorch.org/whl/cu117
Inside is this:
The file we need to download ourselves is torch
, torchvision
,torchaudio
Then we click to enter torch
and find the following location (find the corresponding file according to your own CUDA version and python version, cu117 refers to CUDA version 11.7, cp39 refers to python version 3.9, the version must be corresponding), click to download :
torchvision
Same torchaudio
as:
Then we put these three .whl files in a random folder:
Then open cmd, go to this folder, and pip install 刚刚下载的文件
install :
pip install torch-1.13.1+cu117-cp39-cp39-win_amd64.whl
pip install torchvision-0.14.1+cu117-cp39-cp39-win_amd64.whl
pip install torchaudio-0.13.1+cu117-cp39-cp39-win_amd64.whl
4 Using PyTorch in PyCharm
First create a new project, follow the steps below:
Then click Create
Create Project
The use of PyTorch can be tested with the following code:
import torch
print(torch.version.cuda) # 查看 CUDA 版本
print(torch.cuda.is_available()) # 查看 CUDA 是否可用(即训练时是否可用 GPU)
print(torch.cuda.device_count()) # 查看可行的 CUDA 数目
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
print(torch.cuda.get_device_name(0))
print(torch.rand(3, 3).cuda())
5 Remote Linux server configuration PyTorch
Remote server configures Anaconda and installs PyTorch detailed tutorial