本文介绍了如何在keras中单独使用自动编码器的编码器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了以下自动编码器模型:

I have trained the following autoencoder model:

input_img = Input(shape=(1, 32, 32))

x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)


x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu',border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

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

autoencoder.fit(X_train, X_train,
            nb_epoch=1,
            batch_size=128,
            shuffle=True,
            validation_data=(X_test, X_test)]
            )

训练完此自动编码器后,我想将训练有素的编码器用于受监视的线路.如何仅提取此自动编码器模型中训练有素的编码器部分?

After training this autoencoder i want to use the trained encoder for a supervised line. How can i extract only the trained encoder part of this autoencoder model ?

推荐答案

您可以在训练后仅使用编码器的情况下创建模型:

You can just create a model after training that only uses the encoder:

autoencoder = Model(input_img, encoded)

如果要在编码部分之后添加其他层,也可以执行以下操作:

If you want to add further layers after the encoded portion, you can do that as well:

classifier = Dense(nb_classes, activation='softmax')(encoded)
model = Model(input_img, classifier)

这篇关于如何在keras中单独使用自动编码器的编码器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:23