How to enable gpu in tensorflow 2

Viewed 2235

From my searching result, seems tensorflow 2 automatically will use available gpu. I have a rtx 2080, but when i run the code print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) it gives Num GPUs Available: 0. My tensorflow version is 2.4.x. And I am using jupyter notebook. What should I do to enable the gpu?

3 Answers

Could you please install TensorFlow using the command pip install tf-nightly-gpu and let us know if it works as it worked for me.

try below code snippet, it will return the flag True, if there are available GPUs

tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)

Setting up Tensorflow-GPU in Windows

  1. Download and install Microsoft Visual Studio 2015 with update 3

"Search on Google using the same name and download the ISO image file and mount it. Install MSVS with visualc++ and python under programming language section. Rest is default.

  1. Download and install CUDA 10.0 from nvidia site.

  2. Download CuDNN by making free account. CUDNN 7.5.1 is compatible with CUDA 10.0

  3. Extract the files and paste the files from folders in CUDNN to CUDA in the same folder.

  4. set path of "bin" and "libnvvp" under system variable from CUDA folder in NVIDIA in program files in C.

  5. using pip in cmd download tensorflow-gpu version 1.15.0

    pip install tensorflow-gpu==1.15.0

  6. Install all the other dependencies. numpy sklearn scipy pandas imutils matplotlib etc.

  7. Download Keras using same command.

Test tensorflow installation using following code:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

import tensorflow as tf

if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

Note:- Version Compatibility may be arises but not sure.

Related