问题
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