I am trying to build a reinforcement learning agent to learn off a custom environment, built to openai's gym specifications.
I have np arrays of size (20, 7) which I want to pass to the network, and output one of 7 actions.
I am having trouble building the actual network, as I want to include LSTM layers. My code is as follows:
def build_model():
model = Sequential()
model.add(LSTM(60, return_sequences = True, input_shape=(20, 7), activation = 'relu'))
model.add(Dense(21, activation = "relu"))
model.add(Flatten())
model.add(Dense(7, activation="linear"))
model.compile(loss="mse", optimizer=Adam(lr=0.0002), metrics=['accuracy'])
return model
However, when I build the agent, there is suddenly an extra dimension added on which the network does not expect:
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'])
dqn.fit(env, nb_steps=50000, visualize=False, verbose=1)
ValueError: Error when checking input: expected lstm_input to have 3 dimensions, but got array with shape (1, 1, 20, 7)
Im not exactly sure why the agent is reshaping the data to add an extra dimension, (or two?) but if anyone had an idea on how to stop this from happening so I can train my network I would be very grateful. My solution runs when I code it myself however I want to make use of the keras rl-2 library.
Thanks in advance!