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
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.
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)
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:
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
Create a virtual environment
conda creates a virtual environment named pytorch_gpu: conda create -n pytorch_gpu01 python=3.8
Activate virtual environment
Activate the virtual environment: conda activate pytorch_gpu
Possible problems during first activation
question
Solution: Initialize Teminal. The Teminal in python uses the powershell that comes with the computer.
Instruction: conda init powershell
After initialization, continue to activate the virtual environment:
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:
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:
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.
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.
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
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:
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
Or go directly to anaconda’s envs folder and delete it