构建自动编码器时收到错误

时间:2019-03-26 14:49:15

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

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

  

ValueError:输入0与lstm_10层不兼容:预期ndim = 3,找到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)

1 个答案:

答案 0 :(得分:0)

鉴于您正在使用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)