tensorflowmachine-learningkerasdeep-learningloss-function

Can we use multiple loss functions in same layer?


Can we use mulitple loss function in this architecture: I have two different type of loss functions and want to use it on last layer [Output] loss functions :

Can we do that? enter image description here


Solution

  • yes you can... you simply have to repeat 2 times the model output in the model definition. you can also merge your loss in a different way using loss_weights params (default is [1,1] for two losses). Below an example in a dummy regression problem. https://colab.research.google.com/drive/1SVHC6RuHgNNe5Qj6IOtmBD5geAJ-G9-v?usp=sharing

    def rmse(y_true, y_pred):
    
        error = y_true-y_pred
    
        return K.sqrt(K.mean(K.square(error)))
    
    
    X1 = np.random.uniform(0,1, (1000,10))
    X2 = np.random.uniform(0,1, (1000,10))
    y = np.random.uniform(0,1, 1000)
    
    inp1 = Input((10,))
    inp2 = Input((10,))
    x = Concatenate()([inp1,inp2])
    x = Dense(32, activation='relu')(x)
    out = Dense(1)(x)
    
    m = Model([inp1,inp2], [out,out])
    m.compile(loss=[rmse,'mse'], optimizer='adam') # , loss_weights=[0.3, 0.7]
    history = m.fit([X1,X2], [y,y], epochs=10, verbose=2)