How can I specify a loss function to be quadratic weighted kappa in Keras?

后端 未结 2 1185
逝去的感伤
逝去的感伤 2020-12-11 07:43

My understanding is that keras requires loss functions to have the signature:

def custom_loss(y_true, y_pred):

I am trying to use skl

2条回答
  •  悲哀的现实
    2020-12-11 08:12

    You can define it as a custom loss and yes you are right that keras accepts only two arguments in the loss function. Here is how you can define your loss:

    def get_cohen_kappa(weights=None):
        def cohen_kappa_score(y_true, y_pred):
            """
            Define your code here. You can now use `weights` directly
            in this function
            """
            return score
        return cohen_kappa_score
    

    Now you can pass this function to your model as:

    model.compile(loss=get_cohen_kappa_score(weights=weights),
                  optimizer='adam')
    model.fit(...)
    

提交回复
热议问题