I'm creating a neural architecture using the functional API as follows:

x2 = layer1(x1, name='layer1')
x3 = layer2(x2, name='layer2')
m1 = Model(x1, x3)
x5 = layer3(x4, name='layer3')
x6 = TimeDistributed(m1)(x5)
m2 = Model(x5, x4)

I've trained m2 (with m1 getting implicitly trained). I can access the output of the layers of m2 (say layer3) by creating an intermediate model:

layer_name = 'layer3'
intermediate_layer_model = Model(input=model.input, output=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(input)

Since m1 is a nested model, I'm not able to access the layers of m1 through m2. How do I obtain the output x3 (layer2's output) when you use the trained m2 to predict on a new data point.

  • $\begingroup$ Check model.summary() and you can see which layers you can access $\endgroup$
    – enterML
    Commented Jun 1, 2017 at 6:01

1 Answer 1


The key is to first do .get_layer on the Model object, then do another .get_layer on that specifying the specific layer, THEN do .output:

layer_output = model.get_layer('Your-Model-Object').get_layer('the-layer-contained-in-your-Model-object').output

  • $\begingroup$ This will create a layer output but it cannot be used to predict the given input. $\endgroup$
    – Saurav Rai
    Commented Sep 29, 2020 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.