python - Keras 只训练特定的输出

标签 python neural-network keras

我将 Kears 与 tensorflow 结合使用,我有一个具有 3 个输出的模型,我只想训练其中的 2 个。

model = Model(input=input, output=[out1,out2,out3])
model.compile(loss=[loss1, loss2, loss3], optimizer=my_optimizer)

loss1(y_true, y_pred):
    return calculate_loss1(y_true, y_pred)

loss2(y_true, y_pred):
    return calculate_loss2(y_true, y_pred)

loss3(y_true, y_pred):
    return 0.0*K.mean(y_pred)

我试着用上面的代码来做,但我不确定它是否做了我想做的事。所以我认为它把损失加起来,它用损失来训练每个输出,同时我根本不想训练 out3。 (我需要 out3 因为它用于测试)。任何人都可以告诉我如何实现这一点,或者让我放心,代码实际上提供了我想要的东西吗?

最佳答案

你必须像这样创建 2 个不同的模型

model1 = Model(input=input, output=[out1,out2])
model2 = Model(input=input, output=[out1,out2,out3])

你编译两个但只适合第一个。他们将共享层,因此 model2,即使它没有经过训练,也会从 model1 中学习权重。但是如果 out3 中有一个层是可训练的,但不是在图的输入和 out1 和 out2 之间的流中,则该层将不会被训练,因此将保持其初始值。

这有帮助吗? :-)

关于python - Keras 只训练特定的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42785433/

相关文章:

python多处理: sleep statement killed?

neural-network - 用于神经网络训练的数据集

multithreading - TensorFlow/Keras 多线程模型拟合

python - Keras模型连接: Attribute and Value error

python - Tensorflow,预期 conv2d_input 有 4 个维度

python - 使用 "is"来检查变量中包含哪个函数是个好主意吗?

python - 将不同语言的文本转换为最新的

artificial-intelligence - 有人可以解释一下人工神经网络吗?

artificial-intelligence - 神经网络的大输出

python-3.x - Keras 模型属性错误 : 'str' object has no attribute 'call'