How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide


Introduction

Hello tech enthusiasts! Pradeep here, your trusted source for all things related to machine learning, deep learning, and Python. As you know, I’ve previously covered setting up TensorFlow on Windows. Today, I’m excited to bring you a detailed guide on setting up another popular deep learning framework, PyTorch, with GPU support on Windows 11.

In this modern era of AI and ML, PyTorch has emerged as a go-to framework for researchers and developers alike, thanks to its dynamic computation graph, ease of use, and a thriving community. Whether you are a seasoned deep learning practitioner or a curious beginner, PyTorch has something for everyone.

In this article, I will walk you through the process of installing and configuring PyTorch with GPU support on your Windows 11 machine, specifically tailored for an AMD Ryzen 2970WX CPU and NVIDIA RTX 2070 GPU. By the end of this tutorial, you will have a fully functional PyTorch environment, ready to power your next AI project.

So, let’s dive right in and set up PyTorch with GPU support on Windows 11, ensuring you can harness the full potential of your hardware to accelerate your deep learning experiments!

My Setup – A High-Performance Environment for Deep Learning

In this tutorial, I will be using a powerful hardware configuration designed for optimal deep learning performance. The components I’ve chosen provide an excellent balance between computing power, speed, and affordability. Here’s a quick overview of my setup:

  1. Operating System: Windows 11 Windows 11 is the latest and most advanced version of the popular Windows operating system. With improved performance, better security, and enhanced compatibility, Windows 11 offers a great foundation for our deep learning setup.
  2. CPU: AMD Ryzen 2970WX The AMD Ryzen Threadripper 2970WX is a 24-core, 48-thread processor that delivers outstanding multitasking capabilities and exceptional performance for complex workloads. With a base clock speed of 3.0 GHz and a max boost clock speed of 4.2 GHz, this CPU ensures that our PyTorch installation runs smoothly and efficiently.
  3. Motherboard: ASUS ROG X399 The ASUS ROG X399 motherboard is built to handle the power of AMD’s Ryzen Threadripper processors. With support for quad-channel DDR4 memory, multiple PCIe slots, and extensive connectivity options, this motherboard provides a solid foundation for our high-performance setup.
  4. GPU: NVIDIA RTX 2070 The NVIDIA GeForce RTX 2070 is a powerful graphics card that accelerates deep learning tasks using its Turing architecture and 8 GB of GDDR6 memory. With 2304 CUDA cores and Tensor Cores for AI workloads, the RTX 2070 enables faster training times and smoother experimentation with PyTorch.
  5. Python and Environment Management: Miniconda Miniconda is a minimal distribution of the popular Anaconda Python distribution. It provides a lightweight yet powerful environment for managing Python packages and virtual environments. In this tutorial, we will be using Miniconda to install and manage our PyTorch environment, ensuring that all dependencies are properly isolated and maintained.

With this high-performance setup in place, we are now ready to dive into the process of installing and configuring PyTorch with GPU support on Windows 11. In the following sections, we’ll go step by step through the installation process, ensuring that you can harness the full potential of your hardware for accelerated deep learning experiments.


Summary of Installation Steps

In this blog post, we will guide you through the process of setting up PyTorch with GPU support on Windows 11. Here’s a summary of the installation steps we will cover:

  1. System Prerequisites:
    • Update your system to the latest version of Windows 11.
    • Ensure that your GPU drivers are up to date.
  2. Install Miniconda:
    • Download the latest version of Miniconda for Windows.
    • Follow the installation instructions to set up Miniconda on your system.
  3. Create a Conda Environment:
    • Create a new Conda environment to manage the PyTorch installation and its dependencies separately from your base Python installation.
  4. Setup CUDA Toolkit (11.7) for Windows 11
    • Install the CUDA Toolkit by visiting the Nvidia Downloads page
  5. Install PyTorch with GPU Support:
    • Use the official PyTorch installation command to install the appropriate version of PyTorch with GPU support in your new Conda environment.
  6. Install Additional Packages (Optional):
    • Install any additional Python packages that you may need for your deep learning projects, such as NumPy, Pandas, or Matplotlib.
  7. Verify the Installation:
    • Test your PyTorch installation by running a simple script to ensure that it is functioning correctly and utilizing your GPU.

