【问题标题】:Inverse of keras.Flatten for building autoencoder用于构建自动编码器的 keras.Flatten 的逆
【发布时间】:2020-08-25 22:38:08
【问题描述】:

我的目标是构建一个卷积自动编码器,将输入图像编码为大小为(10,1) 的平面向量。我按照 keras documentation 中的示例进行了修改以用于我的目的。不幸的是,模型是这样的:

input_img = Input(shape=(28, 28, 1))

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Flatten()(x)

encoded = Dense(units = 10, activation = 'relu')(x)


x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)

给我

ValueError: Input 0 is incompatible with layer conv2d_39: expected ndim=4, found ndim=2

我想我应该在我的解码器中添加一些层来反转 Flatten 的效果,但我不确定是哪一层。你能帮忙吗?

【问题讨论】:

    标签: python keras autoencoder


    【解决方案1】:

    为什么要为向量专门设置 (10,1) 形状? 然后您尝试使用大小为 3x3 的内核对其进行卷积,这实际上没有意义。

    卷积层的形状有高度、宽度和通道。密集层的输出必须被重塑,这可以通过重塑层来完成。 然后,您可以将其重塑为单通道 5x2。

    encoded = Reshape((5, 2, 1))(encoded)
    

    【讨论】:

    • 未与 10 结婚。只是在一维向量中编码后的参数数量较少。因此,如果编码器的最后一个最大池化给了我形状(None, 7, 7, 8),这意味着在 Flatten 之后我会有 7*7*8 = 392 的输出形状,对吧?所以说我将它连接到我的 10 个单元。然后我应该添加另一个 Dense 层(解码器中的第一个,去取回我的 392 输出,并将其重塑为 (7, 7, 8)?
    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 2021-10-18
    • 2020-05-08
    • 2019-08-17
    • 2019-10-18
    • 2018-09-20
    • 2018-10-04
    • 2021-12-19
    相关资源
    最近更新 更多