我正在使用具有张量流后端的Keras 2.04。我想在MNIST图像上用ImageDataGenerator训练一个简单的模型。但是,我一直从fit_generator收到以下错误:
ValueError:检查输入时出错:期望input_1有2 尺寸,但有阵列形状(8,28,28,1)。
这是代码:
#loading data & reshaping
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28, 28,1)
#building the model
input_img = Input(shape=(784,))
encoded = Dense(30, activation='relu')(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='mse')
#creating ImageDataGenerator
datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True)
datagen.fit(x_train)
autoencoder.fit_generator(
#x_train, x_train because the target is to reconstruct the input
datagen.flow(x_train, x_train, batch_size=8),
steps_per_epoch=int(len(x_train)/8),
epochs=64,
)
据我所知,ImageDataGenerator应该每次迭代生成一批训练样例,就像它实际上一样(在这种情况下,batch_size = 8)但是从错误中看起来好像它需要一个训练样例。
谢谢!
答案 0 :(得分:0)
解决了 - 应该是:
autoencoder = Sequential()
autoencoder.add(Reshape((784,), input_shape=(28,28,1)))
autoencoder.add(Dense(30, activation='relu'))
autoencoder.add(Dense(784, activation='relu'))
.
.
.
autoencoder.fit_generator(
datagen.flow(x_train, x_train.reshape(len(x_train),784,), batch_size=8),
steps_per_epoch=int(len(x_train)/8),
epochs=64,
)