DATA SCIENCE/TENSORFLOW

The Easy-Peasy Tensorflow-GPU Installation(Tensorflow 2.1, CUDA 11.0, and cuDNN) on Windows 10

The simplest way to install Tensorflow GPU on Windows 10.

Bipin P.
The Startup
Published in
4 min readSep 20, 2020

--

Installing Tensorflow for GPU is an immensely complicated task that will drive you crazy. There are n-number of tutorials online that claims their way of doing things is the most efficient one. Despite their presence, I had a hard time getting stuff done as installing Tensorflow 2.1.0 is a bit different than its predecessor(Tensorflow 1). A minor difference in code will trigger AttributeError. So once I have succeeded, the very thought was to share my experience as a blog elaborating on the process.

The Easy Ways of Installation

The below-mentioned steps will definitely make your life easy:

  • To start with, it is advisable to verify your GPU as a CUDA compatible one. You can verify it here.
  • If Python is not yet installed, you may download it here.
  • Once GPU is found to be compatible, you are required to download the CUDA toolkit from the NVIDIA website. It’s mandatory to restart the OS(Windows 10) after installing the toolkit.
  • Open the Environment Variables by typing the term ‘environment variables’ in Windows 10 search bar in the taskbar, and select ‘Edit the system environment variables’. After installing the CUDA toolkit, I have to manually enter the CUDA_HOME variable. The other two variables-CUDA_PATH and CUDA_PATH_V11_0 were already present in the System variables list. Note that the three variables viz. CUDA_HOME, CUDA_PATH, and CUDA_PATH_V11_0 have the same variable value(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0). If properly installed the System variables will have the three variables(paths) as highlighted by the red stroke as shown below:
  • You must register on the NVIDIA developer website in order to be to download cuDNN(NVIDIA CUDA® Deep Neural Network library). cuDNN is a GPU-accelerated library of primitives for deep neural networks. Once logged in as a registered user, download the cuDNN.
  • The zip file should be extracted to the location where CUDA toolkit was installed. Some files will be overwritten.
  • Open Anaconda prompt and create a new environment named ‘tf_gpu’.
conda create --name tf_gpu
  • If you want to use a particular version of Python, modify the above command to include the python version(for example, python=3.6):
conda create --name tf_gpu python=3.6
  • Activate the newly created environment.
activate tf_gpu
  • Here goes the command to install TensorFlow:
conda install tensorflow-gpu

Verifying the Installation

To verify that TensorFlow has been properly installed, follow the following steps:

  • Go to Anaconda prompt and type the command ‘pip3 show tensorflow’. On the successful installation of TensorFlow, the command will provide all the required details(like version, location, etc.). Note that you should be in the same environment in which you have installed TensorFlow.
  • Open Anaconda Navigator and select the appropriate environment(as shown in red stroke in the image below). Search for ‘tensorflow’ in Search Packages(shown by blue stroke). The packages will be listed if the installation was successful.
  • Now open Jupyter Notebook from either Anaconda Navigator or from Anaconda prompt. Ensure that the appropriate environment is selected. Try the following commands:
import tensorflow as tf
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

The Errors

A faulty installation of TensorFlow will return errors on executing these commands. The most common error that is encountered is:

ImportError: No module named tensorflow

The most widely seen command in StackOverflow to assess the TensorFlow GPU installation is:

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

TensorFlow 2 will return AttributeError on executing this command as tf.Session is not supported in TensorFlow 2:

AttributeError: module 'tensorflow' has no attribute 'Session'

On modifying the command by replacing ‘tf.Session’ by ‘tf.compat.v1.Session’, you will negotiate another AttributeError(as ConfigProto is unsupported by TensorFlow 2):

AttributeError: module 'tensorflow' has no attribute 'ConfigProto'

The ideal solution is to replace ‘tf.ConfigProto’ by ‘tf.compat.v1.ConfigProto’. The tf.compat.v1 is to ensure backward compatibility in TensorFlow 2.

sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

Conclusion

I wish to hope that the installation process discussed here would serve as the silver bullet for the souls who are desperate to get TensorFlow running with CUDA drivers alongside cuDNN library, consequently enhancing and optimizing their workflow. Happy Coding!!!

--

--

Bipin P.
The Startup

Data Science Enthusiast striving to secure greater goals