Search code examples
pythonkerasdeep-learningkeras-layer

Why do we need to create a new model to see intermediate layers' outputs?


In most examples I see where we want to see the intermediate layers' output values, it seems creating a new Model is the way to do:

from keras.models import Model

model = ...  # include here your original model

layer_name = 'my_layer'
intermediate_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)

(source: https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer)

However, what I don't understand about it is: what does this model do exactly? Why do we need it? Can't we just feed inputs to the original model having those layers, and pool its layer values directly?

Do you have to train this new model as well?

I cannot even imagine any diagram that can represent what is going on with this new model... You add the input layer of another model, then add a random intermediary layer of that other model as output, and feed inputs to it?

Also, won't this new model affect the output? Won't it try to learn or require training, or the layer brings its own weights pre trained from the original model, thus providing "altered" output values?


Solution

  • In the FAQ article you've linked, that's not the way, it's a simple way.

    A Model, after all, is just a graph of layers with a convenient API (.train(), .predict(), IO, ...).

    If you create a new Model from another model's input and output layer(s), you're essentially grabbing a slice of it that has the same convenient API.

    The layers in this slice model are as they were on the full model, so if the model was trained to begin with, so will this submodel. (With all likelihood, the layers are just references to the layers in the original model, so training the submodel would also train (likely to a bad result) the primary model.)