Keras 1.0:获得中间层输出

Lou*_*s M 5 theano keras

我目前正在尝试可视化Keras 1.0中的中间层的输出(我可以用Keras 0.3做),但它不再起作用了.

x = model.input
y = model.layers[3].output
f = theano.function([x], y)
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

在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)
Run Code Online (Sandbox Code Playgroud)

所以我怀疑它来自"train = False"参数,我不知道如何设置新版本.

谢谢您的帮助

joy*_*jee 6

尝试:在import语句中首先给出

from keras import backend as K
from theano import function
Run Code Online (Sandbox Code Playgroud)

然后

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]
Run Code Online (Sandbox Code Playgroud)


Lou*_*s M 3

Fran\xc3\xa7ois Chollet 在 github 上刚刚回答了这个问题:

\n
\n

您的模型显然在训练和测试模式下有不同的行为,因此需要知道它应该使用什么模式。

\n

使用

\n

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

\n

并根据您希望模型处于训练模式还是测试模式,传递 1 或 0 作为学习阶段的值。

\n
\n

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

\n