【问题标题】:How can I match the encoder decoder dimension of deep auto encoder?如何匹配深度自动编码器的编码器解码器尺寸?
【发布时间】:2020-05-22 00:51:23
【问题描述】:

我正在使用具有三个潜在空间层的 MNIST 数据集编写非常简单的深度自动编码器。 但是编码器和解码器的维度都存在问题。

确切的错误消息是:ValueError: Error when checks input: expected input_2 to have shape (128,) but got array with shape (32,) at line 60.

(第 60 行:decoded_imgs = decoder.predict(encoded_imgs))

我不知道如何解决它。我将在下面附上我的完整代码。 请帮忙。谢谢。

    from keras.layers import Input, Dense
    from keras.models import Model
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import mlab
    from matplotlib import pyplot as plt
    import sys
    np.set_printoptions(threshold=sys.maxsize)


    # encoding_dimensions
    encoding_dim = 128
    encoding_dim2 = 64
    encoding_dim3 = 32

    # input placeholder
    input_img = Input(shape=(784,))
    encoded = Dense(128, activation='relu')(input_img)
    encoded = Dense(64, activation='relu')(encoded)
    encoded = Dense(32, activation='relu')(encoded)

    decoded = Dense(64, activation='relu')(encoded)
    decoded = Dense(128, activation='relu')(decoded)
    decoded = Dense(784, activation='sigmoid')(decoded)

    print(encoded.shape)
    print(decoded.shape)

    autoencoder = Model(input_img, decoded)
    autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')


    encoder = Model(input_img, encoded)
    encoded_input = Input(shape=(encoding_dim,))


    decoder_layer = autoencoder.layers[-1]
    decoder = Model(encoded_input, decoder_layer(encoded_input))

    from keras.datasets import mnist
    (x_train, _), (x_test, _) = mnist.load_data()

    x_train = x_train.astype('float32') / 255.
    x_test = x_test.astype('float32') / 255.

    x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
    x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

    print(x_test.shape)
    print(x_train.shape)

    autoencoder.fit(x_train, x_train,
                    epochs=1,
                    batch_size=256,
                    shuffle=True,
                    validation_data=(x_test, x_test))

    encoded_imgs = encoder.predict(x_test)
    decoded_imgs = decoder.predict(encoded_imgs)

    n=10
    plt.figure(num=2, figsize=(20, 3))
    for i in range(n):
        # input data
        ax = plt.subplot(2, n, i + 1)
        plt.imshow(x_test[i].reshape(28, 28))
        plt.gray()
        ax.get_yaxis().set_visible(False)
        ax.get_xaxis().set_visible(False)

        # recnstructed data
        ax = plt.subplot(2, n, i + 1 + n)
        plt.imshow(decoded_imgs[i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    plt.show()

【问题讨论】:

    标签: python keras tensorflow2.0 autoencoder


    【解决方案1】:

    问题在于decoder 模型的输入定义。您将解码器模型的输入设置为:encoded_input = Input(shape=(encoding_dim,)) 其中encoding_dim=128。然而,解码器的真正输入是编码器输出,其大小为 32(编码器最后Dense 层中的神经元数量)。您需要修复解码器模型中输入形状的声明,例如:

    encoded_input = Input(shape=(32,))
    

    或一般模式:

    encoded_input = Input(shape=encoded.shape)
    

    【讨论】: