avatarBex T.

Summary

The article provides a comprehensive guide to installing TensorFlow 2 GPU on Windows 10, including the necessary software versions and step-by-step instructions for setting up the environment in 2022.

Abstract

The guide begins by acknowledging the difficulties Windows users often face when installing TensorFlow with GPU support. It outlines a series of steps to successfully install TensorFlow 2 GPU on Windows 10, ensuring compatibility with the latest TensorFlow version (2.6.0 at the time of writing). The steps include installing Microsoft Visual Studio, the NVIDIA CUDA toolkit, and cuDNN, as well as configuring the CUDA toolkit in the system's PATH. The article emphasizes the importance of using the correct software versions corresponding to the chosen TensorFlow version and provides links to the necessary resources. It concludes with instructions for setting up a virtual environment with JupyterLab and verifying the GPU's detection by TensorFlow. The author also promotes their Medium membership and an AI service called ZAI.chat as cost-effective alternatives to other services.

Opinions

  • The author empathizes with the struggles of Windows users installing TensorFlow GPU, suggesting a history of complications in the process.
  • The author believes in the efficiency of the provided steps, claiming they "work 100%!" and are a "kindness" to the reader.
  • There is an emphasis on the importance of matching software versions with the TensorFlow version for successful installation.
  • The author encourages readers to create a virtual environment for TensorFlow to avoid conflicts and ensure a clean setup.
  • The author promotes their work and Medium membership, indicating confidence in the quality and uniqueness of their content.
  • The recommendation of ZAI.chat as a cost-effective AI service implies a value proposition comparison with other services like ChatGPT Plus (GPT-4).

How to Finally Install TensorFlow 2 GPU on Windows 10 in 2022

Works 100%!

Photo by Lucas Kepner on. All images are by the author unless specified otherwise.

I think Windows users, including me, have suffered enough. You are probably stumbling on this after trying hours or even days to make this work. So, as a kindness, I will just cut to the chase and show you the steps you need to install TensorFlow GPU on Windows 10 without giving the usual blog intro.

November 27, 2022 update: The guide works for Windows 11, as well as versions of TensorFlow up to 2.11 (the latest one, currently).

Step 1: Find out the TF version and its drivers.

The first, very important step is to go to this link and decide which TF version you want to install. Based on this, the CUDA driver versions and other software versions change.

As of writing this guide, TF 2.6.0 is the latest, and we will be installing that one.

We are only interested in the TF version, cuDNN, and CUDA versions. We keep this tab open and move on to the next step.

Step 2: Install Microsoft Visual Studio

Next, we install Microsoft Visual Studio. Note that this one is different than Visual Studio Code, which is a lightweight IDE so many people love.

Go to this link and press download:

Run the downloaded executable and it will take a minute to download the requirements. Then, it will ask you to choose what workloads to install. We don’t want any, so just click install without workloads and click continue. After the installation is done, it will ask you to sign in but you don’t have to.

Step 3: Install the NVIDIA CUDA toolkit

NVIDIA CUDA toolkit contains the drivers for your NVIDIA GPU. Depending on your Windows, they may or may not be already installed. If installed, we should check their version and see if they are compatible with the TensorFlow version we want to install.

Go to your Settings on Windows and choose “Apps & Features”. Then, search for NVIDIA:

We want to install TF 2.6.0 which requires NVIDIA CUDA Toolkit version 11.2 (see the first link to double-check). If your drivers are any other version, delete all the ones that have “NVIDIA CUDA” in their title (leave the others). Then, go to Local Disk (C:) > Program Files > NVIDIA GPU Computing Toolkit > CUDA. There, you will see a folder with the CUDA version as a name. Delete that folder.

If you search for NVIDIA and no CUDA toolkit is found, go to this page. Here is what it looks like:

Here, we see three 11.2 versions, which are what we need (we got the version from the first TF version link I provided). Click on any of them and choose Windows 10, and download the network installer:

Follow the on-screen prompts and install the drivers with default parameters. Then, restart your computer and come back.

Step 4: Install cuDNN

For TensorFlow 2.6.0, cuDNN 8.1 is required. Go to this page and press download:

It will ask you for an NVIDIA Developer account:

If you don’t have an account already, click “Join now” and enter your email. Fill up the form — the standard stuff. Then, go back to the cuDNN download page:

At the top, it will ask you to fill out a survey. Fill it out and you will be presented with the above page. Click on the first one since it is the one compatible with CUDA Toolkit v. 11.*. There, you will see a Windows version, which you should download.

Step 5: Extract the ZIP folder and copy core directories

Extract the downloaded ZIP folder:

Open the cuda folder and copy the three folders at the top (bin, include, lib). Then, go to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2 and paste them there.

Explorer tells you that these folders already exist, which you should press Replace the files in the destination. That’s it, we are done with the software requirements! Restart your computer once again.

Step 6: Add CUDA toolkit to PATH

Now, it is time to add a few folders to the environment variables. In the last destination, C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2, there is a bin and folder:

Open it and copy the file path. Then, press the “Start” (Windows) button and type “Environment variables”:

Open it and go to “Environment variables”. This will open this pop-up window:

Choose the “Path” from the top and press edit. Press “New” and paste the copied link there.

Then, go back to the GPU toolkit folder and open the libnvvp folder:

Copy its path and paste it to Environment variables just like you did with the bin folder. Then, close all pop-ups, saving the changes.

Step 7: Install TensorFlow inside a virtual environment with Jupyter Lab

Finally, we are ready to install TensorFlow. Create a virtual environment with your preferred package manager. I use conda, so I create a conda environment named tf with Python version 3.8.

conda create -n tf python==3.8
conda activate tf
pip install --upgrade tensorflow
pip install jupyterlab ipykernel

It is important that both TensorFlow and JupyterLab are installed with either pip or conda. You will get a ModelNotFoundError in JupyterLab if they are installed from different channels.

Next, we should add the conda environment to Jupyterlab so that it is listed as a valid kernel when we launch a session:

ipython kernel install --user --name=<name of the kernel, `tf` for our case>

If you launch JupyterLab, you should be able to see the environment as a kernel. Create a new notebook and run this snippet to check if TF can detect your GPU:

import tensorflow as tf
from tensorflow.python.client import device_lib

print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
device_lib.list_local_devices()

As the output says, I have a single GPU and at the end, it shows its name. If you have a similar output, then my job here is done!

Instructions were taken from this live-saver YouTube video.

Loved this article and, let’s face it, its bizarre writing style? Imagine having access to dozens more just like it, all written by a brilliant, charming, witty author (that’s me, by the way :).

For only 4.99$ membership, you will get access to not just my stories, but a treasure trove of knowledge from the best and brightest minds on Medium. And if you use my referral link, you will earn my supernova of gratitude and a virtual high-five for supporting my work.

Artificial Intelligence
Machine Learning
Data Science
TensorFlow
Programming
Recommended from ReadMedium