Louis M
Louis M

Reputation: 4314

Keras 1.0: getting intermediate layer output

I am currently trying to visualize the output of an intermediate layer in Keras 1.0 (which I could do with Keras 0.3) but it does not work anymore.

x = model.input
y = model.layers[3].output
f = theano.function([x], y)

But I get the following error:

MissingInputError: ("An input of the graph, used to compute DimShuffle{x,x,x,x}(keras_learning_phase), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.", keras_learning_phase)

Prior to Keras 1.0, with my graph model, I could just do:

x = graph.inputs['input'].input
y = graph.nodes[layer].get_output(train=False)
f = theano.function([x], y, allow_input_downcast=True)

So I suspect it to come from the "train=False" parameter which I don't know how to set in the new version.

Thank you for your help

Upvotes: 5

Views: 5292

Answers (2)

joydeep bhattacharjee
joydeep bhattacharjee

Reputation: 1317

Try: In the import statements first give

from keras import backend as K
from theano import function

then

f = K.function([model.layers[0].input, K.learning_phase()],
                              [model.layers[3].output])
# output in test mode = 0
layer_output = get_3rd_layer_output([X_test, 0])[0]

# output in train mode = 1
layer_output = get_3rd_layer_output([X_train, 1])[0]

Upvotes: 6

Louis M
Louis M

Reputation: 4314

This was just answered by François Chollet on github:

Your model apparently has a different behavior in training and test mode, and so needs to know what mode it should be using.

Use

iterate = K.function([input_img, K.learning_phase()], [loss, grads])

and pass 1 or 0 as value for the learning phase, based on whether you want the model in training mode or test mode.

https://github.com/fchollet/keras/issues/2417

Upvotes: 3

Related Questions