【问题标题】:Keras 1.0: getting intermediate layer outputKeras 1.0:获取中间层输出
【发布时间】:2016-04-20 13:45:15
【问题描述】:

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

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”参数。

感谢您的帮助

【问题讨论】:

  • 请注意,Keras 1.0 不再支持图形模型。使用 Merge 选项扩展了顺序模型,并用功能 API 替换了 Graph 模型。我建议您仔细阅读:keras.io/getting-started/functional-api-guide 以获取更多信息。

标签: theano keras


【解决方案1】:

尝试: 在导入语句中首先给出

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]

【讨论】:

    【解决方案2】:

    François Chollet 刚刚在 github 上回答了这个问题:

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

    使用

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多