Error when checking model target: expected dense_2 to have shape (None, 29430) but got array with shape (1108, 1)

时间:2017-07-10 15:21:48

标签: python neural-network keras

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

1 个答案:

答案 0 :(得分:0)

看起来问题在于您提供的用于训练模型的标签。它们的形状为(None, 1),但模型的输出为(None, 29430),因此标签应具有相同的输出。