How to Install TensorFlow and Keras with GPU support on Windows.

Spread the love

In this post, you will learn how to install TensorFlow and Keras with GPU support on windows. We will go through step by step to download and install required software like CUDA Toolkit, cuDNN, GPU Drivers, etc. so that you can utilize your GPU for training neural networks faster.

Let’s get started.

1. Install Nvidia GPU Drivers –

First, you need to install Nvidia GPU drivers. Go to this Nvidia downloads page here – https://www.nvidia.com/download/index.aspx? and then choose the GPU that is installed on your machine and the version of the windows operating system.

Then click on search.

Then click on download.

You can also download the drivers directly from the GEFORCE EXPERIENCE APP on your machine.

Then double click the .exe file and install.

Then Agree and continue. Accept all the default settings Express (Recommended) and click NEXT to install.

2 Install Microsoft Visual Studio –

You also have to install C++ libraries from Microsoft visual studio as CUDA is based on C++ and Windows does not come with C++ by default as Linux and Mac do. So go to this link – https://visualstudio.microsoft.com/ and download the latest community 2022 edition.

Double click the .exe file and start the installation. Here, you need to select the components that you want. Select the Desktop and Mobile development with C++ and click Install. It’s a big file so make sure that you are on Wi-Fi instead of the cellular network.

3. Install CUDA Toolkit –

Next, you need to install the CUDA Toolkit. CUDA is a parallel computing platform and programming model invented by NVIDIA. It enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU). And the CUDA Toolkit installs the CUDA driver and tools needed to create, build and run a CUDA application as well as libraries, header files, and other resources.

3.1. First, make sure that you have Cuda enabled GPU by going into this link – https://developer.nvidia.com/cuda-gpus

You can find the GPU that is installed on your machine inside Device Manager in the control panel or just search for it in the windows search bar.

3.2. Next go to this linkhttps://developer.nvidia.com/cuda-downloads and download the CUDA Toolkit 11.2 or later version of it.

select the options based on your machine and download the exe(local) file.

Then run the .exe file and follow the instructions to install it.

3. 3. Install Zlib –

Go to this link – https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#install-zlib-windows

And download the file and extract it. Then copy the zlibwapi.dll file inside the dll_x64 folder and paste it in the bin folder inside the CUDA Toolkit folder. You can find the CUDA toolkit path in the C Drive. On my machine, the path looks like this – C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin

4. Install cuDNN –

Next, we need to install cuDNN. 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.

Go to the following link and download cuDNN, you will be asked to create an account first. It’s completely free, so need to worry about it. –

https://developer.nvidia.com/cudnn

Download the exe file and then double click to install it.

Set Environment Variables –

Next, we need to set up Environment Variable.

Go to control panel > System and Security > System > Advanced System Settings. Then click on environment variables.

Now, we need to add 4 paths to the system variables. First, go to the C drive where Nvidia Cuda Toolkit is installed. In my system it’s inside – C:\Program Files\NVIDIA GPU Computing Toolkit. And the cuDNN is installed inside – C:\Program Files\NVIDIA\CUDNN . Please check yours, it probably will be the same but still, make sure.

Select path inside System variable and click on edit.

Then click on new and add the following paths one by one. Some of them are already added when you installed them. So, add the remaining that is left.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\extras\CUPTI\lib64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include
C:\Program Files\NVIDIA\CUDNN\v8.3\bin

Once all added. click ok and exit all dialog boxes.

5 . Install Miniconda / Anaconda (optional)-

You might already have Miniconda or Anaconda installed on your machine. But I still put it here in case if you don’t. Miniconda is the lighter version of Anaconda.

Go to the Miniconda website – https://docs.conda.io/en/latest/miniconda.html

Then download the latest Python 3.9 64-bit windows version.

In the Advanced options, select both the options then install. By default, only the second option is selected and the first option is not recommended. But if you will not select it then you won’t able to run python commands from PowerShell or Git Bash, it will not recognize it and you have to always run commands from Anaconda prompt. So, select both options.

Next, install a jupyter notebook. Run the following command in PowerShell.

pip install notebook

And to run jupyter notebook, type

jupyter notebook

6. Install TensorFlow and Keras

Finally, we will install TensorFlow. Keras gets installed automatically when you install TensorFlow so there is no need to install it separately. Keras is a high-level deep learning API to build and train all kinds of neural networks which uses TensorFlow as a backend to perform the heavy computations required by neural networks. There are two ways to install TensorFlow. Either you install it in your global environment or in a virtual environment. When you install a new library, it is always advisable to install it in a virtual environment. But I like to install TensorFlow in my global environment as I use it very frequently and I don’t want to activate and deactivate the environment every time I want to use it. It’s a matter of personal choice, so I will show both ways.

Install in the Global Environment –

pip install tensorflow

You can also install it using conda but conda does not give you the latest version of TensorFlow. So, it is better to use pip.

Install in the Virtual Environment –

First, create the virtual environment.

conda create --name tf_venv python=3.9

Type y when asked to proceed. tf_venv is the name of the environment and we are telling conda to use python version 3.9.

Next, you need to activate the virtual environment.

conda activate tf_venv

If after running the command environment is not get activated then it means that anaconda is not configured in the command prompt. So, run

conda init

to initialize conda. Close the command prompt or PowerShell and re-open it and then activate the virtual environment.

Once the environment gets activated, you will see the name of it (tf_venv) in front of the windows path.

Next, install TensorFlow as usual.

pip install tensorflow

You also have to install a jupyter notebook in this new environment.

pip install notebook

Next, you need to activate the virtual environment for the jupyter notebook so that the jupyter notebook can find this new environment.

python -m ipykernel install --user --name=tf_venv

to launch jupyter notebook, type

jupyter notebook

Now, when you create a new jupyter notebook, you will see the name of the virtual environment. Using this kernel will use the tf_venv libraries and settings that are specific for this environment.

To deactivate the virtual environment when you are done working, you can use the command.

conda deactivate

7. Test the Installation –

Now, let’s test the installation to make sure that everything is running properly and TensorFlow can find the GPU that is installed in your machine.

First import TensorFlow.

import tensorflow as tf

The run the following command.

print(f"Tensorflow version: {tf.__version__}")
print(f"Keras Version: {tf.keras.__version__}")
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE")

TensorFlow can successfully find the GPU. Now, you can use this GPU to train the Deep Learning model faster.

We learned a lot of things in this post. If you like this post then please share it with others and also subscribe to our blog below.

Rating: 1 out of 5.

Leave a Reply