2
$\begingroup$

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.

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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