Reputation: 16469
Python 3.6
Keras 2.2
Tensorflow 1.8 backend
I am having trouble training my neural network because I am getting this error:
ValueError: Error when checking target: expected t_dense_3 to have 2 dimensions, but got array with shape (32, 1, 4)
My neural network
>>> sgd = optimizers.SGD(lr=0.01, decay=1e-6)
>>> target_q_network = Sequential([
Dense(40, input_shape=observation_shape, activation='relu', name='t_dense_1'),
Dense(40, activation='relu', name='t_dense_2'),
Dense(number_of_actions, activation='linear', name='t_dense_3')
])
>>> target_q_network.compile(loss='mean_squared_error', optimizer=sgd)
>>> observation_shape
(8,)
-----------------------------------------------------------------
(Pdb) target_q_network.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
t_dense_1 (Dense) (None, 40) 360
_________________________________________________________________
t_dense_2 (Dense) (None, 40) 1640
_________________________________________________________________
t_dense_3 (Dense) (None, 4) 164
=================================================================
Total params: 2,164
Trainable params: 2,164
Non-trainable params: 0
_________________________________________________________________
When I pass in values to the neural network an array of shape (1, 4) is returned:
(Pdb) env.reset()
array([-0.00126171, 0.94592496, -0.12780861, 0.35410735, 0.00146875, 0.02895054, 0. , 0. ])
# Passing value into Neural Network
(Pdb) target_q_network.predict(env.reset().reshape(1,8))
array([[ 0.07440183, 0.03480911, 0.11266299, -0.08043154]], dtype=float32)
I am passing in training_set
and labels
(Pdb) training_set.shape
(32, 8)
(Pdb) labels.shape
(32, 1, 4)
Upvotes: 1
Views: 120
Reputation: 353
The'mean_squared_error'
loss function is probably expecting to receive a (batch_sz x n_labels)
matrix of labels, but you are passing a (batch_sz x 1 x n_labels)
matrix of labels, specifically with labels.shape=(32, 1, 4)
. You just need to reshape your labels
to have the shape (batch_sz x n_labels)
so it has labels.shape=(32, 4)
, which can then be properly compared to the neural network output.
Upvotes: 2