Keras custom loss as a function of multiple outputs

后端 未结 1 477
野性不改
野性不改 2021-01-02 11:04

I built a custom architecture with keras (a convnet). The network has 4 heads, each outputting a tensor of different size. I am trying to write a custom loss function as a f

相关标签:
1条回答
  • 2021-01-02 11:49

    You could try the model.add_loss() function. The idea is to construct your custom loss as a tensor instead of a function, add it to the model, and compile the model without further specifying a loss. See also this implementation of a variational autoencoder where a similar idea is used.

    Example:

    import keras.backend as K
    from keras.layers import Input, Dense
    from keras.models import Model
    from keras.losses import mse
    import numpy as np
    
    # Some random training data
    features = np.random.rand(100,20)
    labels_1 = np.random.rand(100,4)
    labels_2 = np.random.rand(100,1)
    
    # Input layer, one hidden layer
    input_layer = Input((20,))
    dense_1 = Dense(128)(input_layer)
    
    # Two outputs
    output_1 = Dense(4)(dense_1)
    output_2 = Dense(1)(dense_1)
    
    # Two additional 'inputs' for the labels
    label_layer_1 = Input((4,))
    label_layer_2 = Input((1,))
    
    # Instantiate model, pass label layers as inputs
    model = Model(inputs=[input_layer, label_layer_1, label_layer_2], outputs=[output_1, output_2])
    
    # Construct your custom loss as a tensor
    loss = K.mean(mse(output_1, label_layer_1) * mse(output_2, label_layer_2))
    
    # Add loss to model
    model.add_loss(loss)
    
    # Compile without specifying a loss
    model.compile(optimizer='sgd')
    
    dummy = np.zeros((100,))
    model.fit([features, labels_1, labels_2], dummy, epochs=2)
    
    0 讨论(0)
提交回复
热议问题