4

I'm following the tutorial for deep autoencoders in keras here. For the simple autoencoder in the beginning there is a decoder defined like this:

# 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))

This doesn't work anymore if you have more than one decoder layer. How to do similar if I have three decoder layers?

encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(32, activation='relu')(encoded)

decoded = Dense(64, activation='relu')(encoded)
decoded = Dense(128, activation='relu')(decoded)
decoded = Dense(784, activation='sigmoid')(decoded)

autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)

For encoder it does work easily, but how to get a model of the last three layers?

3
  • Do you mean you want to use a NN of few layers as your decoder? Or you want to use few different decoders with the same input?
    – SRC
    Commented Jun 19, 2017 at 13:50
  • I have an input layer (say 784 neurons), and then some encoder layers with shrinking neurons (say til 32 neurons), followed by decoder layers (now growing back to 784 neurons). The thing is, after training I want to use only parts of the network (either the encoder or the decoder layers). Commented Jun 19, 2017 at 14:42
  • 1
    I am not sure if this helps but may be closer to something you are looking for. - github.com/fchollet/keras/issues/358#issuecomment-119379780
    – SRC
    Commented Jun 19, 2017 at 14:51

1 Answer 1

4

Try (following this answer):

# retrieve the last layer of the autoencoder model 
decoder_layer1 = autoencoder.layers[-3]
decoder_layer2 = autoencoder.layers[-2]
decoder_layer3 = autoencoder.layers[-1]

# create the decoder model
decoder = Model(input=encoded_input, 
output=decoder_layer3(decoder_layer2(decoder_layer1(encoded_input))))

Not the answer you're looking for? Browse other questions tagged or ask your own question.