TL;DR: Ensure the Keras version matches the Tensorflow version
I am experiencing the same thing with:
- Windows
- Python3.8
- Tensorflow-2.6.1
The core issue appears to be that there are two Keras packages installed:
<site-packages>/keras
<site-packages/tensorflow/python/keras
If you look at the release notes for 2.6:
https://github.com/tensorflow/tensorflow/releases/tag/v2.6.0
Keras been split into a separate PIP package (keras), and its code has
been moved to the GitHub repositorykeras-team/keras. The API endpoints
for tf.keras stay unchanged, but are now backed by the keras PIP
package. The existing code in tensorflow/python/keras is a staled copy
and will be removed in future release (2.7). Please remove any imports
to tensorflow.python.keras and replace them with public tf.keras API
instead.
For some reason, it is still importing from both packages which is triggering the valid exception (only one Keras instance should be imported)
Digging a bit further, it looks like Keras-2.7 was being installed, reverting to Keras-2.6 resolved the issue:
pip install keras==2.6.*
For some reason:
https://github.com/tensorflow/tensorflow/blob/v2.6.1/tensorflow/tools/pip_package/setup.py#L106
Is not working, maybe a bug in PIP?