How to separately use the encoder of an Autoencoder in keras?

社会主义新天地 提交于 2019-12-07 14:10:33

问题


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 ?


回答1:


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)


来源:https://stackoverflow.com/questions/39551478/how-to-separately-use-the-encoder-of-an-autoencoder-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!