我可以在keras中对模型的一部分运行度量吗?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我可以在keras中对模型的一部分运行度量吗?相关的知识,希望对你有一定的参考价值。

我有一个带有自定义损失函数的序列模型用于训练。为了进行预测和验证,我想删除一层。有什么办法吗?我想到的最简单的事情是,通过获得上一层的输出值而无需访问输入,就可以在自定义指标内。另外,我可以在单独的模型上进行预测和验证,但是我担心构造单独的模型,因为我希望节省权重。有什么建议么?我花了很多时间进行此操作,我尝试做的任何事情都涉及范围问题。我看了一下:Keras, How to get the output of each layer?,但我看到的每个答案都要求我知道输入内容。

答案

您可以创建单独的模型。每个模型都需要编译。我的解决方案是这种形式...

inputs = Input(input_shape)
model = Conv2D(32, [3,3])(inputs)
# pass the model through some layers
# finish the model
model = Model(inputs=inputs, outputs=model)

input_2 = Input(input_shape)
second_model = model(input_2)
# pass the second model through some layers
second_model = Model(inputs=inputs, outputs=second_model)

model.compile(...
second_model.compile(...

现在对second_model进行的任何训练都会影响模型的权重,使您可以根据second_model和模型进行预测。

以上是关于我可以在keras中对模型的一部分运行度量吗?的主要内容,如果未能解决你的问题,请参考以下文章

Keras - 损失和度量的计算方式不同?

Keras-Tuner:是不是可以在目标/度量函数中使用测试/验证集?

如果我将层传递给两个 Keras 模型并且只训练一个模型,那么在前者训练后两个模型会共享权重吗

尝试从 Keras 运行顺序模型时出现 ValueError

无法使用 Plaidml 在 GPU 上运行 Keras 模型

Keras:恢复训练的加载检查点模型会降低准确性吗?