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)
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?
You have to create 2 different models like this
model1 = Model(input=input, output=[out1,out2])
model2 = Model(input=input, output=[out1,out2,out3])
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.
Does that help? :-)
You can set one of the losses to None
:
model = Model(input=input, output=[out1,out2,out3])
model.compile(loss=[loss1, loss2, None], 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With