我正在尝试使用 CNN 作为编码器和 LSTM 作为解码器为我的术语项目构建一个自动编码器,当我显示模型的摘要时。我收到以下错误:
值错误:输入 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)
鉴于您使用的是 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 实现了 LSTM 如何重塑 3 通道数据集以输入神经网络。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)