简体   繁体   English

如何在Keras中将损失函数指定为二次加权kappa?

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

My understanding is that keras requires loss functions to have the signature: 我的理解是keras需要丢失函数来签名:

def custom_loss(y_true, y_pred):

I am trying to use sklearn.metrics.cohen_kappa_score , which takes (y1, y2, labels=None, weights=None, sample_weight=None)` 我正在尝试使用sklearn.metrics.cohen_kappa_score ,它取(y1,y2,labels = None,weights = None,sample_weight = None)

If I use it as is: 如果我按原样使用它:

model.compile(loss=metrics.cohen_kappa_score,
              optimizer='adam', metrics=['accuracy'])

Then the weights won't be set. 然后不会设置weights I want to set that to quadtratic . 我想:被设置为quadtratic Is there some what to pass this through? 有什么可以通过这个吗?

There are two steps in implementing a parameterized custom loss function ( cohen_kappa_score ) in Keras. cohen_kappa_score中实现参数化自定义丢失函数( cohen_kappa_score )有两个步骤。 Since there are implemented function for your needs, there is no need for you to implement it yourself. 由于已实现满足您需求的功能,因此您无需自行实施。 However, according to TensorFlow Documentation , sklearn.metrics.cohen_kappa_score does not support weighted matrix. 但是,根据TensorFlow文档sklearn.metrics.cohen_kappa_score不支持加权矩阵。 Therefore, I suggest TensorFlow's implementation of cohen_kappa. 因此,我建议TensorFlow实现cohen_kappa。 However, using TensorFlow in Keras is not that easy... According to this Question , they used control_dependencies to use a TensorFlow metric in Keras. 然而,在Keras中使用TensorFlow并不那么容易......根据这个问题 ,他们使用control_dependencies在Keras中使用TensorFlow指标。 Here is a example: 这是一个例子:

import keras.backend as K
def _cohen_kappa(y_true, y_pred, num_classes, weights=None, metrics_collections=None, updates_collections=None, name=None):
   kappa, update_op = tf.contrib.metrics.cohen_kappa(y_true, y_pred, num_classes, weights, metrics_collections, updates_collections, name)
   K.get_session().run(tf.local_variables_initializer())
   with tf.control_dependencies([update_op]):
      kappa = tf.identity(kappa)
   return kappa

Since Keras loss functions take (y_true, y_pred) as parameters, you need a wrapper function that returns another function. 由于(y_true, y_pred) 损失函数(y_true, y_pred)作为参数,因此需要一个返回另一个函数的包装函数。 Here is some code: 这是一些代码:

def cohen_kappa_loss(num_classes, weights=None, metrics_collections=None, updates_collections=None, name=None):
   def cohen_kappa(y_true, y_pred):
      return -_cohen_kappa(y_true, y_pred, num_classes, weights, metrics_collections, updates_collections, name)
   return cohen_kappa

Finally, you can use it as follows in Keras: 最后,你可以在Keras中使用它如下:

# get the loss function and set parameters
model_cohen_kappa = cohen_kappa_loss(num_classes=3,weights=weights)
# compile model
model.compile(loss=model_cohen_kappa,
          optimizer='adam', metrics=['accuracy'])

Regarding using the Cohen-Kappa metric as a loss function. 关于使用Cohen-Kappa度量作为损失函数。 In general it is possible to use weighted kappa as a loss function. 通常,可以使用加权kappa作为损失函数。 Here is a paper using weighted kappa as a loss function for multi-class classification. 这是一篇使用加权kappa作为多类分类的损失函数的论文

You can define it as a custom loss and yes you are right that keras accepts only two arguments in the loss function. 您可以将其定义为自定义丢失,是的,您认为keras在loss函数中只接受两个参数。 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(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM