在编译自编码器时出现错误?

这是我的自编码器模型:

input_img = Input(shape=(1, 32, 32))x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)x = MaxPooling2D((2, 2), border_mode='same')(x)x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)x = MaxPooling2D((2, 2), border_mode='same')(x)x = Convolution2D(8, 2, 2, activation='relu', border_mode='same')(x)encoded = MaxPooling2D((2, 2), border_mode='same')(x)x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)x = UpSampling2D((2, 2))(x)x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)x = UpSampling2D((2, 2))(x)x = Convolution2D(16, 3, 3, activation='relu')(x)x = UpSampling2D((2, 2))(x)decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)autoencoder = Model(input_img, decoded)autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

这是我的训练和预测函数:

autoencoder.fit(X_train, X_train,            nb_epoch=10,            batch_size=128,            shuffle=True,            validation_data=(X_test, X_test))decoded_imgs = autoencoder.predict(X_test)

当我尝试编译时,我得到了以下错误。我的数据集中的所有图像都是32×32像素。为什么会出现这个错误?

Exception: Error when checking model target: expected convolution2d_7 to have shape (None, 1, 28, 28) but got array with shape (4200, 1, 32, 32)

我需要对模型做哪些更改,使输入形状变为(1,32,32)?


回答:

这很简单:

input_img = Input(shape=(1, 32, 32))x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)x = MaxPooling2D((2, 2), border_mode='same')(x)x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)x = MaxPooling2D((2, 2), border_mode='same')(x)x = Convolution2D(8, 2, 2, activation='relu', border_mode='same')(x)encoded = MaxPooling2D((2, 2), border_mode='same')(x)x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)x = UpSampling2D((2, 2))(x)x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)x = UpSampling2D((2, 2))(x)x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)x = UpSampling2D((2, 2))(x)decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)autoencoder = Model(input_img, decoded)autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

你忘记在第六个卷积层添加适当的 border_mode='same' 参数了。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注