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 be a handson practical step by step guide to create this environment from scratch with a fresh Windows 11 installation.

I have a decent machine with a decent GPU and I do some hobby machine learning projects. I know getting the CUDA support up and running on tensorflow is such a pain point and that is why we have all these posts and videos on different platforms how to do it properly. This blog post is yet another such post but the idea here is – I am writing this as I am setting up the things and this blog post should help you setup your environment just like I did.

I would argue that recently it has become very simple and straightforward to get up and running on Windows (Windows 11 specifically). Before we get started here are the set of software that you need to install on your machine. Some of this software is optional but I like to have a fully loaded environment so that I don’t have to wait for the software/tools installation later on when I have some awesome idea.

My Environment

My environment is as follows.

  1. Operating System: Windows 11 Pro (everything in artical would work on Windows 11 editions)
  2. Processor: AMD Ryzen Threadripper 2970WX (CPU doesn’t matter)
  3. Motherboard: Asus Rog X399 (Any motherboard is fine)
  4. Graphics Card: NVIDIA GeForce RTX 2070 8GB GDRR6 (any RTX or Quadro GPU is fine)

I have added a new way of running tensorflow with GPU using docker containers, that approach is much simpler and more robust then configuring the entire drivers and CUDA libraries on your local machine. Checkout the article at the below link.

https://thegeeksdiary.com/2023/01/29/how-to-setup-tensorflow-with-gpu-support-using-docker/

Steps

You will need the following installed on your machine.

  1. NVIDIA Graphics Driver: I am using driver version 510.06 gameready drivers. These drivers are special because they also include WSL 2 support which means that I would be able to use the same drivers inside my WSL2 distributions – if you don’t understand WSl 2 – that is fine – you don’t need to. You can download the current version of the drivers from this link.
  2. Visual Studio 2019 – for C++ runtime: You can down it from the official website here. Please note that Visual Studio 2019 community edition is free to download and install. I will explain the installation process below.
  3. Miniconda: You can get the latest version on Miniconda from here. As of this writing I am using Miniconda 3 64 bit version. The version of python included with base environment is 3.9.
  4. CUDA Toolkit for Windows 11: In order for us to be able to use CUDA capable software and libraries we would need the CUDA SDK or CUDA Toolkit as it is often known. This provides us the set of primitives and libraries that interact with the underlying hardware. CUDA Toolkit also includes NVCC compiler which is a specialized C/C++ compiler that compiles code which is able to run CUDA capable GPUs. You can download the CUDA toolkit form this link. As of this writing the current version of CUDA toolkit is 11.4.
  5. CUDNN: CUDNN is a set of C/C++ Libraries that expose the deep neural network capabilities on top CUDA. All deep learning frameworks use CUDNN to use NVIDIA GPUs – including tensorflow.
  6. Create a Conda environment: After installing all the software we will just create a new conda environment.

Step 1: NVIDIA Graphics Driver Installation

I am starting off with a fresh install of Windows 11 so I need to make sure that I have applied all the latest updates. Although not necessary it is good to install them so that any dependencies needed by the software that we will install later is installed smoothly. Once done, you can visit this page to get the latest NVIDIA GPU Driver (note that this is a special driver that adds WSL2 GPU sharing capabilities – you can event install the normal driver for the steps mentioned in this article). Even a simple google search will lead you to this page https://www.nvidia.com/Download/index.aspx. On this page you need to select your graphics card and operating system to be able to download the correct driver. Here is an example selection for my setup.

Select correct options to download your driver.

Once you have downloaded the setup binary – just double click on the file to run it. Just click next as many times you are prompted to :-). Congratulations you have just completed step 1.

Step 2: Visual Studio 2019

As of this writing the stable version of Visual Studio that is available is Visual Studio 2019. I am using the community edition which is a full development environment. If you have any other license (professional or enterprise) that is also fine. Download the visual studio installer from https://visualstudio.microsoft.com/downloads/. Double click the file and make sure that you have the highlighted (“Desktop development with C++”) workloads installed. This will install the required C++ toolchain.

Visual Studio Installer

This will take a while to install depending on the workloads that you have selected. wait for it to finish. You might have to restart your computer before going on to next step.

Step 3: Miniconda

Anaconda is a python distribution that allows you to have several python environments on one machine. This gives you the freedom to work with multiple isolated versions of python without relying on a global environment which might have package conflicts. Isolation is always good. Head over to https://docs.conda.io/en/latest/miniconda.html to download a trimmed version of Anaconda otherwise known as Miniconda. Download the installer and finish the steps with the regular options.

