Training only one output of a network in Keras

前端 未结 2 1241
耶瑟儿~
耶瑟儿~ 2020-12-14 23:58

I have a network in Keras with many outputs, however, my training data only provides information for a single output at a time.

At the moment my method for training

相关标签:
2条回答
  • 2020-12-15 00:41

    Outputting multiple results and optimizing only one of them

    Let's say you want to return output from multiple layers, maybe from some intermediate layers, but you need to optimize only one target output. Here's how you can do it:

    Let's start with this model:

    inputs = Input(shape=(784,))
    x = Dense(64, activation='relu')(inputs)
    
    # you want to extract these values
    useful_info = Dense(32, activation='relu', name='useful_info')(x)
    
    # final output. used for loss calculation and optimization
    result = Dense(1, activation='softmax', name='result')(useful_info)
    

    Compile with multiple outputs, set loss as None for extra outputs:

    Give None for outputs that you don't want to use for loss calculation and optimization

    model = Model(inputs=inputs, outputs=[result, useful_info])
    model.compile(optimizer='rmsprop',
                  loss=['categorical_crossentropy', None],
                  metrics=['accuracy'])
    

    Provide only target outputs when training. Skipping extra outputs:

    model.fit(my_inputs, {'result': train_labels}, epochs=.., batch_size=...)
    
    # this also works:
    #model.fit(my_inputs, [train_labels], epochs=.., batch_size=...)
    

    One predict to get them all

    Having one model you can run predict only once to get all outputs you need:

    predicted_labels, useful_info = model.predict(new_x)
    
    0 讨论(0)
  • 2020-12-15 00:44

    In order to achieve this I ended up using the 'Functional API'. You basically create multiple models, using the same layers input and hidden layers but different output layers.

    For example:

    https://keras.io/getting-started/functional-api-guide/

    from keras.layers import Input, Dense
    from keras.models import Model
    
    # This returns a tensor
    inputs = Input(shape=(784,))
    
    # a layer instance is callable on a tensor, and returns a tensor
    x = Dense(64, activation='relu')(inputs)
    x = Dense(64, activation='relu')(x)
    predictions_A = Dense(1, activation='softmax')(x)
    predictions_B = Dense(1, activation='softmax')(x)
    
    # This creates a model that includes
    # the Input layer and three Dense layers
    modelA = Model(inputs=inputs, outputs=predictions_A)
    modelA.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    modelB = Model(inputs=inputs, outputs=predictions_B)
    modelB.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    
    0 讨论(0)
提交回复
热议问题