In this Python, PyTorch, and machine learning tutorial, we explain how to correctly install PyTorch GPU version in Python on a Windows computer and how to test the installation. That is we explain how to install PyTorch on graphics processing units in Windows. This tutorial is based on installing PyTorch GPU in a virtual Python environment. However, everything explained here can be generalized to other methods of running Python, such as for example running Python by using Anaconda. To install PyTorch with GPU capabilities, you have to have a relatively recent GPU capable of running CUDA. The YouTunbe tutorial accompanying this webpage is given below
First, open a Windows Command Prompt, and type:
where Python
The output should look something like this:
C:\Users\aleks\AppData\Local\Programs\Python\Python312\python.exe
Then, let us double-check that the GPU card is detected by the system. To do that, in the terminal type:
nvidia-smi
In my case the output is
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 536.19 Driver Version: 536.19 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 2060 ... WDDM | 00000000:01:00.0 On | N/A |
| 30% 42C P8 7W / 175W | 1171MiB / 8192MiB | 1% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 3128 C+G ...b3d8bbwe\Microsoft.Media.Player.exe N/A |
| 0 N/A N/A 9816 C+G ...aam7r\AcrobatNotificationClient.exe N/A |
| 0 N/A N/A 9880 C+G C:\Windows\explorer.exe N/A |
| 0 N/A N/A 14132 C+G ....Search_cw5n1h2txyewy\SearchApp.exe N/A |
| 0 N/A N/A 15564 C+G ...Brave-Browser\Application\brave.exe N/A |
| 0 N/A N/A 16296 C+G ...ekyb3d8bbwe\PhoneExperienceHost.exe N/A |
| 0 N/A N/A 16360 C+G ...CBS_cw5n1h2txyewy\TextInputHost.exe N/A |
| 0 N/A N/A 17736 C+G ....5536.0_x64__8j3eq9eme6ctt\IGCC.exe N/A |
| 0 N/A N/A 18848 C+G ...5n1h2txyewy\ShellExperienceHost.exe N/A |
| 0 N/A N/A 19104 C+G ...oogle\Chrome\Application\chrome.exe N/A |
| 0 N/A N/A 20820 C+G ....Search_cw5n1h2txyewy\SearchApp.exe N/A |
| 0 N/A N/A 20860 C+G ...crosoft\Edge\Application\msedge.exe N/A |
| 0 N/A N/A 21228 C+G ...n\126.0.2592.113\msedgewebview2.exe N/A |
| 0 N/A N/A 24784 C+G ...1.0_x64__8wekyb3d8bbwe\Video.UI.exe N/A |
| 0 N/A N/A 26748 C ...ta\Local\Programs\Ollama\ollama.exe N/A |
| 0 N/A N/A 29808 C+G ...siveControlPanel\SystemSettings.exe N/A |
| 0 N/A N/A 29852 C+G ...cal\Microsoft\OneDrive\OneDrive.exe N/A |
| 0 N/A N/A 32008 C+G ...ft Office\root\Office16\WINWORD.EXE N/A |
+---------------------------------------------------------------------------------------+
This means that you have Python installed on your computer and Python is added to the Windows path.
Next, let us create a Python virtual environment. Create a folder for your virtual environment. In my case, the folder is created like this:
cd\
mkdir codes
cd codes
mkdir testPytorch
cd testPytorch
In this folder we will create our virtual environment.
python -m venv env1
Then, let us activate this environment by typing:
env1\Scripts\activate.bat
This will activate the environment. Next, go to
https://pytorch.org/get-started/locally
and select the appropriate option in the table. In our case, we selected Windows, pip, CUDA 12.2 (since the nvdia-smi command returns this number) (your GPU should support any of the versions listed in the table).
The resulting installation command will appear at the bottom of the table, and looks like this:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
Execute this command in the Command Prompt and created virtual environment. Next, let us test if the system can use the GPU. We do it like this:
notepad test1.py
To create a new Python file and to open Windows Notepad. Next, type the following code
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
devNumber=torch.cuda.current_device()
print(f"Current device number is: {devNumber}" )
devName=torch.cuda.get_device_name(devNumber)
print(f" GPU name is: {devName}")
Save this file, close notepad, and in the virtual environment execute
python test1.py
And you should see the output, confirming that the CUDA is installed, and printing the name of the GPU.