Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将损失函数指定为Keras中的二次加权kappa?_Python_Keras_Scikit Learn - Fatal编程技术网

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

Python 如何将损失函数指定为Keras中的二次加权kappa?,python,keras,scikit-learn,Python,Keras,Scikit Learn,我的理解是,keras要求损失函数具有签名: def custom_loss(y_true, y_pred): 我试图使用sklearn.metrics.cohen\u kappa\u score,它需要 (y1,y2,标签=无,重量=无,样品重量=无)` 如果我按原样使用: model.compile(loss=metrics.cohen_kappa_score, optimizer='adam', metrics=['accuracy']) 然后将不会设置权重

我的理解是,keras要求损失函数具有签名:

def custom_loss(y_true, y_pred):

我试图使用
sklearn.metrics.cohen\u kappa\u score
,它需要 (y1,y2,标签=无,重量=无,样品重量=无)`

如果我按原样使用:

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

然后将不会设置
权重。我想将其设置为
quadratic
。您可以将其定义为自定义损失,是的,
keras
只接受损失函数中的两个参数。以下是您如何定义您的损失:

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
现在,您可以通过以下方式将此函数传递给您的模型:

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

在Keras中实现参数化自定义损失函数(
cohen_kappa_得分
)分为两个步骤。因为已经实现了满足您需求的功能,所以您不需要自己实现它。然而,根据,
sklearn.metrics.cohen_kappa_score
不支持加权矩阵。 因此,我建议TensorFlow实现cohen_kappa。然而,在Keras中使用TensorFlow并不是那么容易。。。 根据这一点,他们使用
control\u dependencies
在Keras中使用TensorFlow度量。以下是一个例子:

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
由于将
(y\u true,y\u pred)
作为参数,因此需要一个返回另一个函数的包装函数。下面是一些代码:

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
最后,您可以在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'])

关于使用Cohen-Kappa度量作为损失函数。一般来说,可以使用加权kappa作为损失函数。这是一个使用加权kappa作为多类分类损失函数的方法。

sklearn.metrics.cohen_kappa_分数
不是有效的Keras损失,因为它不适用于Kerastensors@rvinas像sklearn.metrics.cohen_kappa_分数这样的东西可以通过(Keras的Scikit学习API)访问吗?这可能会让OP基本上做到这一点way@seisvelas我不这么认为-这些包装器允许在您的Scikit学习工作流中使用Keras模型,但不是相反。我看到的唯一解决方案是使用Keras实现
sklearn.metrics.cohen_kappa_score
operations@seisvelas我将如何实现Keras操作?中的答案可能就是您想要的。修改我的问题以指示
二次加权kappa
。只需插入内部函数中的逻辑,它就会工作正如所料,还有一件事
Kappa
是一个
metric
,为什么要将其用作损失函数?我想最小化Kappa,所以将其用作损失函数是否有意义?您可以使用任何指标作为损失函数,只要它是可微的,感谢您的解决方案,但我遇到了以下问题:ValueError:无法压缩dim[1],预期尺寸为1,输入形状为[?,5]的“损失”\u 7/密集”\u 8\u损失/cohen\u kappa/移除”\u可挤压的\u尺寸/挤压”(op:“挤压”)得到5。有什么想法吗?这个解决方案没有使用二次加权kappa。你找到二次kappa分数的解决方案了吗?我也有同样的问题。还没有,但我会尝试为tensorflow 2制作一个版本,因为它看起来更容易。