检查模型目标时发生错误:预期density_2具有形状(None,29430),但数组的形状为(1108,1) [英] Error when checking model target: expected dense_2 to have shape (None, 29430) but got array with shape (1108, 1)

查看:67
本文介绍了检查模型目标时发生错误:预期density_2具有形状(None,29430),但数组的形状为(1108,1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Keras有点陌生,我正在尝试创建尺寸合适的模型.我的训练数据成形为len(x_train) = 1108和len(x_train)[0] = 29430,但是我似乎制作的形状不正确. (标题中的确切错误消息在标有星号***的位置.)

I'm a bit new to Keras and I'm trying to create a model with the right dimensions. My training data is shaped such that len(x_train) = 1108 and len(x_train)[0] = 29430, but I seem to be making the shape incorrectly. (The exact error message in the title is at the place labeled with stars ***.)

我运行了一个模型摘要,因此形状应如下所示:

I ran a model summary, so the shapes should be like this:

Layer (type) 
Output Shape          
Param #     
Connected to                     
____________________________________________________________________________________________________
input_1 (InputLayer)             
(None, 29430)         
0    
[nothing]                                       
____________________________________________________________________________________________________
dense_1 (Dense)                  
(None, 64)            
1883584     
input_1[0][0]                    
____________________________________________________________________________________________________
dense_2 (Dense)                  
(None, 29430)         
1912950     
dense_1[0][0]                    
____________________________________________________________________________________________________

inputs = Input(shape=(29430, ))
h = Dense(64, activation='sigmoid')(inputs)
outputs = Dense(29430)(h)

model = Model(input=inputs, output=outputs)

model.summary()

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, # ***
    batch_size=batch,
    #epochs=epochs,
    validation_data=(x_test, y_test),
    callbacks=[TestCallback((x_test, y_test))])

h.trainable = False

outputs = Dense(1)(h)
outputs = Activation('sigmoid')

model2 = Model(input=inputs, output=outputs)

model2.fit(x_train, y_train,
batch_size=batch,
epochs=epochs,
validation_data=(x_test, y_test))

推荐答案

问题似乎出在您提供的用于训练模型的标签中.它们是形状(None, 1),但是模型的输出是(None, 29430),因此标签应该具有相同的输出.

It looks like the problem is in the labels you provide to train the model. They are shape (None, 1) but the output of your model is (None, 29430) so the labels should have the same output.

这篇关于检查模型目标时发生错误:预期density_2具有形状(None,29430),但数组的形状为(1108,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