【发布时间】:2020-07-11 16:05:36
【问题描述】:
我尝试使用 Keras 构建一个简单的自动编码器,为此我从单个全连接神经层作为编码器和解码器开始。
> input_img = Input(shape=(784,))
>encoded = Dense(encoding_dim,activation='relu')(input_img)
>decoded = Dense(784, activation='sigmoid')(encoded)
>autoencoder =Model(input_img, decoded)
我还借助
创建了一个单独的编码器模块encoder = Model(input_img, encoded)
以及解码器模型:
encoded_input = Input(shape=(32,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
然后我训练了模型
autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
但即使我没有训练我的编码器和解码器,即使我在训练之前通过了层,它们也会共享自动编码器的权重。我只训练了编码器,但编码器和解码器都在接受训练。
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
【问题讨论】:
-
自动编码器不能这样工作。编码器和解码器都通过优化损失或再现误差一起训练。然后根据需要我们可以解耦编码器和解码器并相应地使用它。
标签: python keras keras-layer