Module ‘keras.backend’ has no attribute ‘set_session’

In this tutorial, you will learn how to Module ‘keras.backend’ has no attribute ‘set_session’.

I will explain three solutions to this error and provide the best solution for permanently eliminating it.

Let’s start,

Module ‘keras.backend’ has no attribute ‘set_session’

The error message Module ‘keras.backend’ has no attribute ‘set_session’, indicates that you are trying to access the set_session() attribute from the submodule keras.backend. The attribute doesn’t exist in this module.

Below is the code that generates this error.

from keras import backend as K
Module 'keras.backend' has no attribute 'set_session'

When you try to access the attribute set_session() from the keras.backend, look at the above output; it shows the error; in your case, it might be another part of the code that can cause this error.

This error occurs because of an incompatibility between the version of Keras or TensorFlow Keras you are using and the code attempting to use the set_session() attribute or function.

Here, you are using Keras as a standalone, and I would suggest you use TensorFlow instead of Keras. In the new version of TensorFlow, Keras is integrated so that you can use all the Keras functions from the submodule tf.keras.

To fix this error, there are three solutions that you will learn one by one. But here, I will explain the solution based on TensorFlow.

The first solution is to downgrade TensorFlow. You can use the below command to downgrade to 1.14.

pip install tensorflow==1.14
!pip install tensorflow==1.14

After downgrading the tensorflow version to 1.14, you can now access the set_session() attribute from the tf.keras

READ:  How to install matplotlib python

If downgrading does not work, you can follow the next solution, which requires using the tf.compat.v1 module.

You can access all the functions or attributes from the older version of Tensorflow using this module.

import tensorflow as tf
from keras import backend as K
sess = tf.compat.v1.Session()
Second Solution to Module 'keras.backend' has no attribute 'set_session'

Look at the above output. To set the session, you must access the Session() and set_session() functions from the tf.compat.v1 module. This tf.compat.v1 allows you to use all the TensorFlow version functions or attributes in the environment of TensorFlow version 2.

This is how to fix the error Module ‘keras.backend’ has no attribute ‘set_session’ using the tf.compat.v1.

The next solution is to use the sub-module tensorflow.python.keras, you can access the set_session() from this submodule as shown below.

import tensorflow.python.keras.backend as K
import tensorflow as tf
sess = tf.compat.v1.Session()
Third Solution to Module 'keras.backend' has no attribute 'set_session'

In the above output, you have used the ‘tensorflow.python.keras.backends as K’ sub-module; you can access the set_session() function. Before using this method, remember that the submodule tensorflow.python is private to tensorflow.

After this last solution, which is to update your code according to the latest version of TensorFlow, I believe that this is the optimal solution.

This is how to resolve the error Module ‘keras.backend’ has no attribute ‘set_session’.


In this tutorial you learned how to fix the error Module ‘keras.backend’ has no attribute ‘set_session’

You learned how to downgrade the Tensorflow version to solve the error and use the tf.compat.v1 module to solve that error. Then, you learned that the best solution to this error is to adapt to the code of the latest version of TensorFlow.

READ:  Login system in Python Django

You may like to read: