module 'keras.backend' has no attribute 'tensorflow_backend'

Viewed 6417

When I run my code where I am trying to use tensor flow as backend and I do the following:

import tensorflow
KERAS_BACKEND=tensorflow
import keras
from keras import backend as K
K.tensorflow_backend._get_available_gpus()

I get the following error:

K.tensorflow_backend._get_available_gpus()
AttributeError: module 'keras.backend' has no attribute 'tensorflow_backend'

However, when I run the same piece of code on Jupyter notebook, it works and gives me the following output:

['/job:localhost/replica:0/task:0/device:GPU:0']

And this output means that tensowflow is installed. I don't get it why I get the above mentioned error when I run the same script through terminal. I am in the same environment in both cases. Insights would be appreciated.

1 Answers

From your code you are trying to understand, Is Tensorflow installed and using GPU ?

In Tensorflow 2.x, the same can be known in the following way

tf.config.list_physical_devices('GPU') 

Output:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

The following will also return the name of your GPU devices.

import tensorflow as tf
tf.test.gpu_device_name()

If a non-GPU version of the package is installed, the function would also return False. Use tf.test.is_built_with_cuda to validate if TensorFlow was build with CUDA support.

Note: From TF 2.0, onwards Keras are integrated with Tensorflow as tf.keras, please use this module tf.keras.backend

Related