Keras training only specific outputs

后端 未结 2 498
盖世英雄少女心
盖世英雄少女心 2020-12-09 06:40

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])
mo         


        
相关标签:
2条回答
  • 2020-12-09 07:02

    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)
    
    0 讨论(0)
  • 2020-12-09 07:06

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

    0 讨论(0)
提交回复
热议问题