我的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)
看模型实际做什么。
我认为这应该是最简单的用例,但实现起来很麻烦。
非常感谢任何帮助。感谢。
答案 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
是模型的矢量化预测答案。