Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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__'
like image 818
scopchanov Avatar asked Nov 08 '25 00:11

scopchanov


2 Answers

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
like image 90
Xie He Avatar answered Nov 09 '25 12:11

Xie He


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

like image 35
Vamsi Krishna Avatar answered Nov 09 '25 13:11

Vamsi Krishna