获得Keras模型/图层的输出

时间:2017-01-10 14:13:12

标签: python machine-learning deep-learning keras keras-layer

我的Keras模型是Keras存储库中的babi_rnn example

我想在数据集上获得模型的输出(单词)。

我试过了:

layer = model.layers[-1] # For the last layer
    f = K.function([model.get_input(train=False)], [layer.get_output(train=False)])
    print(f([x])[0])  # your activation tensor

但是我收到了错误:

AttributeError: 'Sequential' object has no attribute 'get_input'

如何在输入输入时简单地获取模型或图层的输出?

也就是说,我需要

    # I supply the X list and want to get the Y list.

    Y = Model(X) # X and Y are both lists. Model.Layer[Index] can also be a use case.

    # The responses are the set of label probabilities for each word in the vocabulary.

所以我可以:for x, y in zip(X,Y): print(x,y)模型实际做什么

我认为这应该是最简单的用例,但实现起来很麻烦。

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:2)

您只需使用model.predict获取Y predict函数内部调用_make_predict_function()即可完成您要执行的操作。

但是你的模型经过训练可以将某些类型的输入映射到某种类型的输出......所以你需要在使用predict函数时处理它们并解释它们。在此示例中,此转换在vectorize_stories()中完成,因此请尝试并了解它正在做什么。

在这种情况下,为了得到单词预测,在训练模型后你需要做的就是:

Y_pred = model.predict([tX, tXq])
for pred in Y_pred:
    print (vocab[pred.argmax()-1])

再次注意tX是矢量化测试故事tXq是矢量化测试查询,Y_pred是模型的矢量化预测答案。