【问题标题】:Get Ouput Of A Keras Model/Layer获取 Keras 模型/层的输出
【发布时间】:2017-05-25 01:33:23
【问题描述】:

我的 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) 用于查看模型实际在做什么

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

任何帮助将不胜感激。谢谢。

【问题讨论】:

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


    【解决方案1】:

    您可以简单地使用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 是模型的矢量化预测答案。

    【讨论】:

    • 非常感谢您的帮助。你能解释一下如何获得任何层的输出吗?我试过了,但我得到了问题中的错误。谢谢。
    • 对于任何层,您可以使用model.layers[index].input model.layers[index].output 来获取层的输入和输出。同样对于顺序模型,您可以将model.input model.output 用于整个模型的输入和输出。
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 2017-11-21
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多