I'm having problems with running a deep q-learning model with Keras-RL and OpenAI Gym in Python. In particular I get an error when loading the Sequential model from the keras package.
The code is the following:
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.optimizers import Adam
from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
class DeepQL:
def __init__(self, env):
self.env = env
self.actions = env.action_space.n
self.states = env.observation_space.shape[0]
def build_model(self, states, actions):
model = Sequential()
model.add(Flatten(input_shape=(1,states)))
model.add(Dense(24, activation='relu'))
model.add(Dense(24, activation='relu'))
model.add(Dense(actions, activation='linear'))
return model
def build_agent(self):
model = self.build_model(self.states, self.actions)
policy = BoltzmannQPolicy()
memory = SequentialMemory(limit=50000, window_length=1)
dqn = DQNAgent(model=model, memory=memory, policy=policy, nb_actions=self.actions, nb_steps_warmup=10, target_model_update=1e-2)
return dqn
def compile(self):
dqn = self.build_agent()
dqn.compile(Adam(lr=1e-3), metrics=['mae'])
dqn.fit(self.env, nb_steps=5000, visualize=False, verbose=1)
The error I get is the following:
Traceback (most recent call last):
File "/Users/albi/PycharmProjects/replaybg_reinforce/py_replay_bg/reinforcementLearning/Agents/deepQL.py", line 1, in <module>
from keras.models import Sequential
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/__init__.py", line 8, in <module>
from keras import _tf_keras
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/_tf_keras/__init__.py", line 1, in <module>
from keras._tf_keras import keras
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/_tf_keras/keras/__init__.py", line 8, in <module>
from keras import activations
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/activations/__init__.py", line 8, in <module>
from keras.src.activations import deserialize
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/__init__.py", line 1, in <module>
from keras.src import activations
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/activations/__init__.py", line 3, in <module>
from keras.src.activations.activations import elu
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/activations/activations.py", line 1, in <module>
from keras.src import backend
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/backend/__init__.py", line 10, in <module>
from keras.src.backend.common.keras_tensor import KerasTensor
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/backend/common/keras_tensor.py", line 2, in <module>
from keras.src.utils import tree
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/utils/tree.py", line 12, in <module>
from tensorflow.python.trackable.data_structures import ListWrapper
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/__init__.py", line 45, in <module>
from tensorflow._api.v2 import __internal__
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/_api/v2/__internal__/__init__.py", line 8, in <module>
from tensorflow._api.v2.__internal__ import autograph
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/_api/v2/__internal__/autograph/__init__.py", line 8, in <module>
from tensorflow.python.autograph.core.ag_ctx import control_status_ctx # line: 34
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/autograph/core/ag_ctx.py", line 21, in <module>
from tensorflow.python.autograph.utils import ag_logging
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/autograph/utils/__init__.py", line 17, in <module>
from tensorflow.python.autograph.utils.context_managers import control_dependency_on_returns
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/autograph/utils/context_managers.py", line 19, in <module>
from tensorflow.python.framework import ops
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 5906, in <module>
) -> Optional[Callable[[Any], message.Message]]:
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 243, in inner
return func(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 316, in __getitem__
return self._getitem(self, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 433, in Optional
return Union[arg, type(None)]
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 243, in inner
return func(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 316, in __getitem__
return self._getitem(self, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 421, in Union
parameters = _remove_dups_flatten(parameters)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 215, in _remove_dups_flatten
all_params = set(params)
TypeError: unhashable type: 'list'
It seems the problem is related to something in the packages and not to something I wrote but I may be wrong. I have no idea how to solve this. I am running the latest versions of all the imported packages on Python 3.9