Setting Up TensorFlow 2 GPU on Windows in 2021

Machine Learning is complicated — but shouldn’t installation be the easiest part?

Leah Hartwell
Analytics Vidhya
5 min readJul 4, 2021

--

Photo by Nana Dua from Pexels

I have a kind of love-hate relationship with TensorFlow but I would like to think over the years this relationship has been growing and improving with each new release. TensorFlow is an insanely powerful platform for machine learning which is why I really do love what they are doing. The hate part of my relationship came in when I tried to install my local TensorFlow V1 environment on Windows with GPU support for the first time and it took me over a day to get it somewhat working. I understand, machine learning is complicated — but shouldn’t installation be the easiest part?

In this guide, I’m going to try to make this process as painless as possible so you can have a loving relationship with TensorFlow right from the start!

TensorFlow 2 (CPU Only)

Anaconda for Python Installation:

I highly recommend installing Anaconda if you have not already installed it as it's really great at managing packages and creating virtual environments which are both important for the next step for installing Tensorflow.

  1. Download Anaconda Individual Edition
  2. Run the executable installer and navigate through steps ← defaults are often good enough. You can “Add Anaconda3 to my PATH environment variables” if you do not already have conflicting distributions of python already installed on your PC.

TensorFlow 2 in Anaconda Installation:

  1. Open an Anaconda Prompt (Anaconda3) Terminal ← search “Anaconda Prompt” in the Start menu and window will pop up and read (base) C:\Users\name>
  2. Run: conda create -n tf pip python=3.9 ← tells Anaconda to make a new virtual environment called "tf" that will be using Python version 3.9
  3. Run: activate tf ← tells Anaconda to run inside virtual environment “tf” and will read (tf) C:\Users\name>
  4. Run: pip install --ignore-installed --upgrade tensorflow==2.5.0 ← installs TensorFlow version 2.5.0 package which includes GPU support as well now (check for newest stable versions)

TensorFlow 2 with GPU Support

This guide, up until this point, has been very similar to others found across the internet and has been pretty straightforward. We now have a Tensorflow environment install which is good enough to use if you only want to train your machine learning models with your CPU. For some, this may be okay, but machine learning is known to work best when training using GPUs as it is much faster than just using your CPU.

Due to this, we will be going through the process to enable GPU Support for TensorFlow 2 in the section below.

Check Compatibility:

TensorFlow GPU Compatibility List ← TensorFlow has specific software compatibility requirements so it is important to make sure you are downloading the correct versions in the next steps.

For TF 2.5.0, CUDA 11.2 and cuDNN 8.1 have been verified by TensorFlow.

Nvidia CUDA Toolkit Installation:

  1. Download CUDA Toolkit 11.2 ← if you need a different version of CUDA for a different TensorFlow version, find it in the CUDA Toolkit Archive
  2. Run the executable installer and navigate through steps ← defaults are often good enough

Nvidia cuDNN Setup:

  1. Create an account for the Nvidia Developer Progam Membership ← needed in order to download cuDNN
  2. Sign in and download cuDNN 8.1 for Windows ← if you need a different version of cuDNN for a different TensorFlow version, find it in the cuDNN Archive
  3. Extract C:\Users\name\Downloads\cudnn-11.2-windows-x64-v8.1.0.77.zip\cuda folder into C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2 folder

Nvidia GPU Driver Update:

  1. Open Nvidia’s Geforce Experience ← you can just type in “Geforce Experience” in the Start Search Bar and it will pop up as an app
  2. Click on the “Drivers” Tab
  3. Under “Available” a driver might be ready to install, if not, click on “Check For Updates” and see if there are any driver updates ready after that

Environment Variables Setup:

  1. Open “System Properties” ← search “Edit system environment variables” from the Start menu
  2. Click on “Environment Variables…” ← under the “Advanced” tab at the bottom right corner
  3. Find and edit Path variable ← under the “System variables” section
  4. Add the following five new Path values:

Verify Full TensorFlow Installation:

  1. Open a new Anaconda Prompt (Anaconda3) Terminal
  2. Run: python -c “import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))”
  3. Correct output should have “Successfully opened dynamic library” and “Created TensorFlow device” flags and will look like this:

… and we’re DONE! That is all that you need to do to set up TensorFlow 2! Whenever you want to use this environment, just open a new Anaconda Prompt (Anaconda3) Terminal and run: activate tf

What’s Next?

In my next articles, I will be going through the installation and setup for creating your own custom object detection models using TensorFlow’s Object Detection API. I will be going through all the code fixes and tweaks I needed to figure out along the way so you don't have to!

I highly recommend looking at TensorFlow 2 Object Detection API Tutorials as it has some more in-depth explanations and is one of the best resources I’ve found so far. In this series of articles, I will be going through similar steps to their tutorial, but helping to fix a few key pieces of code along the way.

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Leah Hartwell
Leah Hartwell

Written by Leah Hartwell

I’m a burnt-out mechanical engineering student figuring out stuff so you don’t have to :’)