'keras AssertionError: Duplicate registrations for type 'experimentalOptimizer'

I'm trying to build a Deep Q Learning code for CartPole-v1 game. However I encounter an AssertionError:

AssertionError: Duplicate registrations for type 'experimentalOptimizer'

Some of the stacktrace is shown below:

the error

I upgraded TensorFlow but it didn't work. What should I do to fix it?

System Details

  • Windows OS
  • Spyder IDE
  • Anaconda 3 (base environment)


Solution 1:[1]

Same problem : I completely uninstalled the libraries (nightly...), and reinstalled the latest stable version (pip3 install tensorflow) I'm on linux, don't have your configuration, don't know which libraries are installed. Anyway I hope this helps.

So : uninstall keras, tensorboard, tensorflow, tensorflow-gpu if it's installed, idem for tb-nightly, keras-nightly, tf-nightly

Re-install without specifications tensorflow, it will take care of needed lib.

Solution 2:[2]

Sorry this does not help. I tried

pip3 uninstall tensorflow absl-py astunparse flatbuffers gast google-pasta grpcio h5py keras keras-preprocessing libclang numpy opt-einsum protobuf setuptools six tensorboard tensorflow-io-gcs-filesystem termcolor tf-estimator-nightly typing-extensions wrapt

then

pip3 install --no-cache-dir tensorflow

But the error is still there. I guess after uninstall I need to remove some files manually...

Update: got it. Had to remove .local/lib/python3.9/site-packages/keras as sudo. Then reinstall helped.

Solution 3:[3]

  1. uninstall:
    • keras
    • keras-nightly
    • keras-preprocessing
    • tensorboard
    • tensorflow
    • tb-nightly
    • tf-nightly
  2. pip3 install tensorflow
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Mike