Reputation: 1654
following keras FAQ example [1] I want to clarify how to get prediction from intermediate layer. Let's say I have several dense layers with dropouts, e.g.
model = Sequential()
model.add(Dense(out_dim, input_dim=input_dim, name="layer1"))
model.add(LeakyReLU()) # or whatever activation you want to use
model.add(Dropout(0.5))
model.add(Dense(int_dim, name="layer2"))
.... # here we have activation, dropout, etc.
model.add(Dense(lat_dim, name="layerN"))
# at last we add sigmoid and yield probabilities
model.add(Dense(1))
model.add(Activation("sigmoid"))
model.summary()
And, I'm interested to get prediction from layerN including all previous layers. The [1] recommend several approaches, the first one to build another model, e.g.
new_model = Model(inputs=model.input,
outputs=model.get_layer("layerN").output)
output = new_model.predict(data)
My question is does new model takes input vector and only yield prediction from "layerN" or it actually propagates my input through all layers (including dropouts, etc.) up to a "layerN" and then produce predictions? If it is the former, does it mean that I need to basically build new model with all layers as original up-to "layerN"?
Best, Valentin.
[1] https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer
Upvotes: 2
Views: 2002
Reputation: 56387
Yes, you have to build a model including all the layers you want as outputs, this is not automatically done.
Upvotes: 2