How to do Cohen Kappa Quadratic Loss in Tensorflow 2.0?

痴心易碎 提交于 2021-02-18 08:16:14

问题


I'm trying to create the loss function according to:

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

But in tensorflow 2.0:

tf.contrib.metrics.cohen_kappa

No longer exists. Is there an alternative?


回答1:


def kappa_loss(y_pred, y_true, y_pow=2, eps=1e-10, N=4, bsize=256, name='kappa'):
"""A continuous differentiable approximation of discrete kappa loss.
    Args:
        y_pred: 2D tensor or array, [batch_size, num_classes]
        y_true: 2D tensor or array,[batch_size, num_classes]
        y_pow: int,  e.g. y_pow=2
        N: typically num_classes of the model
        bsize: batch_size of the training or validation ops
        eps: a float, prevents divide by zero
        name: Optional scope/name for op_scope.
    Returns:
        A tensor with the kappa loss."""

with tf.name_scope(name):
    y_true = tf.cast(y_true,dtype='float')
    repeat_op = tf.cast(tf.tile(tf.reshape(tf.range(0, N), [N, 1]), [1, N]), dtype='float')
    repeat_op_sq = tf.square((repeat_op - tf.transpose(repeat_op)))
    weights = repeat_op_sq / tf.cast((N - 1) ** 2, dtype='float')

    pred_ = y_pred ** y_pow
    try:
        pred_norm = pred_ / (eps + tf.reshape(tf.reduce_sum(pred_, 1), [-1, 1]))
    except Exception:
        pred_norm = pred_ / (eps + tf.reshape(tf.reduce_sum(pred_, 1), [bsize, 1]))

    hist_rater_a = tf.reduce_sum(pred_norm, 0)
    hist_rater_b = tf.reduce_sum(y_true, 0)

    conf_mat = tf.matmul(tf.transpose(pred_norm), y_true)

    nom = tf.reduce_sum(weights * conf_mat)
    denom = tf.reduce_sum(weights * tf.matmul(
        tf.reshape(hist_rater_a, [N, 1]), tf.reshape(hist_rater_b, [1, N])) /
                          tf.cast(bsize, dtype='float'))

    return nom / (denom + eps)

and use

 lossMetric = kappa_loss
 model.compile(optimizer=optimizer, loss=lossMetric, metrics=metricsToWatch)

and cast values to floats beforehand:

tf.cast(nn_x_train.values, dtype='float')

I also used a numpy validation version:

def qwk3(a1, a2, max_rat=3):
    assert(len(a1) == len(a2))
    a1 = np.asarray(a1, dtype=int)
    a2 = np.asarray(a2, dtype=int)

    hist1 = np.zeros((max_rat + 1, ))
    hist2 = np.zeros((max_rat + 1, ))

    o = 0
    for k in range(a1.shape[0]):
        i, j = a1[k], a2[k]
        hist1[i] += 1
        hist2[j] += 1
        o +=  (i - j) * (i - j)

    e = 0
    for i in range(max_rat + 1):
        for j in range(max_rat + 1):
            e += hist1[i] * hist2[j] * (i - j) * (i - j)

    e = e / a1.shape[0]

    return sum(1 - o / e)/len(1 - o / e)

and use

nn_y_valid=tf.cast(nn_y_train.values, dtype='float')
print(qwk3(nn_y_valid, trainPredict))

where nn_x_train and nn_y_train are pandas dataframes



来源:https://stackoverflow.com/questions/58979824/how-to-do-cohen-kappa-quadratic-loss-in-tensorflow-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!