Custom loss function in Keras based on the input data

后端 未结 2 1959
夕颜
夕颜 2020-12-01 08:51

I am trying to create the custom loss function using Keras. I want to compute the loss function based on the input and predicted the output of the neural network.

I

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 09:17

    You could wrap your custom loss with another function that takes the input tensor as an argument:

    def customloss(x):
        def loss(y_true, y_pred):
            # Use x here as you wish
            err = K.mean(K.square(y_pred - y_true), axis=-1)
            return err
    
        return loss
    

    And then compile your model as follows:

    model.compile('sgd', customloss(x))
    

    where x is your input tensor.

    NOTE: Not tested.

提交回复
热议问题