简体   繁体   English

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

[英]Keras Train Neural Network Dimension Value Error: expected to have 2 dimensions, but got array with shape (32, 1, 4)

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: 当我将值传递给神经网络时,将返回形状(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)

I am passing in training_set and labels 我正在传递training_setlabels

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

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) . 'mean_squared_error'损失函数可能期望接收(batch_sz x n_labels)标签矩阵,但是您要传递(batch_sz x 1 x n_labels)标签矩阵,尤其是使用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. 您只需要调整labels的形状以使其具有形状(batch_sz x n_labels) ,使其具有labels.shape=(32, 4) ,然后可以将其与神经网络输出进行适当比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查输入时的Keras值错误:预期density_27_input具有5个维度,但数组的形状为(32,150,150,3) - Keras Value Error when checking input: expected dense_27_input to have 5 dimensions, but got array with shape (32, 150, 150, 3) Keras fit_generator:检查目标时出错:预期activation_32具有4个维,但数组的形状为(4,4) - Keras fit_generator : Error when checking target: expected activation_32 to have 4 dimensions, but got array with shape (4, 4) Keras(FIT_GENERATOR)- 检查目标时出错:预期 activation_1 具有 3 个维度,但得到的数组形状为 (32, 416, 608, 3) - Keras(FIT_GENERATOR)- Error, when checking target: expected activation_1 to have 3 dimensions, but got array with shape (32, 416, 608, 3) input_shape 的错误预期有 4 个维度,但得到了形状为 (73257, 32, 32) 的数组 - Error with the input_shape expected to have 4 dimensions, but got array with shape (73257, 32, 32) 检查输入时出错:预期 conv2d_4_input 有 4 个维度,但得到了形状为 (32, 32) 的数组 - Error when checking input: expected conv2d_4_input to have 4 dimensions, but got array with shape (32, 32) ValueError:检查输入时出错:预期 input_1 有 5 个维度,但得到了形状为 (10000, 32, 3, 32) 的数组 - ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (10000, 32, 3, 32) 预期输入有 4 个维度,但得到了形状为 (32, 549, 1) 的数组 - expected inputs to have 4 dimensions, but got array with shape (32, 549, 1) Keras错误:预期input_1具有3个维度,但数组的形状为(256326,3) - Keras error: expected input_1 to have 3 dimensions, but got array with shape (256326, 3) Keras ValueError:检查目标时出错:预期density_15具有3维,但数组的形状为(301390,8) - Keras ValueError: Error when checking target: expected dense_15 to have 3 dimensions, but got array with shape (301390, 8) ValueError:检查目标时出错:预期density_2具有4维,但数组的形状为(64,50)(Keras) - ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (64, 50) (Keras)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM