gpt4 book ai didi

python - 为什么 model.predict 与最后一层(keras)的输出不同?

转载 作者:行者123 更新时间:2023-12-01 08:07:08 25 4
gpt4 key购买 nike

我想评估一个模型,同时捕获倒数第二层的激活。我用过this answer寻求解决方案。我使用 pen_ulti_activs = layer_outs[-2] 访问倒数第二个激活。

但是为了仔细检查该解决方案是否确实有效,我在代码中添加了一个断言,通过比较来验证仿函数的激活是否与 model.predict 的激活实际匹配从仿函数返回的最后一层激活以及从 model.predict 返回的数组。但断言失败了。所以我想我误解了链接答案的用途。

from keras import backend as K


def evaluate_model(model, test_gen):


inp = model.input # input placeholder
outputs = [layer.output for layer in model.layers] # all layer outputs
functor = K.function([inp, K.learning_phase()], outputs ) # evaluation function


for inputs, targets in test_gen:

layer_outs = functor([inputs, 1.])


predictions = layer_outs[-1]

predictions_ = model.predict(inputs)

assert(np.allclose(predictions, predictions_))

那么:为什么 predictionspredictions_ 不相等? model.predict 不应该返回与最后一层的输出相同的结果吗?毕竟 mode.predict 返回最后一层的输出。

最佳答案

您没有提供有关模型的太多详细信息,因此只能猜测。一种可能性是您正在使用 softmax 交叉熵进行分类,在这种情况下,最后一层通常输出(非标准化)logits,而 predict()softmax 应用于此输出以返回标准化概率。

关于python - 为什么 model.predict 与最后一层(keras)的输出不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55485266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com