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?

Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
ScientiaEtVeritas
  • 5,158
  • 4
  • 41
  • 59
  • 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 Jun 19 '17 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). – ScientiaEtVeritas Jun 19 '17 at 14:42
  • 1
    I am not sure if this helps but may be closer to something you are looking for. - https://github.com/fchollet/keras/issues/358#issuecomment-119379780 – SRC Jun 19 '17 at 14:51

1 Answers1

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))))
Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120