How to Install TensorFlow with Cuda and cuDNN support in Windows

How to Install TensorFlow with Cuda and cuDNN support in Windows

Hey guys, In this blog we will see how we can install Tensorflow with CUDA and cuDNN. This process can be really tricky and frustrating if you are doing it for the very first time. So without any further due, let’s do it…

Step 1 — Decide versions for CUDA,cuDNN, and Visual Studio

tensorflow.org/install/source_windows#gpu

We will download the following versions.

  • CUDA 11.5
  • cuDNN 8.3
  • Microsoft Visual Studio 2019

Step 2 — Download CUDA Toolkit

Download Link — developer.nvidia.com/cuda-11-5-2-download-a..

Windows > x86_64 > 11 > exe(local) > Download

2.png

Step 3 — Download cuDNN

Download Link — developer.nvidia.com/rdp/cudnn-archive

If you have an account on Nvidia simply Login otherwise click on Join Now. We will select the latest version (8.3) for CUDA 11.5 and we will download the zip for Windows.

3.png

Step 4 — Download Visual Studio 2019 Community.

Download Link — my.visualstudio.com/Downloads?PId=8228

  • Login to Microsoft and then search Visual Studio 2019 and download the Community version.
  • It will download a setup.
  • Install that setup.
  • It will ask to download workloads, just skip it and just install Visual Studio Core Editor.

Step 5 — Extracting and merging files

  • Once you have successfully downloaded CUDA and cuDNN, install the CUDA toolkit by double-clicking on it.
  • Agree and Continue > Express (Recommended). It can restart while installation process.
  • Then extract the cuDNN zip. It will produce 4 files.
  • Copy these 4 files and paste them into C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5 and replace the files in destination.
  • Now open the bin Folder and copy the path from address bar.

4.png

Now open the start menu and type env and you will see an option “Edit the System Environment Variables”. A window like below will popup

5.png

  • Click on Environment Variables here.
  • In the user variables (in the top section) double click on Path and click New.
  • Now paste the bin path you copied in a new field and hit OK.
  • Now do the same steps for libnvvp folder also.
  • Copy the path to libnvvp folder and add a new record in path.

6.png

RESTART YOUR COMPUTER.

Step 6 — Check successful installation of CUDA

  • Run the nvidia-smi command in your terminal.
  • You can see in the top right corner, CUDA Version: 11.5

7.png

Step 7 — Create conda environment and install TensorFlow

  • Now open your terminal and create a new conda environment.
  • Use the following command and hit “y”.
  • Here gpu is the name that I given to my conda environment.

       conda create -n gpu python=3.9
    
  • Activate the conda environment and install TensorFlow (gpu version).

              conda activate gpu
             pip install tensorflow-gpu
    
  • Now simply copy the code below and paste it into a file named test.py.

           import tensorflow as tf
           tf.compat.v1.disable_eager_execution()
           with tf.device('/gpu:0'):
           a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
           b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
           c = tf.matmul(a, b)
          with tf.compat.v1.Session() as sess:
          print (sess.run(c))
    
  • Now run the python file in the conda environment.

  • If you see these results, bingo!! we have successfully installed TensorFlow with CUDA and cuDNN support :)

So this is how you will install TensorFlow with Cuda and cuDNN in Windows, thanks for reading it and I hope you are taking something with you after reading this and till the next time 👋…

Did you find this article valuable?

Support TheCoderBro by becoming a sponsor. Any amount is appreciated!