I want to turn my keras model into a theano function so that I can compute the gradients on the inputs. I thought this might be cool for visualizing the network. I want to
Following the FAQ, try:
from keras import backend as K
get_last_layer_output = K.function([model.layers[0].input],
[model.layers[-1].output])
For the most recent version of Keras (1.0), use
from keras import backend as K
get_last_layer_output = K.function([model.layers[0].input],
[model.layers[-1].get_output(train=False)])
For "old" keras(0.3.x for example):
I don't use this version but examples like this one should work.
For "new" keras(1.0+):
Since you use Dropout
layer, you will need to add another input K.learning_phase()
and give it the value 0 (0 for testing, 1 for training.)
code:
from keras import backend as K
K.function([model.layers[0].input, K.learning_phase()], [model.layers[-1].output])
Reference: keras FAQ