[tensorflow] How to check if keras tensorflow backend is GPU or CPU version?

I understand that when installing tensorflow, you either install the GPU or CPU version. How can I check which one is installed (I use linux).

If the GPU version is installed, would it be automatically running on CPU if GPU is unavailable or would it throw an error? And if GPU is available, is there a specific field or value you need to set to make sure it's running on GPU?

This question is related to tensorflow keras

The answer is


According to the documentation.

If you are running on the TensorFlow or CNTK backends, your code will automatically run on GPU if any available GPU is detected.

You can check what all devices are used by tensorflow by -

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

Also as suggested in this answer

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

This will print whether your tensorflow is using a CPU or a GPU backend. If you are running this command in jupyter notebook, check out the console from where you have launched the notebook.

If you are sceptic whether you have installed the tensorflow gpu version or not. You can install the gpu version via pip.

pip install tensorflow-gpu


Also you can check using Keras backend function:

from keras import backend as K
K.tensorflow_backend._get_available_gpus()

I test this on Keras (2.1.1)