Search code examples
pythonpython-3.xtensorflowkerasloss-function

How to use multiple variables as loss for a model with one output in Keras?


I have a Keras model that has a single output, but to calculate the custom loss I want to compare the output of the model with two variables. But the problem is that if I want y_true to have a shape of (?, 2) y_pred will also have a shape of (?, 2) when it should be (?, 1). I could pass one of the variables as an additional input to the model and use the input layer while calculating the loss, but this variable should practically be unknown, as it has to be predicted by the model. To make the purpose clearer, here is an example of something similar to what I want to do:

def loss(y_true1, y_true2, y_pred):
    return K.mean(K.square(y_pred-y_true1) + K.square(y_pred-y_true2), -1)

Solution

  • You can have different shapes with a custom loss, no problem. You just need to have consistent results.

    A few options

    def loss(y_true, y_pred): #where true is shape (batch, 2) and pred is shape (batch, 1)
        return K.mean(K.square(y_true - y_pred), axis=-1)
    
    def loss(y_true, y_pred): #where true is shape (batch, 2) and pred is shape (batch,)
        return K.square(y_pred - y_true[:,0]) + K.square(y_pred - y_true[:,1])
    
    def loss(y_true, y_pred): #where true is shape (batch, 2) and pred is shape (batch,)
        y_pred = K.reshape(y_pred, (-1,1))
        return K.mean(K.square(y_true - y_pred), axis=-1)