Skip to content

Instantly share code, notes, and snippets.

@anirban94chakraborty
Last active February 12, 2024 16:38
Show Gist options
  • Save anirban94chakraborty/0364e37ec1ddd57fe935fcf1e7fabd36 to your computer and use it in GitHub Desktop.
Save anirban94chakraborty/0364e37ec1ddd57fe935fcf1e7fabd36 to your computer and use it in GitHub Desktop.
Install Tensorflow-GPU (for NVIDIA GPUs) for use in JupyterLab using Anaconda

Install Tensorflow-GPU (for NVIDIA GPUs) for use in JupyterLab using Anaconda

This tutorial is for computers with NVIDIA GPUs installed.

Tensorflow for GPU significantly reduces the time taken by Deep Neural Networks (like CNNs, LSTMs, etc) to complete each Epoch (compute cycle) by utilizing the CUDA cores present in the GPU for parallel processing.

The following steps are to be followed:

  1. Make sure that you have installed the latest drivers of your NVIDIA GPU for your OS.

  2. Install Anaconda Distribution (Individual Edition) suitable for your OS.

    Download URL: https://www.anaconda.com/products/individual

  3. Open the Terminal of your OS in Superuser Mode.

    For Example:

    • Windows Users have to open Command Prompt as Administrator.

    • Linux / macOS users have to open Terminal and log in as root user.

  4. Check Compatible Versions of Python, cuDNN and CUDA for TensorFlow-GPU.

    URL:

    For Windows: https://www.tensorflow.org/install/source_windows#gpu

    For Linux / macOS: https://www.tensorflow.org/install/source#gpu

    It is advised that you use the latest version of Tensorflow and its compatible Python, cuDNN and CUDA versions (usually displayed at the first row of the list)

    In this example, we will use TensorFlow version 2.4.1 [Latest as on 06 April 2021] for which the Python Version to be used is 3.6 to 3.8 (3.8 recommended), cuDNN version to be used is 8.0, and CUDA Toolkit version to be used is 11.0

  5. Create a Virtual Environment in Anaconda using the following command:

    conda create -n tf_gpu python==3.8

    Here, tf_gpu is the name of the virtual environment in which the Python 3.8 is to be installed.

  6. Activate the Virtual Environment using the following command:

    conda activate tf_gpu
  7. Install cuDNN and CUDA Toolkit from Conda-Forge Repo Source using the following command:

    conda install cudatoolkit=11.0 cudnn=8.0 -c=conda-forge
  8. Install TensorFlow for GPU using the following command:

    pip install --upgrade tensorflow-gpu==2.4.1

    At this point, the TensorFlow library is ready to be used. You can check if your GPU is detected by TensorFlow using the following python code in a python shell of the Virtual Environment:

    >>> import tensorflow as tf
    >>> tf.test.is_gpu_available()
  9. Install iPython Kernel for using in JupyterLab using the following command:

    pip install ipykernel

    Don't conda install ipykernel as it may downgrade your TensorFlow installation.

  10. Register the iPython Kernel for selection within JupyterLab using the following command:

    python -m ipykernel install --user --name tf_gpu --display-name "TensorFlowGPU"
  11. Install JupyterLab using the following command:

    conda install jupyterlab
  12. Also install Keras library to prevent the occurrence of any runtime errors, by using the following command:

    pip install keras

    Don't conda install keras as it may downgrade your TensorFlow installation.

  13. That's it !! DONE !! You can now run code inside iPython Notebooks in JupyterLab that uses TensorFlow Library functions. Also, make sure that the the newly created Kernel is selected as the execution kernel for the iPython Notebook.


Sample Test Code & iPython Notebook files (for running an LSTM) that uses TensorFlow for GPU can be downloaded from here:

https://ea4269d4-d326-4ad3-8762-3cd36a7b1e50.filesusr.com/archives/d9662a_32aa1694580b4fd698345293e04104b2.rar?dn=tensorflow-gpu-tutorial.rar

Copy link

ghost commented Jul 31, 2022

Dear Sir, Although I had followed your instruction and I can choose both iPython3(ipkernal) and TensorFlowGPU..but when I make new file by TensorFlowGPU in jupyter notebook, run import tensorflow as tf , it still showed "No module named 'tensorflow',could you please share some suggestion? TT

@anirban94chakraborty
Copy link
Author

anirban94chakraborty commented Aug 1, 2022

Did you select the appropriate kernel for the Notebook file? @233LogicChen233

Copy link

ghost commented Aug 1, 2022

oh!!thanks sir, i have solved the problem!

@anirban94chakraborty
Copy link
Author

@233LogicChen233 You're most welcome. I am glad to know that this gist helped you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment