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

TensorflowKeras

Tensorflow Problem Overview


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?

Tensorflow Solutions


Solution 1 - Tensorflow

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)

Solution 2 - Tensorflow

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmatchifangView Question on Stackoverflow
Solution 1 - TensorflowBumseokView Answer on Stackoverflow
Solution 2 - TensorflowmarkroxorView Answer on Stackoverflow