Deshwal
Deshwal

Reputation: 4152

Get the output of a specific layer as the result on test data in place of last layer (auto-encoder latent features) in keras

I am trying to get the output of the latent layer/hidden layer to use it as input for something else. I trained my model in an efficient way to minimize the loss so my model could learn the latent features efficiently and as close as possible to the image. My model is

input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

#Encoder
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)

encoded = MaxPooling2D((2, 2), padding='same')(x) 


# Decoder
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded) 
x = UpSampling2D((2, 2))(x) # opposite of Pooling
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

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

I want the output of encoded layer as my output for the model. Is it possible? ad If yes, Please tell me how.

Upvotes: 2

Views: 334

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

you can simply do in this way

autoencoder.fit(...)

latent_model = Model(input_img, encoded)
latent_representation = latent_model.predict(X)

Upvotes: 4

Related Questions