Keras 只训练特定的输出 [英] Keras training only specific outputs

查看:23
本文介绍了Keras 只训练特定的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Kears 与 tensorflow 一起使用,并且我有一个带有 3 个输出的模型,我只想训练其中的 2 个.

I am using Kears with tensorflow and I have a model with 3 output out of which I only want to train 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 因为它用于测试).有人能告诉我如何实现这一点,或者让我放心,代码实际上是我想要的吗?

I tried to do it with the code above but I am not sure it does what I want do do. So I think it adds up the losses and it trains each output with that loss meanwhile I do not wish to train out3 at all. (I need out3 because it is used in testing). Could anybody tell me how to achieve this or reassure me that the code actually dose what I want?

推荐答案

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

You have to create 2 different models like this

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

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

You compile both but only fit the first. They will share the layers so model2, even if it wasn't trained, will have the weights learned from model1. But if there is a layer in out3 which is trainable but not in the flow between input and out1 and out2 of the graph, that layer wont be trained so will stay wirh its inital values.

这有帮助吗?:-)

这篇关于Keras 只训练特定的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