gpt4 book ai didi

python - 构建卷积自动编码器时的尺寸错误

转载 作者:行者123 更新时间:2023-11-30 22:48:55 27 4
gpt4 key购买 nike

我正在 Keras 中迈出第一步,并与层的尺寸作斗争。我目前正在构建一个卷积自动编码器,我想使用 MNIST 数据集对其进行训练。不幸的是,我似乎无法获得正确的尺寸,而且我很难理解我的错误在哪里。

我的模型是通过以下方式构建的:

def build_model(nb_filters=32, nb_pool=2, nb_conv=3):
input_img = Input(shape=(1, 28, 28))

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

return Model(input_img, decoded)

并且使用以下方式检索数据:

def load_data():
(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 1, 28, 28))
x_test = np.reshape(x_test, (len(x_test), 1, 28, 28))
return x_train, x_test

如您所见,我正在尝试标准化图像以以黑白显示它们,并且只是训练自动编码器以能够恢复它们。

下面您可以看到我收到的错误:

Traceback (most recent call last): File "C:/Users//Documents/GitHub/main/research/research_framework/experiment.py", line 46, in callbacks=[EarlyStopping(patience=3)]) File "C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\keras\engine\training.py", line 1047, in fit batch_size=batch_size) File "C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\keras\engine\training.py", line 978, in _standardize_user_data exception_prefix='model target') File "C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\keras\engine\training.py", line 111, in standardize_input_data str(array.shape)) Exception: Error when checking model target: expected convolution2d_7 to have shape (None, 8, 32, 1) but got array with shape (60000L, 1L, 28L, 28L) Total params: 8273

Process finished with exit code 1

你能帮我解密这个错误吗?除了 Keras 网站之外,还有关于构建模型和处理此类问题的 Material 吗?

干杯

最佳答案

您的输入形状似乎不正确。尝试将 (1,28,28) 更改为 (28,28,1),看看这是否适合您。有关更多详细信息和解决问题的其他选项,请参阅the answer to another question .

关于python - 构建卷积自动编码器时的尺寸错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39997894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com