期望dense_218_input有2个维度,但得到形状的数组(512,28,28,1)

时间:2018-02-18 09:05:42

标签: deep-learning keras mnist

我正在尝试在keras中扩充我的MNIST数据集但由于某种原因它无法正常工作。任何帮助将不胜感激。

部分代码:

x_train = x_train.reshape(x_train.shape[0],28, 28,1)
x_test = x_test.reshape(x_test.shape[0],28, 28,1)

x_train = x_train.reshape(x_train.shape[0],28, 28,1)
x_test = x_test.reshape(x_test.shape[0],28, 28,1)


datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2)


model.compile(loss='categorical_crossentropy',
              optimizer= adam,
              metrics=['accuracy'])


train_gen = datagen.flow(x_train, r_train, batch_size=batch_size)

history2 = model.fit_generator(train_gen,
                              steps_per_epoch=int(np.ceil(x_train.shape[0] / float(batch_size))),
                              epochs=epochs)


# history = model.fit(x_train, r_train,
#                     batch_size=batch_size,
#                     epochs=epochs,
#                     verbose=1,
#                     validation_data=(x_test, r_test))

score = model.evaluate(x_test, r_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

错误:

ValueError:检查输入时出错:期望的dense_218_input有2个维度,但得到的数组有形状(512,28,28,1)

1 个答案:

答案 0 :(得分:0)

dense_218_input应该是一个numpy二维数组而不是形状:(512,28,28,1)。您可以使用numpy.reshape重新整形。