2016-04-20 73 views
4

我目前正在試圖在Keras 1.0(我可以用Keras 0.3做的)中可視化中間層的輸出,但它不再工作。Keras 1.0:獲取中間層輸出

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

,但我得到了以下錯誤:

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) 

此前Keras 1.0,用我的圖模型,我可以這樣做:

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

所以我懷疑它來自「train = False」參數,我不知道如何在新版本中設置。

謝謝您的幫助

+0

請注意,Keras 1.0不再支持圖形模型。順序模型使用「合併」選項進行了擴展,「圖形」模型被功能API取代。我建議你仔細閱讀:http://keras.io/getting-started/functional-api-guide/以獲得更多信息。 –

回答

3

這只是由弗朗索瓦CHOLLET回答在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

5

嘗試: 在import語句先給

from keras import backend as K 
from theano import function 

然後

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]