Install and configure the pytorch deep learning environment in pycharm

Install and configure pycharm in pycharm

Prerequisites

Anaconda and pycharm have been installed

Check your graphics card driver version

Check the driver version to select the cuda software version later

Please add image description

cuda version, here is the actual cuda version, but you can use a lower version of cuda software to manage the higher version. It should be understood that the higher version is compatible with the lower version.

Insert image description here

Choose the appropriate CUDA version

Select the cuda management tool software according to the above graphics card driver version

CUDA 12.1 Release Notes (nvidia.com)

Please add image description

Insert image description here

Download the corresponding pytorch version

There are two pytorch and torchvision that need to be downloaded. Select according to the link below and the CUDA version installed above. There are combinations. Do not choose randomly. See the official website for recommendations (open the link below to view).

https://pytorch.org/get-started/previous-versions/

An example is shown below:

Insert image description here

After determining the combination, download the whl files of pytorch and torchvision according to the link below

https://download.pytorch.org/whl/torch_stable.html

Create a project project

Create a py file in the project

Insert image description here

Create a virtual environment

conda creates a virtual environment named pytorch_gpu: conda create -n pytorch_gpu01 python=3.8

Insert image description here

Activate virtual environment

Activate the virtual environment: conda activate pytorch_gpu

Possible problems during first activation

question

Insert image description here

Solution: Initialize Teminal. The Teminal in python uses the powershell that comes with the computer.

Instruction: conda init powershell

Insert image description here

After initialization, continue to activate the virtual environment:

Insert image description here

If that doesn't work, try changing conda init cmd.exe. The main basis is the type used by Teminal as shown in the screenshot below:

Insert image description here

Install pytorch for virtual environment

Teminal switches the directory to the whl file directory of pytorch and torchvision just downloaded. It is recommended to place it directly under the project to avoid switching, as shown below:

Insert image description here

In Teminal, pip install this torch file. Use the tab key to switch and select files in the current folder, so you don't have to type.

Insert image description here

Insert image description here

The two whl files are installed, indicating that the pytorch toolkit has been installed in the virtual environment. Next, associate the project to the virtual environment, and the project can use the toolkit in the virtual environment.

Associated virtual environments

The created virtual environment will be placed in the directory where anaconda is installed. Some are on the C drive by default, and some are custom-installed on other drives. You can just find it yourself. You need to associate the project with the virtual environment.

Associate the virtual environment, as shown in the figure below, find the envs folder in the anaconda directory, go in and find the virtual environment folder you configured, find the python.exe file, and click to select it. After confirmation, the python project is associated with the virtual environment.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-ztJXkIcl-1678103910260) (pytorch installation.assets/image-20230305171555494.png)]

Test whether the installation is successful

py test code

import torch
print("hello torch{}".format(torch.__version__))

flag = torch.cuda.is_available()
print(flag)

ngpu = 1
# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
print(device)
print(torch.cuda.get_device_name(0))
print(torch.rand(3, 3).cuda())

Test Results

Insert image description here

If it doesn't work, try changing to a different torch version. I also succeeded after changing to a different version.

Delete virtual environment

Open anaconda prompt:
Insert image description here

View the current virtual environment: conda env list

Delete the specified virtual environment: conda remove -n your_env_name (virtual environment name) --all

The virtual environment pytorch_gpu in anaconda is gone too
Insert image description here

Or go directly to anaconda’s envs folder and delete it

Guess you like

Origin blog.csdn.net/2301_76863102/article/details/129369549