Set up GPU Accelerated Tensorflow & Keras on Windows 10 with Anaconda

Ankit Bhatia
5 min readJan 26, 2018

In this post I will outline how to configure & install the drivers and packages needed to set up Keras deep learning framework on Windows 10 on both GPU & CPU systems.

Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. It was developed with a focus on enabling fast experimentation.

Use Keras if you need a deep learning library that:

  • Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility).
  • Supports both convolutional networks and recurrent networks, as well as combinations of the two.
  • Runs seamlessly on CPU and GPU.

Here we will use tensorflow as a backend for Keras,

Environment

I am using below configurations which may vary for you:

Acer Predator Helios 300:

  • Windows 10 Professional
  • NVIDIA GeForce GTX 1060 6GB GDDR5
  • Core i7–7700 HQ
  • 16GB DDR4 RAM.

Setting up Backend for Tensorflow

Requirements to run TensorFlow with GPU support:

  1. Download & Install the latest version of Anaconda

2. Download & Install Visual Studio 2015

Visual Studio 2015 is not the latest version but mandatory! Signup and registration with Visual Studio Dev Essentials are required to download older version.

3. CUDA Toolkit 9.0

The NVIDIA® CUDA® Toolkit provides a development environment for creating high performance GPU-accelerated applications.

CUDA site will show the latest(currently v10) to download. Please make sure Tensorflow requires CUDA 9.0. You can download CUDA 9.0 from here. You need to sign up to download the older version.

Note : You may get the following warning, this message appears because the installer searches for ‘compatible graphics hardware’ that was released before the installation program was made. Thus, any newer video cards released recently will trigger that message, because they have not been hard-coded as ‘compatible hardware’ in NVIDIA’s installation binary.

Thus it is safe to ignore this message on any recent card, as it will be CUDA enabled.

4. cuDNN v 7.0 for CUDA 8.0

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers.

You can Download cuDNN v7.1.4 (May 16, 2018), for CUDA 9.0

The cuDNN library contains three files: \bin\cudnn64_7.dll (the version number may be different), \include\cudnn.h and \lib\x64\cudnn.lib. You should copy them to the following locations:

%CUDA_Installation_directory%\bin\cudnn64_7.dll

% CUDA_Installation_directory %\include\cudnn.h

% CUDA_Installation_directory %\lib\x64\cudnn.lib

By default, % CUDA_Installation_directory % points to

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0

5. Environment Variables

Add the following entries in Environment Variables > System variables > Path:

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\libnvvp
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64

6. Restart the machine.

Install Tensorflow

  1. Create Tensorflow Environment

Open Anaconda Prompt, enter the following command

conda create --name tensorflow --clone root

— clone root will inherit the libraries from default python to tensorflow environment.

;;

Note : At many places you may find following command ‘ conda create -n tensorflow python=3.5’ but it will not inherit the default libraries from default python to tensorflow environment you have to explicitly install all the libraries under tensorflow environment such as numpy, pandas, sklearn etc.

Edit: If you are getting some warning of “ SafetyError:” while running the above command, that is probably a bad package installed at your root environment. You can run below commands and create the environment again.

conda env remove --tensorflow        #Remove the environment
conda clean --packages --tarballs # Clean unused cached packages.

2. Install Tensorflow for GPU

Enter the following commands:

activate tensorflow

pip install --ignore-installed --upgrade tensorflow-gpu

Note: If you want to install a speific version of tensorflow, you can instead use pip install tensorflow-gpu==1.8.0

3. Validate your installation

Open Jupyter Notebook, under tensorflow environment by running the following commands on Command Prompt

call activate tensorflow
jupyter notebook

Enter the following commands in Jupyter Notebook

import tensorflow as tfprint(tf.__version__)1.10.0

Install Keras

Open Anaconda Prompt, open tensorflow environment by using ‘activate tensorflow environment’ & enter the following command

conda install keras

Validate your installation by running the following commands in Jupyter Notebook.

import kerasUsing TensorFlow backend.print(keras.__version__)2.1.6

How to check if the code is running on GPU or CPU?

According to the documentation.

If you are running on the TensorFlow or CNTK backends, your code will automatically run on GPU if any available GPU is detected.

You can check what all devices are used by tensorflow by -

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 8320990378049208634
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 4952267161
locality {
bus_id: 1
links {
}
}
incarnation: 2490779436580148339
physical_device_desc: "device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1"
]

Alternative : TensorFlow with CPU support only

Alternatively, if you want to install Keras on Tensorflow with CPU support only that is much simpler than GPU installation, there is no need of CUDA Toolkit & Visual Studio & will take 5–10 minutes.

You just need to the following steps:

  1. Download & Install the latest version of Anaconda
  2. From Anaconda Prompt run the following commands

conda create — name tensorflow — clone root

activate tensorflow

pip install --upgrade tensorflow

conda install keras

3. Validate your installation.

Summary

This article gives you a starting point for building a deep learning setup running with Keras and TensorFlow both on GPU & CPU environment.

Like and share if you find this helpful!

Connect with me on Linkedin.

--

--