Download Miniconda Installer

Step 4: CUDA Toolkit for Windows 11

You can download and install the CUDA toolkit by visiting this link https://developer.nvidia.com/cuda-downloads. As of this writing I am using CUDA Toolkit version 11.4. Download the installer and just click next until finished – it’s simple.

Select CUDA Toolkit for your platform

Step 5: CUDNN Library

Now it’s time for us to install the CUDNN library for which is used by tensorflow for creating deep neural networks on GPU using CUDA. Visit the page https://developer.nvidia.com/rdp/cudnn-download and download CUDNN for CUDA Version 11.4 (as of this writing latest version 11.4 for CUDA and 8.2.4 for CUDNN). Please note that you need have a NVIDIA developer account with NVIDIA which is free to create – I guess everybody want have your email address today.

Version selector for CUDNN

Download the zip file as shown above, extract the contents in any directory. Open the extracted folder, and also open another explorer window with the following path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA. Inside this folder you will find a folder for your current CUDA toolkit version, for example for me it is v11.4. Copy the files from the extracted folder like so. In the below tables ALL_CAPS values like these refer to the specific parameters specific to your environment for example CUDA_VERSION refers to the version of CUDA that you downloaded, likewise EXTRACTED_FOLDER refers to the path where you extracted the CUDNN Library zip file.

SourceDestination
<EXTRACTED_FOLDER>\bin\*C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v<CUDA_VERSION>\bin\
<EXTRACTED_FOLDER>\include\*C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v<CUDA_VERSION>\include\
<EXTRACTED_FOLDER>\lib\x64\*C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v<CUDA_VERSION>\lib\x64
File copy mappings

Please note that you need to copy the files of the above folders and not the folders themselves. After this verify that you have the below listed Environment Variables created on you machine.

Environment Variablevalue
CUDA_PATHC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v<CUDA_VERSION>
CUDA_PATH_V<CUDA_VERSION>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v<CUDA_VERSION>
Environment Variables

If you need to know how to access and set environment variables on Windows 11, refer to this article. Although these instructions are for Windows 10 but they still apply to Windows 11.

Step 6: Create a Conda environment

This is now the moment of truth. In this step we will created the Conda environment with Python version 3.8. Let’s start.

Open the Anaconda Prompt by going to start menu.

Start Menu Anaconda Prompt

Once on the prompt run the following commands.

# install Jupyter Notebook in our base environment
conda install -y jupyter

# create a new environment with name 'tensorflow-gpu' and python version 3.8
conda create --name tensorflow-gpu python=3.8
# activate the environment
conda activate tensorflow-gpu
# install python 2.5
pip install tensorflow==2.5
# enable notebook support in our environment
conda install -y -c conda-forge nb_conda
# add the environment to Jupyter Kernel
python -m ipykernel install --user --name tensorflow-gpu --display-name "Python 3.8 (tensorflow-gpu)"
# deactivate the environment
conda deactivate

# create a new directory for our test notebook to check the tensorflow GPU initialization
mkdir test-installation
# go inside the new directory
cd test-installation
# activate our test environment
conda activate tensorflow-gpu
# start jupyter notebook
jupyter notebook

This will open a browser window as shown below.

Jupyter Notebook in our test folder using the new environment

Now create a new notebook by clicking on the “New” toolbar on the right hand corner as shown below, make sure that you select the kernel name as “Python 3.8 (tensorflow-gpu)” – my environment name is “Teflon-GPU-TF (Python 3.8)” but if you followed the above commands yours environment name will be Python 3.8 (tensorflow-gpu). This will open a new blank notebook as shown below.

Jupyter Notebook for Tensorflow Code

Yours notebook will be empty (whereas above has code – which you need to paste from below).

import sys

import tensorflow.keras
import tensorflow as tf
import numpy as np
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")

After running the above code in the notebook you should see an output similar to the one shown below.

Tensor Flow Version: 2.5.0
Keras Version: 2.5.0

Python 3.8.11 (default, Aug  6 2021, 09:57:55) [MSC v.1916 64 bit (AMD64)]
GPU is available

Congratulations you have just completed your environment setup with Miniconda. You can now build great deep learning models using your GPU. Please comment below if you face any issues.

