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

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

问题描述

我正在使用带有tensorflow的Kears,并且我有一个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])

您可以同时编译两者,但只适合第一个.他们将共享各层,因此,即使没有对model2进行训练,也将具有从model1学习的权重.但是,如果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天全站免登陆