Error while import keras: AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'register_clear_session_function'

Viewed 14331

I want to import keras after I did pip install keras, but it shows message as shown below. I even can't call any function from keras library. Can anyone know about this?

import keras

Error:

AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'register_clear_session_function'
5 Answers

you can should use import tensorflow.keras instead of import keras. More info here

!pip install -U -q segmentation-models
!pip install -q tensorflow==2.1
!pip install -q keras==2.3.1
!pip install -q tensorflow-estimator==2.1.

## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ["SM_FRAMEWORK"] = "tf.keras"

from tensorflow import keras
import segmentation_models as sm

This can be caused by a version mismatch between Keras installation and Tensorflow. Make sure their versions match.

None of the other answers here worked for me. I ended up updating keras and tensorflow as follows:

pip install --upgrade keras
pip install --upgrade tensorflow

Now, it works for me.

after tf2, you should use

from tensorflow import keras
Related