How to fix "cannot import name '__version__' from 'tensorflow.keras'"?

3.8k Views Asked by At

Trying to import DQNAgent like this

from rl.agents.dqn import DQNAgent

I get the following error:

cannot import name '__version__' from 'tensorflow.keras'

The installed versions are:

Tensorflow: 2.13.0, Keras: 2.13.1, Keras-rl2: 1.0.5

I am using Anaconda and Jupyter Notebook.

How can I fix this?

EDIT

If that helps to resolve the issue, this import

import tensorflow
from tensorflow import keras

print(keras.__version__)

causes

module 'tensorflow.keras' has no attribute '__version__'
5

There are 5 best solutions below

1
Vamsi Krishna On

In rl/callbacks.py change from tensorflow.keras to from keras import __version__. that should do the trick.

0
Xie He On

Find one solution without modifying the source code. cite from:https://github.com/tensorflow/tensorflow/issues/50372

from keras import __version__
tf.keras.__version__ = __version__

and then:

from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
0
Shirvan On

In ".../site-packages/rl/callbacks.py" change

from tensorflow.keras import __version__ as KERAS_VERSION

to

from keras import __version__ as KERAS_VERSION

0
Visionscaper On

I had a similar issue. I wanted to import:

from tensorflow.python.keras.saving import hdf5_format

The subsequent import error was:

cannot import name '__version__' from 'tensorflow.python.keras'

What worked for me was:

import tensorflow.python.keras as tf_keras
from keras import __version__
tf_keras.__version__ = __version__
0
Veer On

I was working on google colab and faced similar problem. I added below lines of code to fix it.

import tensorflow.keras as tf
from keras import __version__
tf.keras.__version__ = __version__

and then the code block where I was getting error

from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory

fixed my error. Thanks for all the answers above as well.