ImportError: cannot import name 'cast' from partially initialized module 'keras.src.backend'

1.5k views Asked by At

I have imported the following libraries for my machine learning project but have problem when I try to run my model in the terminal:

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import layers, models

and an error message appears:

ImportError: cannot import name 'cast' from partially initialized module 'keras.src.backend' (most likely due to a circular import) (C:\Users\DELL\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\backend\__init__.py)

Hope someone can help me resolve this error?

1

There are 1 answers

0
Jon On

Uninstalling and installing keras fixed it for me. I uninstalled keras with:

pip uninstall keras

Then I installed a keras version compatible with my tensorflow version - version compatibility can be found here. In my case, I had tensorflow version 2.14.0, so I installed keras version 2.14.0 with:

pip install keras==2.14.0