Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras 1.0: getting intermediate layer output

Tags:

keras

theano

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

like image 347
Louis M Avatar asked Apr 20 '16 13:04

Louis M


2 Answers

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]
like image 76
joydeep bhattacharjee Avatar answered Sep 23 '22 01:09

joydeep bhattacharjee


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

like image 26
Louis M Avatar answered Sep 26 '22 01:09

Louis M