问题描述
我的理解是,keras要求损失函数具有签名:
def custom_loss(y_true, y_pred):
我正在尝试使用sklearn.metrics.cohen_kappa_score
,这需要(y1,y2,标签=无,权重=无,sample_weight =无)`
如果我照原样使用它:
model.compile(loss=metrics.cohen_kappa_score,
optimizer='adam', metrics=['accuracy'])
然后将不会设置weights
.我想将其设置为quadtratic
.有什么要传递的吗?
在Keras中实现参数化的自定义损失函数(cohen_kappa_score
)有两个步骤.由于已经实现了满足您需要的功能,因此您无需自己实现它.但是,根据 TensorFlow文档,sklearn.metrics.cohen_kappa_score
不支持加权矩阵.因此,我建议使用TensorFlow的cohen_kappa实现.但是,在Keras中使用TensorFlow并非易事...根据此问题,他们使用control_dependencies
使用TensorFlow Keras中的指标.这是一个示例:
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
由于 Keras损失函数采用(y_true, y_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作为多类分类的损失函数. >
My understanding is that keras requires loss functions to have the signature:
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)`
If I use it as is:
model.compile(loss=metrics.cohen_kappa_score,
optimizer='adam', metrics=['accuracy'])
Then the weights
won't be set. I want to set that to 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. 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.Therefore, I suggest TensorFlow's implementation of 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. 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. 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:
# 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. In general it is possible to use weighted kappa as a loss function. Here is a paper using weighted kappa as a loss function for multi-class classification.
这篇关于如何在Keras中将损失函数指定为二次加权kappa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!