Keras Train神经网络维度值错误:预期具有2个维度,但数组的形状为(32,1,4)

时间:2018-06-29 03:35:13

标签: python numpy tensorflow neural-network keras

Python 3.6
Keras 2.2
Tensorflow 1.8 backend

由于遇到此错误,我无法训练我的神经网络:

ValueError: Error when checking target: expected t_dense_3 to have 2 dimensions, but got array with shape (32, 1, 4)

我的神经网络

>>> 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
_________________________________________________________________

当我将值传递到神经网络时,将返回形状(1、4)的数组:

(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)

我正在传递training_setlabels

(Pdb) training_set.shape
(32, 8)
(Pdb) labels.shape
(32, 1, 4)

1 个答案:

答案 0 :(得分:2)

'mean_squared_error'损失函数可能期望接收(batch_sz x n_labels)标签矩阵,但是您正在传递(batch_sz x 1 x n_labels)标签矩阵,尤其是labels.shape=(32, 1, 4)。您只需要重塑labels使其具有形状(batch_sz x n_labels),使其具有labels.shape=(32, 4)的形状,然后可以将其适当地与神经网络输出进行比较。