I am trying to make a custom Gym Environment so that I can use it in a Keras Network. But there is a problem that is happening to me when I try to fit de neural network.
ValueError: Error when checking input: expected dense_6_input to have 2 dimensions, but got array with shape (1, 1, 15)
What I understand about this problem is that the states (the inputs that the network receives) are structured as a 3 dimensional array, but I don´t know why.
Here is my init method in the class that defines the environment:
def __init__ (self):
self.action_space = Discrete (4)
self.observation_space = Box(low=0, high=100, shape=(15,))
self.state = np.array([1,2,0,3,2,0,4,0,0,1,3,0,0,0,0], dtype=float)
#self.state = state
self.length = 15
self.index = 0
After, i initialize two variables that save the shape of the states and the actions, so we can define the model.
states = env.observation_space.shape
actions = env.action_space.n
def build_model(states, actions):
model = Sequential()
model.add(Dense(24, activation='relu', input_shape=states))
model.add(Dense(24, activation='relu'))
model.add(Dense(actions, activation='linear'))
return model
The summary of the model:
Layer (type) Output Shape Param #
dense_6 (Dense) (None, 24) 384
dense_7 (Dense) (None, 24) 600
dense_8 (Dense) (None, 4) 100
The last step before the error is when i buid the agent. After that, we call the fit method and occurs the problem.
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)
I tried to change the input_shape of the first layer to be (1, 1, 15) but doesn´t seems to work. Maybe the problem is related with the definition of the environment (observation space) or how the environments provides the information to the network. I don´t know...
I hope you can help me. let me know if you need some more information to handle the error.
Thank you so much!
The input shape should be (1,15) in your case. This is because the actual input shape is increased by an additional dimension to what you specify in shape
as keras processes inputs in batches, with the batch size being the first parameter.
The error message is telling you that inputs of shape (1,1,15) are being passed to the model ( i.e. batch of 1, and input shape (1,15), so ndims=3), but you only have ndims=2. Despite only passing (15,) as the input shape, notice that ndims = 2. This is because Keras is adding an additional dimension for the batch.
So to fix, set shape=(1,15)
which will then be processed as (1,1,15)
which is what Keras expects.