How to turn entire keras model into theano function

六月ゝ 毕业季﹏ 提交于 2019-12-30 06:41:08

问题


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 use these gradients to enhance features in the original image based on what the neural network thinks they are. I do not understand what I am doing wrong with the following code.

model = Sequential()
model.add(InputLayer((3, H, W)))
model.add(GaussianNoise(0.03))

model.add(Flatten())
model.add(Dense(512, activation = 'relu', name = 'dense'))
model.add(Dropout(0.2))
model.add(Dense(20, activation = 'relu'))
model.add(Dense(C, activation = 'softmax', W_regularizer = l2()))
...
f = theano.function([model.input], model.output)

I get the following exception.

theano.gof.fg.MissingInputError: A variable that is an input to the graph was neither provided as an input to the function nor given a value. A chain of variables leading from this input to an output is [keras_learning_phase, DimShuffle{x,x}.0, Elemwise{switch,no_inplace}.0, dot.0, Elemwise{add,no_inplace}.0, Elemwise{add,no_inplace}.0, Elemwise{mul,no_inplace}.0, dot.0, Elemwise{add,no_inplace}.0, Softmax.0]. This chain may not be unique
Backtrace when the variable is created:
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/usr/local/lib/python3.5/dist-packages/keras/backend/__init__.py", line 51, in <module>
    from .theano_backend import *
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/usr/local/lib/python3.5/dist-packages/keras/backend/theano_backend.py", line 13, in <module>
    _LEARNING_PHASE = T.scalar(dtype='uint8', name='keras_learning_phase')  # 0 = test, 1 = train

回答1:


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)])



回答2:


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



来源:https://stackoverflow.com/questions/37221621/how-to-turn-entire-keras-model-into-theano-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!