65

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?

talonmies
  • 70,661
  • 34
  • 192
  • 269
matchifang
  • 5,190
  • 12
  • 47
  • 76
  • @SalvadorDali I have tried the answers to that question, but it does not print out anything. Also, it does not answer my question: 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? – matchifang Jul 13 '17 at 07:44
  • *but it does not print out anything* how is it possible? Have you tried my answer there. It is either printing something or failing and the answer explain what each of these steps mean. Basically 2 of the questions you just asked here in the comments are answered there – Salvador Dali Jul 13 '17 at 07:50
  • @SalvadorDali, Apologies. I tried your code, it works and it shows that it's running on CPU. However, how can I check if the tensorflow I have is GPU version or CPU version? – matchifang Jul 13 '17 at 11:43

2 Answers2

77

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)

Bumseok
  • 971
  • 8
  • 6
  • 1
    Running from a command line session on Ubuntu 16.04 this works, but then it doesn't prompt for other commands, same with directly tensorflow, and then O'd have to restart the terminal. Any idea what might be the cause? – Kostas Mouratidis Jul 11 '18 at 08:25
  • 3
    What should be the output of above command – Nitin Jun 28 '19 at 14:13
  • @Nitin It should give you a list of available GPUs on your machine. For instance on mine I get: `['/job:localhost/replica:0/task:0/device:GPU:0']` – rayryeng Jul 31 '19 at 07:02
  • 14
    does not work in TF 2.0 – Shital Shah Mar 18 '20 at 04:01
  • ```AttributeError Traceback (most recent call last) in 1 from keras import backend as K ----> 2 K.tensorflow_backend._get_available_gpus() ...\tensorflow_backend.py in _get_available_gpus() 504 _LOCAL_DEVICES = [x.name for x in devices] 505 else: --> 506 _LOCAL_DEVICES = tf.config.experimental_list_devices() 507 return [x for x in _LOCAL_DEVICES if 'device:gpu' in x.lower()] 508 AttributeError: module 'tensorflow_core._api.v2.config' has no attribute 'experimental_list_devices'``` – Constructor Jul 25 '20 at 02:22
  • 4
    for **TF 2.0**: `from tensorflow.python.keras import backend as K` @ShitalShah – Elior B.Y. Aug 20 '21 at 01:10
  • 2
    Adding to the above comment, for TF 2.0, `from tensorflow.python.keras import backend as K` followed by `K._get_available_gpus()`. – Anirudh Ajith Sep 23 '21 at 04:31
  • I get this error: AttributeError: module 'keras.backend' has no attribute 'tensorflow_backend' – skan Jan 28 '23 at 17:41
30

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

markroxor
  • 5,928
  • 2
  • 34
  • 43
  • 15
    i have tensorflow gpu installed, but keras doesn't pick it, what to do? – kRazzy R Feb 15 '18 at 01:25
  • 1
    Did you follow the official guide - https://keras.io/backend/ ? – markroxor Aug 02 '18 at 16:59
  • If you have tensorflow-gpu installed but Keras isn't picking it up, then it's likely that the CUDA libraries aren't being found. You need the CUDA lib paths and bin path (for ptxas) to use GPU with Keras/TF effectively. – Gearoid Murphy Jun 25 '20 at 22:08