【问题标题】:Keras Train Neural Network Dimension Value Error: expected to have 2 dimensions, but got array with shape (32, 1, 4)Keras 训练神经网络维度值错误:预期有 2 个维度,但得到了形状为 (32, 1, 4) 的数组
【发布时间】:2018-12-08 04:27:57
【问题描述】:
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)

【问题讨论】:

    标签: python numpy tensorflow neural-network keras


    【解决方案1】:

    '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),然后可以将其与神经网络输出进行适当的比较。

    【讨论】:

    • 我相信 tf.squeeze 是 OP 需要使用的函数,它将摆脱那个空维度。
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 2020-06-18
    • 2018-02-06
    相关资源
    最近更新 更多