I have added a new way of running tensorflow with GPU using docker containers, that approach is much simpler and more robust then configuring the entire drivers and CUDA libraries on your local machine. Checkout the article at the below link.

https://thegeeksdiary.com/2023/01/29/how-to-setup-tensorflow-with-gpu-support-using-docker/

I have created a series on tensorflow deep learning – checkout the first article in that series here.

https://thegeeksdiary.com/2023/03/23/introduction-to-deep-learning-with-tensorflow-deep-learning-1/
, , , ,


20 responses to “How To: Setup Tensorflow With GPU Support in Windows 11”

  1. Ben Lau Avatar
    Ben Lau

    Thank you so much, I am a beginner of learning ML, I stuck in setup TensorFlow for a long time, your post give me great help to setting up my environment.

    1. Pradeep Singh Avatar

      Hey mate – glad that it helped.

  2. ML Avatar

    I got this error message while importing tensorflow.keras. I tried google but couldn’t find a solution, do you have any idea?

    TypeError: Descriptors cannot not be created directly.
    If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
    If you cannot immediately regenerate your protos, some other possible workarounds are:
    1. Downgrade the protobuf package to 3.20.x or lower.
    2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

    1. JackTing Avatar
      JackTing

      This error message comes from combination of (python 3.7 + TF 2.x) and (Python 3.x + TF 2.8.0)

      To solve this issue, you could upgrade tensorflow 2.8.0 to its bug fix version:
      `pip install –upgrade tensorflow~=2.8.0`
      or down grade the protobuf:
      `pip install –upgrade protobuf~=3.20.0`

      Both will solve this issue.

      Be sure use `~=` to get the leatest bug fixed version.

  3. Penbo Avatar
    Penbo

    So useful. It’s work now 🙂

  4. gato Avatar

    It was very helpful to me. Have a wonderful time!

    1. Pradeep Singh Avatar

      Glad that it worked for you. Have a great time!

  5. Vishnupriya Vijayan Avatar
    Vishnupriya Vijayan

    Thank you so much…at last i could use GPU in Jupiter notebook.

    1. Pradeep Singh Avatar

      Happy to help!

  6. […] 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 […]

  7. HAN Avatar
    HAN

    Thank you so much for creating this page. Your guide is spot on! Cheers!

    1. Pradeep Singh Avatar

      So glad that you found it useful.

  8. duobelt Avatar
    duobelt

    THX – So useful. It works.

    Win 11

    Tensor Flow Version: 2.5.0
    Keras Version: 2.5.0

    Python 3.8.16 (default, Mar 2 2023, 03:18:16) [MSC v.1916 64 bit (AMD64)]
    GPU is available

    1. Pradeep Singh Avatar

      Hey,
      I am glad that you found it helpful. Please check out the guide that I posted later for setting up the GPU tensorflow using docker which is a much simpler process.
      Do let me know how it goes for you. Here is link to the article:
      https://thegeeksdiary.com/2023/01/29/how-to-setup-tensorflow-with-gpu-support-using-docker/

  9. […] 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 […]

  10. Didula Dissanayaka Avatar
    Didula Dissanayaka

    Hi, I followed the instructions but GPU is not detected.

    Tensor Flow Version: 2.5.0
    Keras Version: 2.5.0

    Python 3.8.16 (default, Mar 2 2023, 03:18:16) [MSC v.1916 64 bit (AMD64)]
    GPU is NOT AVAILABLE

    any fix?

    1. Pradeep Singh Avatar

      Hi Didula,

      There could be a lot reasons that could have happened. I will list a few:

      1. missing or incompatible graphics driver
      2. Missing cuDNN libraries or environment variables
      3. Incompatible cuDNN and tensorflow versions.

      It is due to these issues that I recommend that you follow this guide:
      https://thegeeksdiary.com/2023/01/29/how-to-setup-tensorflow-with-gpu-support-using-docker/

      It uses docker to give you consistent setup that you can recreate as many times as you like and it also comes with most machine learning utilities built in. Hope this helps.

  11. […] Setting up Tensorflow with GPU support using Miniconda on Windows 11 along with Jupyter notebooks: This article was published as soon as Windows 11 was available as a preview build and is quite popular and gets quite a number of visits everyday from people trying to get it working on their machines. This guide uses Miniconda to create a virtual environment and I keep it up-to-date as Windows 11 evolves. […]

Leave a Reply

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…

%d bloggers like this: