Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras training only specific outputs

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?

like image 644
DalekSupreme Avatar asked Mar 14 '17 11:03

DalekSupreme


2 Answers

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? :-)

like image 157
Nassim Ben Avatar answered Oct 31 '22 16:10

Nassim Ben


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)
like image 1
cserpell Avatar answered Oct 31 '22 15:10

cserpell