Search code examples
pythonkerasneural-networkartificial-intelligenceconv-neural-network

Is it possible to set a middle layer as an output layer in keras


I would like to try out an idea about autoencoder. The model is like this:

input (pictures) - conv2d - pooling - dense - dense(supervised output) - dense - conv - upsampling - output (pictures)

If it is possible to train the NN having desired outputs for dense(supervised output) and output (pictures)? In other words, I want to make a classifier-and-back.


Solution

  • This can be done with the Keras functional API (https://keras.io/getting-started/functional-api-guide/).

    A minimal example, where the model has 2 outputs, one from an intermediate layer, and one from the final layer:

    import keras
    input = keras.layers.Input(shape=(3,))
    
    intermediate = keras.layers.Dense(10)(input)
    final_output = keras.layers.Dense(3)(intermediate)
    
    model = keras.Model(inputs=input, outputs=[intermediate, final_output])