How to make comparisons in a keras custom loss function using input

  keras, loss-function, python, tensorflow

I have a multidimensional input (None, 8, 105)

I need to access the value – i[-1:][0][-1:][0][:1] and make comparisons between y_py_actual, y_predicted and input_tensor

This is more or less what I got, but the function doesn’t work

   def custon_loss(self, input_tensor):
        def loss(y_actual, y_predicted):
            i = input_tensor[0][-1:][0][:1]
            mse = K.mean(K.sum(K.square(y_actual - y_predicted)))
            return K.switch((K.greater(i, y_predicted) & (K.greater(i, y_actual)) | (K.less(i, y_predicted)) & (K.less(i, y_actual))), mse, (mse ** 2))
        return loss

The comparison for a simple function would be like this (just an example)

def custon_loss(self, input_tensor):
    def loss(y_actual, y_predicted):
        i = input_tensor[0][-1:][0][:1]
        mse = K.mean(K.sum(K.square(y_actual - y_predicted)))
        if ((i > y_actual) & (i > y_predicted) | (i < y_actual) & (i < y_predicted)): return mse
        else: return mse ** 2
    return loss

Source: Python Questions

LEAVE A COMMENT