【问题标题】:Error Received while building the Auto encoder构建自动编码器时收到错误
【发布时间】:2019-08-17 00:09:41
【问题描述】:

我正在尝试为我的学期项目构建一个自动编码器,使用 CNN 作为编码器和 LSTM 作为解码器,但是当我显示模型的摘要时。我收到以下错误:

ValueError: Input 0 is in compatible with layer lstm_10: expected ndim=3, found ndim=2

x.shape = (45406, 100, 100)
y.shape = (45406,)

我已经尝试更改 LSTM 的输入形状,但没有奏效。

def keras_model(image_x, image_y):

model = Sequential()
model.add(Lambda(lambda x: x / 127.5 - 1., input_shape=(image_x, image_y, 1)))

last = model.output
x = Conv2D(3, (3, 3), padding='same')(last)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), padding='valid')(x)

encoded= Flatten()(x)
x = LSTM(8, return_sequences=True, input_shape=(100,100))(encoded)
decoded = LSTM(64, return_sequences = True)(x)

x = Dropout(0.5)(decoded)
x = Dense(400, activation='relu')(x)
x = Dense(25, activation='relu')(x)
final = Dense(1, activation='relu')(x)

autoencoder = Model(model.input, final)

autoencoder.compile(optimizer="Adam", loss="mse")
autoencoder.summary()

model= keras_model(100, 100)

【问题讨论】:

    标签: deep-learning lstm recurrent-neural-network autoencoder


    【解决方案1】:

    鉴于您使用的是 LSTM,您需要一个时间维度。所以你的输入形状应该是:(时间,image_x,image_y,nb_image_channels)。

    我建议更深入地了解自动编码器、LSTM 和 2D 卷积,因为所有这些都在这里一起发挥作用。这是一个有用的介绍:https://machinelearningmastery.com/lstm-autoencoders/ 和这个https://blog.keras.io/building-autoencoders-in-keras.html)。

    也看看这个例子,有人用 Conv2D How to reshape 3 channel dataset for input to neural network 实现了一个 LSTM。 TimeDistributed 层在这里很有用。

    然而,为了解决你的错误,你可以添加一个 Reshape() 层来伪造额外的维度:

    def keras_model(image_x, image_y):
    
        model = Sequential()
        model.add(Lambda(lambda x: x / 127.5 - 1., input_shape=(image_x, image_y, 1)))
    
        last = model.output
        x = Conv2D(3, (3, 3), padding='same')(last)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((2, 2), padding='valid')(x)
    
        encoded= Flatten()(x)
        # (50,50,3) is the output shape of the max pooling layer (see model summary)
        encoded = Reshape((50*50*3, 1))(encoded)
        x = LSTM(8, return_sequences=True)(encoded)  # input shape can be removed
        decoded = LSTM(64, return_sequences = True)(x)
    
        x = Dropout(0.5)(decoded)
        x = Dense(400, activation='relu')(x)
        x = Dense(25, activation='relu')(x)
        final = Dense(1, activation='relu')(x)
    
        autoencoder = Model(model.input, final)
    
        autoencoder.compile(optimizer="Adam", loss="mse")
        print(autoencoder.summary())
    
    model= keras_model(100, 100)
    

    【讨论】:

    • 非常感谢。你会推荐在图层之前使用 TimeDistributed 吗?
    • 是的,建议将其用作图层的包装器
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多