How to Correctly Install TensorFlow in a GPU Enabled Laptop?

This report shows how to correctly install TensorFlow 2.x in a GPU enabled system with a Windows operating system. Made by Ayush Thakur using W&B
Ayush Thakur

Introduction

TensorFlow is one of the most popular deep learning frameworks. The GPU support provided by TensorFlow enables neural network training in relatively less time.

Google Colab and Kaggle Kernels are two popular platforms that many machine learning practitioners turn to as they offer off-the-shelf GPU support besides readily available Python packages. Thus one can focus more on their machine learning workflow.

However, they are limited by the number of GPU hours and temporary/limited file storage. This can be a bottleneck in your ML workflow. Debugging your machine learning pipeline can be hard on these platforms as well.

Using your own GPU enabled machine can be really helpful especially for developing your pipeline. However, setting up your local environment to leverage the power of GPU is an involved process. One might not want to go down that path because of the complexities involved.

This report is written with the intent to make this process a bit less involved so that everyone can leverage the power of a local GPU. If you have a GPU enabled machine and use TensorFlow then this report is meant for you.

System Specifications

For clarity, I have these system specs:

Step 1: Install TensorFlow

We can either start by installing the correct drivers, CUDA, and cuDNN or by installing TensorFlow.

In your Anaconda command prompt, you can create a new Conda environment and then install TensorFlow using pip. You can also install via Conda however you might not get the latest release. You will install TensorFlow 2.3 until specified, in which case you will have to make sure that you have the correct CUDA and cuDNN version. However, the process will remain more or less the same.

# create a new conda environment
> conda create -n tf2

# install tensorflow
> pip install tensorflow

To check if you have successfully installed TensorFlow, simply open your Python console and import TensorFlow.

tf21.png

-> Figure 2: Import TensorFlow to see if you have successfully installed TensorFlow. <-

Note: The warning Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found was raised because CUDA 10.1 was not installed in my system and is required by TensorFlow to use GPU. If you have already installed the correct versions of CUDA and cuDNN and you still get this warning then you have not set the %PATH% environmental variable correctly. We will come back to this later in this report.

Step 2: Install Nvidia CUDA 10.1 ToolKit.

TensorFlow 2.1, 2.2, and 2.3 requires CUDA 10.1. CUDA 10.1 requires Nvidia GPU driver to be 418.x or higher.

toolkit1.png

-> Figure 3: Install CUDA 10.1 ToolKit with default settings. <-

You can find the complete installation guide here.

Step 3: Install cuDNN 7.6

TensorFlow requires cuDNN 7.6. Installing this is easy.

Step 4: Setup %PATH% Environment Variables

C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;%PATH%
C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\lib64;%PATH%
C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include;%PATH%
C:\> SET PATH=<installpath>\cuda\bin;%PATH%

And that's it! You are set up for some serious deep learning now.

Step 5: Check Your Installation and Monitor GPU Usage Using Weights and Biases.

Download Python script to test GPU $\rightarrow$

Now that you have successfully installed TensorFlow let's train a simple neural network and check GPU usage. Weights and Biases can automatically log important GPU metrics.

Weights and Biases will automatically log GPU metrics as shown below. :point_down:

Section 8