Machine learning MXNet中的自定义MCC损失函数(马修斯相关系数)

Machine learning MXNet中的自定义MCC损失函数(马修斯相关系数),machine-learning,classification,loss-function,mxnet,Machine Learning,Classification,Loss Function,Mxnet,我想写一个MCC损失函数。我起草了布局,但它必须以参考文献1所示的矩阵操作形式完成(这不是学校硬件)。下面的代码是我要做的工作的伪代码 class MCCLoss(Loss): def __init__(self, weight=None, batch_axis=0, **kwargs): super(MCCLoss, self).__init__(weight, batch_axis, **kwargs) @staticmethod def compu

我想写一个MCC损失函数。我起草了布局,但它必须以参考文献1所示的矩阵操作形式完成(这不是学校硬件)。下面的代码是我要做的工作的伪代码

class MCCLoss(Loss):
    def __init__(self, weight=None, batch_axis=0, **kwargs):
        super(MCCLoss, self).__init__(weight, batch_axis, **kwargs)

    @staticmethod
    def compute_confusion_matrix_values(y_true, y_pred):
        tp = 0
        fp = 0
        tn = 0
        fn = 0

        for i in range(len(y_pred)):
            if y_true[i] == y_pred[i] == 1:
                tp += 1
            if y_pred[i] == 1 and y_true[i] != y_pred[i]:
                fp += 1
            if y_true[i] == y_pred[i] == 0:
                tn += 1
            if y_pred[i] == 0 and y_true[i] != y_pred[i]:
                fn += 1

        return tp, fp, tn, fn

    @staticmethod
    def matthews_corrcoef(F, tp, fp, tn, fn):
        # https://stackoverflow.com/a/56875660/992687
        x = (tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)
        epsilon = np.finfo(np.float64).eps
        return ((tp * tn) - (fp * fn)) / F.sqrt(x + epsilon)

    def hybrid_forward(self, F, y_pred, y_true, sample_weight=None):
        tp, fp, tn, fn = self.compute_confusion_matrix_values(y_true, y_pred)
        loss = 1 - self.matthews_corrcoef(F, tp, fp, tn, fn)
        return loss
我发现了一些非常有用的资源,特别是在下面的参考文献1链接中使用Keras的示例实现

我不确定是否可以在参考文献2中使用MakeLoss来简化整个过程

参考:

  • Keras中MCC(Matthews相关系数实现)的多重分类

  • 使用MakeLoss自定义丢失函数

  • MXNet MCC公制

  • 有没有技术专家能帮我实现这一点?需要一双好手吗

    非常感谢