1

How come I am getting a shape ValueError for my neural network when what I am passing in is an array of shape (8,1) but the error I am getting is that the neural network is complaining about getting a (1,)?

Neural Network:

>>> observation_dimension
(8,)
>>> q_network = Sequential([
    Dense(40, input_dim=observation_dimension, activation='relu'),
    Dense(40, activation='relu'),
    Dense(number_of_actions, activation='linear')
])
>>> obs
array([-0.00371828,  0.93953934, -0.37663383, -0.07161933,  0.00431531,
        0.08531308,  0.        ,  0.        ])
>>> obs.shape
(8,)

Error:

>>> q_network.predict(obs)
Traceback (most recent call last):
...
...
ValueError: Error when checking input: expected dense_27_input to have shape (8,) but got array with shape (1,)

2 Answers 2

2

model.predict takes a batch of samples, if you give it a single sample with the wrong shape, it will interpret the first dimension as the batch one.

A simple solution is to add a dimension with a value of one:

q_network.predict(obs.reshape(1, 8))
Sign up to request clarification or add additional context in comments.

Comments

1

The predict method expects a 2d array so just reshape your obs:

obs = np.reshape(obs,(-1,len(obs)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.