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

Question:

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'
Asked By: dan

||

Answers:

after tf2, you should use

from tensorflow import keras
Answered By: Xiaomin Wu
!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
Answered By: AKSHAY KUMAR RAY

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

Answered By: MHP

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

Answered By: xuhdev

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.

Answered By: Hagbard

from keras.models import Model

ERROR:

AttributeError: module ‘tensorflow.compat.v2.internal‘ has no attribute ‘register_clear_session_function’

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.