Search code examples
tensorflowkerasloss-function

custom loss in keras (tf backend) requiring tensor manipulation


My goal is to predict the coordinates of 2 points: [x1, x2]. (The y coordinates of the 2 points are fixed). In addition of the mean squared error (xtrue - xpred)**2 , I want to minimize the error from the slope: approximately (1/(x2true - x1true) - 1/(x2pred - x1pred))**2.

enter image description here

Here is my implementation which raised the following error:

Shape must be rank 2 but is rank 1 for 'concat_1' (op: 'ConcatV2')
with input shapes: [?,?], [?], [].

--

Custom loss

def combo_mse():
    def combo_loss(y_true, y_pred):
        slope_true = 1/( y_true[:, 1] - y_true[:, 0] )
        combo_true = K.concatenate([y_true, slope_true])

    slope_pred = 1/( y_pred[:, 1] - y_pred[:, 0] )
    combo_pred = K.concatenate([y_pred, slope_pred])

    se = K.square( combo_pred - combo_true)
    loss = K.mean(se, axis=-1)
    return loss
return combo_loss

How can I slice the output tensors y_true and y_pred, run some operations and create a new tensor using K.concatenate() to generate the new custom loss function?


Solution

  • You have to change the shape of slope_true and slope_pred before concatenation. The following code should work. The reason is your slope tensors are one dimensions and your y_true and y_pred tensors are 2-dimensionals. Concatenation operations is allowed between same dimension tensors.

    def combo_mse():
            def combo_loss(y_true, y_pred):
                slope_true = 1/( y_true[:, 1] - y_true[:, 0] )
                slope_true = tf.reshape(slope_true, (-1, 1))
                combo_true = K.concatenate([y_true, slope_true])
    
            slope_pred = 1/( y_pred[:, 1] - y_pred[:, 0] )
            slope_pred = tf.reshape(slope_pred, (-1, 1))
            combo_pred = K.concatenate([y_pred, slope_pred])
    
            mse = K.square( combo_pred - combo_true)
            loss = K.mean(mse, axis=-1)
            return loss
        return combo_loss