Search code examples
keraskeras-layer

How to create a keras 'Decoder model' that begins from an intermediate layer and extends to output layer


In an Autoencoder it is easy to create an Encoder Model as we have a clear Input layer. But I am puzzled as to how to create a Decoder model. For example, here are the layers:

m = Sequential()

##  Encoder 
m.add(Dense(512,  activation='elu', input_shape=(784,)))
m.add(Dense(128,  activation='elu'))
m.add(Dense(2,
            activation='linear',
            name="bottleneck")   
            )  


##  Decoder
m.add(Dense(128,  activation='elu', name = "first_decode_layer"))
m.add(Dense(512,  activation='elu'))
m.add(Dense(784,  activation='sigmoid', name = "output_layer")) 
#  Compile the model
m.compile(
          loss='mean_squared_error',
          optimizer = Adam()
         )

Now an encoder model is easily created, as:

encoder = Model(m.input,                         
                m.get_layer('bottleneck').output  
               )

But, I am at a loss to understand as to how to create a decoder model. For example, this does not work:

decoder = Model(m.get_layer("first_decode_layer").input,                          
                    m.get_layer('output_layer').output  
                )

The error requires that I should have an Input layer. It says:

"inputs must come from `keras.layers.Input` (thus holding past layer
metadata), they cannot be the output of a previous non-Input layer.
Here, a tensor specified as input to your model was not an Input tensor, "

I will be grateful for guidance.


Solution

  • #encoder
    encoder = Sequential()
    ....
    encoder.add(...."bottleneck")
    
    #decoder
    decoder = Sequential()
    decoder.add(......"first_decoder_layer")
    ...
    decoder.add(......"output_layer")
    
    #autoencoder
    auto_out = decoder(encoder.output)
    autoencoder = Model(encoder.input, auto_out)