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:
-
Make sure that you have installed the latest drivers of your NVIDIA GPU for your OS.
-
Install Anaconda Distribution (Individual Edition) suitable for your OS.
Download URL: https://www.anaconda.com/products/individual
-
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.
-
-
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
-
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. -
Activate the Virtual Environment using the following command:
conda activate tf_gpu
-
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
-
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()
-
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. -
Register the iPython Kernel for selection within JupyterLab using the following command:
python -m ipykernel install --user --name tf_gpu --display-name "TensorFlowGPU"
-
Install JupyterLab using the following command:
conda install jupyterlab
-
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. -
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:
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