预期density_218_input具有2维,但数组的形状为(512,28,28,1) [英] expected dense_218_input to have 2 dimensions, but got array with shape (512, 28, 28, 1)

查看:63
本文介绍了预期density_218_input具有2维,但数组的形状为(512,28,28,1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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:检查输入时出错:预期density_218_input具有2维,但数组的形状为(512,28,28,1)

解决方案

dense_218_input应该是一个numpy二维数组,而不是形状:(512,28,28,1).您可以使用 numpy.reshape 对其进行重塑. /p>

I am trying to augment my MNIST dataset in keras but for some reason its not working. Any help will be appreciated.

Part of the code:

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

error:

ValueError: Error when checking input: expected dense_218_input to have 2 dimensions, but got array with shape (512, 28, 28, 1)

解决方案

The dense_218_input should be a numpy 2d-array instead of shape: (512, 28, 28, 1). You can reshape it with numpy.reshape.

这篇关于预期density_218_input具有2维,但数组的形状为(512,28,28,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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