By following these steps, you will have a fully functional PyTorch environment with GPU support on your Windows 11 machine, ready to power your next AI project.

Step 1 – System Prerequisites

Before we begin the installation process, it is crucial to ensure that your system meets the necessary requirements and prerequisites. This will help avoid any compatibility issues or performance bottlenecks during the installation and usage of PyTorch. Here’s what you need to do:

  1. Update Windows 11: To start, make sure your system is running the latest version of Windows 11. Microsoft regularly releases updates to improve performance, stability, and security. To check for updates, follow these steps:
    • Click on the “Start” button and select “Settings.”
    • Choose “Windows Update” from the left sidebar.
    • Click on “Check for updates” and install any available updates.
    • Restart your system if required.
  2. Update GPU Drivers: Having up-to-date GPU drivers is essential for optimal performance and compatibility with PyTorch. To update your NVIDIA RTX 2070 GPU drivers, follow these steps:
    • Visit the NVIDIA Driver Downloads page (https://www.nvidia.com/Download/index.aspx).
    • Select your GPU model (GeForce), series (RTX 20 Series), and specific model (RTX 2070).
    • Choose “Windows 11 64-bit” as your operating system and click on “Search.”
    • Download the latest driver version and run the installer.
    • Follow the on-screen instructions to complete the installation.
    • Restart your system if required.

By completing these two prerequisites, you ensure that your Windows 11 system and NVIDIA GPU are ready for a smooth PyTorch installation. In the next section, we will install Miniconda to manage our Python environment and dependencies.

Step 2 – Install Miniconda

Miniconda is a lightweight distribution of the popular Anaconda Python distribution, which provides an efficient way to manage Python packages and virtual environments. In this step, we will guide you through the process of installing Miniconda on your Windows 11 system.

  1. Download Miniconda Installer: Visit the Miniconda Downloads page (https://docs.conda.io/en/latest/miniconda.html) and download the latest version of the Miniconda installer for Windows. Make sure to choose the Python 3.x version (64-bit) suitable for your system.
  2. Run the Miniconda Installer: Locate the downloaded Miniconda installer (usually in your “Downloads” folder) and double-click on the executable file (.exe) to start the installation process.
  3. Follow the Installation Steps: During the installation, follow these recommendations:
    • Choose “Just Me (recommended)” when asked to select the installation type.
    • Accept the default installation location or choose a custom path if you prefer.
    • Check the box to “Add Miniconda3 to my PATH environment variable.” Although not recommended by the installer, this step will simplify the usage of Conda commands in the command prompt.
    • Click on “Install” to start the installation.
  4. Complete the Installation: Once the installation is complete, click on the “Finish” button to close the installer.
  5. Verify the Installation: To verify that Miniconda has been installed correctly, open a new command prompt by pressing Win + X and selecting “Windows Terminal” or “Command Prompt.” Type conda --version and press Enter. If the installation was successful, you should see the Conda version displayed in the terminal.

Now that Miniconda is installed, you can use it to create and manage Python environments and packages. In the next section, we will create a new Conda environment specifically for our PyTorch installation.

Step 3 – Create a Conda Environment

Creating a separate Conda environment for our PyTorch installation allows us to manage its dependencies independently from the base Python installation. This ensures a clean and isolated workspace for our deep learning projects. In this section, we will guide you through the process of creating a new Conda environment.

  • Open the Command Prompt: Press Win + X and select “Windows Terminal” or “Command Prompt” to open a new terminal window.
  • Create a New Conda Environment: To create a new Conda environment, use the following command, replacing <env_name> with a descriptive name for your environment (e.g., pytorch-gpu):
conda create --name pytorch-gpu-python-3-10 python=3.10

In this example, we’re creating an environment with Python 3.8. You can choose a different Python version if you prefer, but make sure it’s compatible with the PyTorch version you plan to install.

Press Enter and wait for the environment creation process to complete. Conda will automatically install the specified Python version and its essential packages in the new environment. Please make sure to replace with the environment name that woould like to have, example I am using pytorch-gpu-python-3-10 as the name but you could call it something like pytorch-gpu only.

  • Activate the Conda Environment: Before you can use the newly created Conda environment, you need to activate it. To do this, run the following command, replacing <env_name> with the name of your environment:
conda activate pytorch-gpu-python-3-10

Once the environment is activated, you will see the environment name in parentheses at the beginning of the command prompt, indicating that you are currently working in the new environment.

With the Conda environment set up and activated, we can now proceed to install PyTorch with GPU support in the next section.

Step 4 – CUDA Toolkit for Windows 11

You can download and install the CUDA toolkit by visiting this link https://developer.nvidia.com/cuda-11-7-0-download-archive (we are using CUDA toolkit 11.7 – as of this writing pytorch 2.0 works with CUDA 11.7). As of this writing I am using CUDA Toolkit version 11.4. Download the installer (steps shown below – but they could change over time due to changes on Nvidia Website) and just click next until finished โ€“ itโ€™s simple.

Step 5 – Install PyTorch with GPU Support

In this step, we will guide you through the process of installing PyTorch with GPU support in your newly created Conda environment. By the end of this section, you will have a fully functional PyTorch installation, ready for GPU-accelerated deep learning.

  1. Visit the PyTorch Official Website: Go to the PyTorch official website (https://pytorch.org/get-started/locally/) and navigate to the “Install” page. Here, you will find a helpful installation command generator that provides the appropriate command based on your preferences and system specifications.
  2. Select the Installation Preferences: Choose the following options in the installation command generator:
    • Package: Select “PyTorch.”
    • Language: Choose “Python.”
    • CUDA: Select the version compatible with your NVIDIA GPU and installed CUDA toolkit. For the NVIDIA RTX 2070, a common choice would be “CUDA 11.7” (if you have the corresponding version of the CUDA toolkit installed).
    • Platform: Pick “Windows.”

The generator will produce an installation command that looks like this:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

Please note that the generated command may vary based on the selected options.

  1. Install PyTorch in the Conda Environment: Make sure your desired Conda environment is activated (you should see the environment name in parentheses at the beginning of the command prompt). Copy and paste the generated installation command into the terminal and press Enter.

Wait for the installation process to complete. Conda will automatically download and install PyTorch and its dependencies, including the appropriate GPU-accelerated version of the PyTorch library.

Once the installation is complete, you will have successfully set up PyTorch with GPU support in your Conda environment. In the next section, we will discuss installing additional packages, and then we will verify that the installation is functioning correctly.

Step 6 – Install Additional Packages (Optional)

While PyTorch provides the core functionality required for deep learning, you may want to install additional Python packages to enhance your workflow and improve productivity. In this section, we will discuss installing some popular packages often used alongside PyTorch, including Jupyter Notebook for interactive coding and visualization.

  1. Install Jupyter Notebook: To install Jupyter Notebook in your Conda environment, run the following command:
conda install -c conda-forge notebook

Wait for the installation process to complete. Conda will automatically download and install Jupyter Notebook and its dependencies.

  1. Add Conda Environment Kernel to Jupyter: To use the current Conda environment as a kernel in Jupyter Notebook, you first need to install the ipykernel package by running:
conda install -c conda-forge ipykernel

Once the ipykernel package is installed, add the Conda environment as a new kernel by running:

python -m ipykernel install --user --name pytorch-gpu-python-3-10 --display-name "Pytorch GPU (Python 3.10)"

Replace "pytorch-gpu-python-3-10" with the name of your Conda environment (e.g., pytorch-gpu) and "Pytorch GPU (Python 3.10)" with a descriptive name that you like that will appear in the Jupyter Notebook interface (e.g., “PyTorch GPU”).

  1. Install Additional Deep Learning Packages: You can install other popular deep learning packages, such as NumPy, Pandas, Matplotlib, or scikit-learn, by running the following command:
conda install -c conda-forge numpy pandas matplotlib scikit-learn

Feel free to customize this command to include any other packages you may need for your projects.

After completing these steps, you will have a comprehensive deep learning environment equipped with PyTorch, Jupyter Notebook, and various additional packages. In the next section, we will verify that the PyTorch installation is functioning correctly and utilizing the GPU.

Step 6 – Verify the Installation

In this final step, we will verify that our PyTorch installation is functioning correctly and utilizing the GPU. We will create a simple script in a Jupyter Notebook to ensure that everything is set up as intended.

  1. Activate the Conda Environment (if not already active): If you haven’t already, activate your Conda environment by running the following command in the terminal, replacing <env_name> with the name of your environment:
conda activate pytorch-gpu-python-3-10
  1. Launch Jupyter Notebook: With the Conda environment activated, start the Jupyter Notebook by running:
jupyter notebook

Your default web browser should automatically open, displaying the Jupyter Notebook interface. If it does not open automatically, copy the URL displayed in the terminal (e.g., http://localhost:8888/?token=...) and paste it into your browser’s address bar.

  1. Create a New Notebook: In the Jupyter Notebook interface, click the “New” button in the top right corner and select the kernel corresponding to your Conda environment (e.g., “PyTorch GPU”). A new notebook will open with the selected kernel.
  2. Verify the PyTorch Installation and GPU Support: In the first cell of the new notebook, enter the following code:
import torch

# Check if PyTorch is using the GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"PyTorch is using: {device}")

# Print PyTorch version
print(f"PyTorch version: {torch.__version__}")

# Test a simple PyTorch tensor operation on the GPU
x = torch.tensor([2.0, 3.0], device=device)
y = torch.tensor([4.0, 5.0], device=device)
result = x * y
print(f"Result of tensor operation: {result}")

Press Shift + Enter to run the cell. If the installation is successful and the GPU is being utilized, you should see output similar to the following:

PyTorch is using: cuda
PyTorch version: x.y.z
Result of tensor operation: tensor([ 8., 15.], device='cuda:0')

The output confirms that PyTorch is installed correctly, using the GPU for computations, and performing basic tensor operations without any issues.

With these verification steps complete, you now have a fully functional PyTorch environment with GPU support on your Windows 11 system, ready to power your next deep learning project. Happy coding!

Conclusion

In this tutorial, we have walked you through the process of setting up PyTorch with GPU support on a Windows 11 system. We covered the necessary system prerequisites, such as updating Windows 11 and GPU drivers, and installing Miniconda to manage Python environments and dependencies. We then demonstrated how to create a Conda environment, install PyTorch with GPU support, and set up additional packages like Jupyter Notebook.

By following these steps, you have successfully configured a dedicated deep learning environment, complete with the powerful PyTorch library and GPU acceleration. This will enable you to harness the full potential of your hardware for faster model training and experimentation. I hope that this tutorial has provided you with a solid foundation for your future deep learning projects using PyTorch on Windows 11.

Happy coding, and best of luck with your AI endeavors!

Other posts

  • Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Discover how to perform object extraction using image segmentation with Detectron2 and Mask2Former in our step-by-step tutorial. Learn to set up the environment, configure the model, and visualize segmentation results, extracting objects from images with ease. Boost your computer vision skills and optimize your image processing projects with this comprehensive guide.

  • Building Your First Neural Network with TensorFlow – Deep Learning 2

    Building Your First Neural Network with TensorFlow – Deep Learning 2

    Neural networks are a fundamental concept in deep learning and are used for a wide range of applications such as image and speech recognition, natural language processing, and much more. In this article, we will walk you through the process of building your first neural network using TensorFlow, a popular open-source machine learning library. We'll…

  • Introduction to Deep Learning with TensorFlow – Deep Learning 1

    Introduction to Deep Learning with TensorFlow – Deep Learning 1

    In this article, we provide an introduction to deep learning with TensorFlow. We cover what deep learning is, what it can do, why TensorFlow is a great choice for deep learning, and an overview of TensorFlow itself. We also explore the different types of neural networks used in deep learning, and demonstrate how to build…

  • How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    Introduction Hello tech enthusiasts! Pradeep here, your trusted source for all things related to machine learning, deep learning, and Python. As you know, I’ve previously covered setting up TensorFlow on Windows. Today, I’m excited to bring you a detailed guide on setting up another popular deep learning framework, PyTorch, with GPU support on Windows 11.…

  • Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    In this article, we demonstrate how to solve a logistics optimization problem using the Pulp library in Python. By defining the variables, objective function, and constraints, and using the solve method to find the optimal solution, we are able to minimize the total cost of transportation while satisfying the constraints. This article concludes the multi-part…

  • Linear Programming in Python using PuLP โ€“ Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    Linear Programming in Python using PuLP โ€“ Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    In this article, we used the Pulp library in Python to solve a linear programming problem to find the optimal investment portfolio. We defined variables, added constraints, defined objectives, and solved the problem to find the optimal solution that balances the trade-off between maximizing returns and minimizing risk. The code was concise and easy to…

  • Linear Programming in Python using Pulp – Part 2

    Linear Programming in Python using Pulp – Part 2

    In this article, we delve deeper into linear programming and explore how to solve a multi-objective optimization problem using the Pulp library in Python. We present a problem in which a nutritionist must find the optimal meal plan for a patient suffering from anemia, balancing the intake of Vitamin B12 and fat. We demonstrate how…

  • Linear Programming in Python using PuLP – Part 1

    Linear Programming in Python using PuLP – Part 1

    Linear programming is an optimization technique used to find the best outcomes for a given problem. This technique relies on a set of constructs which are all expressed using a system of linear equations. It is important to understand that you should be able to express your objective as a linear equation dependent on an…

  • How To: Setup Tensorflow With GPU Support using Docker

    How To: Setup Tensorflow With GPU Support using Docker

    Previously I published a guide for setting up tensorflow in an anconda environment with GPU support. A lot of people liked it and I have been working with this environment myself for more than a year now. I am happy with the results however the process is a bit involved and requires quite a bit…

  • How To: Setup Tensorflow With GPU Support in Windows 11

    How To: Setup Tensorflow With GPU Support in Windows 11

    It's been just 2 days since Windows 11 came out and I am already setting up my system for the ultimate machine learning environment. Today we are going to setup a new anaconda environment with tensorflow 2.5 with GPU support using NVIDIA CUDA 11.4 and CUDNN 8.2.4 along with Python 3.8. This is going to…

  • Tools of The Trade – II

    Tools of The Trade – II

    In continuation of my previous post today I will talk about the website tanooja.com. I did this project on request of my wife because she wanted to pursue blogging and didn't want to go through the ordeal needed to write, publish and manage SEO using most of the prominent blogging platforms like WordPress, Joomla, Drupal…

  • Tools of The Trade – I

    Tools of The Trade – I

    In this post I will share a few tools and technologies that I am using to run a couple of blazing fast websites using latest modern tools and technologies. The caveat here is that I don't pay any infrastructure/hosting costs for any of these websites and they can scale infinitely in terms of supported users…

  • Building Lizzie – IV

    Building Lizzie – IV

    Another post about Lizzie. I started off with a Raspberry Pi 3 to build a personal assistant for my car and I have come a long way both in terms of the concept and the functionality. Most importantly I have formalized the application flow and also extended the scope from one device to almost all…

  • OBD-II with Raspberry Pi3

    OBD-II with Raspberry Pi3

    I am writing this article in response to a question posted on my YouTube channel. Here I would be talking about communicating to an OBD-II device (ELM327 chip with Bluetooth) hooked into your car’s OBD-II port. The OS I am using is Windows 10 IoT core. This information is important because it makes a difference…

  • Building Lizzie – III

    Building Lizzie – III

    As mentioned in previous article today I would be talking about OBD-II integration in Lizzie using a Bluetooth serial communication with an ELM327 adapter that fits on a OBD-II port in your car. OBD stands for On Board Diagnostics which is connected to the ECU (Engine Control Unit) and provides a ton of information (both…

  • Building Lizzie – II

    Building Lizzie – II

    In the previous post I described my experiments around building an intelligent artificial personal assistant – Lizzie. The pseudo intelligent agents available today around us (Siri, Cortana or Google Next) are all great feats of engineering given the fact that they reside on small devices like mobile phones and are able to do powerful things…

  • Building Lizzie – I

    Building Lizzie – I

    Recently I have been busy building a personal assistant that I would be fitting in my car. Currently I am in experimentation mode and I am experimenting with speech capabilities. I would start with a description of my journey so far. First let me show off a little bit with these videos that I created…

  • How To: Super Fast Tensorflow 2 Setup with GPU Support in VS Code

    I am publishing this article in response to a comment I received on How To: Setup Tensorflow With GPU Support using Docker article. Docker is all good and nice but all of us agree that we use IDEs (integrated dev environment – think VSCode) on our machines. So let’s say you want to get the…

Discover more from The Geek's Diary

Subscribe now to keep reading and get access to the full archive.

Continue reading