I'm trying to train an agent using TensorFlow and Keras-rl2 to be able to play a gym environment called CartPole-v1 and I'm using google colaboratory this's my implementation:
!pip install gym[classic_control]
!pip install keras-rl2
import tensorflow as tf
from tensorflow import keras as k
import numpy as np
import gym
import random
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
env = gym.make('CartPole-v1')
states = env.observation_space.shape[0]
actions = env.action_space.n #actions = 2
def build_model(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
model = build_model(states, actions)
model.summary()
from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
def build_agent(model, actions):
policy = BoltzmannQPolicy()
memory = SequentialMemory(limit=50000, window_length=1)
dqn = DQNAgent(model=model, memory=memory, policy=policy,
nb_actions=actions, nb_steps_warmup=10, target_model_update=1e-2)
return dqn
dqn = build_agent(model, actions)
dqn.compile(Adam(lr=1e-3), metrics=['mae']) # this's the line that is problematic
dqn.fit(env, nb_steps=50000, visualize=False, verbose=1)
I get this Error when I try to compile my DQN agent:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-08c9e140f6bc> in <module>
1 dqn = build_agent(model, actions)
----> 2 dqn.compile(Adam(lr=1e-3), metrics=['mae'])
3 dqn.fit(env, nb_steps=50000, visualize=False, verbose=1)
2 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training_v1.py in get_weights(self)
154 """
155 strategy = (self._distribution_strategy or
--> 156 self._compile_time_distribution_strategy)
157 if strategy:
158 with strategy.scope():
AttributeError: 'Sequential' object has no attribute '_compile_time_distribution_strategy'
I tried searching for a solution on the internet but I couldn't find any I did find someone with a similar problem but he was building the model before importing the dependencies but this wasn't the problem in my case