简体   繁体   English

如何在 tensorflow keras 中实现 AUROC 作为损失函数

[英]How to implement AUROC as loss function in tensorflow keras

I'm trying to build a network with tensorflow and keras, for classification with two classes (success or failure).我正在尝试使用 tensorflow 和 keras 构建一个网络,用于分类两个类(成功或失败)。 I can play around with the size of the data depending on how I handle NaN data, but for this let's say that my complete input dataset is (502, 68).我可以根据我处理 NaN 数据的方式来调整数据的大小,但是为此假设我的完整输入数据集是 (502, 68)。 Most features are continuous, some are binary.大多数特征是连续的,有些是二元的。

The difficulty is that the data is imbalanced (96% Success).困难在于数据不平衡(96% 成功)。

With how unbalanced the data is, overfitting comes quick, and the result that minimizes loss is "just predict everything as a success".随着数据的不平衡,过度拟合很快就会到来,而将损失最小化的结果就是“只预测一切都是成功的”。 I've played around with class weights, but without very convincing results.我玩过班级重量,但没有非常令人信服的结果。

The problem to me is the loss function.我的问题是损失函数。 That's why I would like to use the AUROC as a loss.这就是为什么我想使用 AUROC 作为损失。 The only SO post I've found talking about it is this from 6 years ago which originally made me dismiss the idea.我发现的唯一一篇关于它的 SO 帖子是 6 年前的这篇文章,最初让我驳回了这个想法。 Add AUC as loss function for keras "Well, AUROC isn't differentiable, let's drop this idea". 添加 AUC 作为 keras 的损失函数“好吧,AUROC 不可微,让我们放弃这个想法”。

Since then, I have found some more recent algorithm, most notable roc-star in Pytorch.从那以后,我发现了一些更新的算法,最著名的是 Pytorch 中的 roc-star。 I would like to apply it as a custom loss function.我想将其用作自定义损失函数。 However, keras takes as custom loss a function that takes y_true and y_pred and returns a value.但是,keras 将一个接受 y_true 和 y_pred 并返回一个值的函数作为自定义损失。 The roc-star algorithm takes the gradient and values at the previous iteration as input. roc-star 算法将上一次迭代的梯度和值作为输入。 Do you know a way around this ?你知道解决这个问题的方法吗?

I'm using a simple network created with keras.models.Sequential.我正在使用用 keras.models.Sequential 创建的简单网络。

So my question is at several layers, feel free to respond to any of them while ignoring the others =p所以我的问题有好几个层次,请随意回答其中任何一个,而忽略其他的=p

  1. Does anyone know any other, simple way to use AUROC as a loss function ?有谁知道使用 AUROC 作为损失函数的其他简单方法?
  2. Am I too fixated on the AUROC ?我是不是太执着于 AUROC 了? I guess I could make a simpler, and more easily differentiable function, based on the confusion matrix that could work as well.我想我可以基于可以工作的混淆矩阵来制作一个更简单、更容易区分的函数。
  3. How can I implement the roc-star algorithm to the custom loss function ?如何将 roc-star 算法实现到自定义损失函数?

Edit: I realized that I did not provide a link to the roc-star algorithm: https://github.com/iridiumblue/roc-star编辑:我意识到我没有提供 roc-star 算法的链接: https ://github.com/iridiumblue/roc-star

I'm attempting to use it in my tensorflow model atm.我正在尝试在我的张量流模型 atm 中使用它。 It's going, subpar.会的,差强人意。 The dataset I'm using is complex and hard to predict, and EDA and other models (ie pca and decision tree), are resulting similar prediction %s.我使用的数据集复杂且难以预测,而 EDA 和其他模型(即 pca 和决策树)得出的预测结果相似 %s。 Nonetheless, here's what I've done to implement it.尽管如此,这就是我为实现它所做的工作。 By advised that tf keras and kerasregressor are different and have different documentation.被告知 tf keras 和 kerasregressor 是不同的并且有不同的文档。 But they work similarly and can basically do the same.但是它们的工作方式相似,并且基本上可以做同样的事情。

def auroc(yTrainSet, yValidationSet): return tf.py_function(roc_auc_score, (yTrainSet, yValidationSet), tf.double) def auroc(yTrainSet, yValidationSet): return tf.py_function(roc_auc_score, (yTrainSet, yValidationSet), tf.double)

import itertools
import tensorflow as tf
import pandas as pd
from tensorflow import keras
from tensorflow.keras import models
from tensorflow.keras import datasets
from tensorflow.keras import layers
from sklearn.metrics import roc_auc_score
# 1- Instantiate Model
ourModel = keras.Sequential()
# 2- Specify Shape of First Layer
ourModel.add(layers.Dense(512, activation = 'relu', input_shape = ourInputShape))
# 3- Add the layers
ourModel.add(layers.Dense(3, activation= 'softmax')) #softmax returns array of probability scores (num prior), and in this case we have to predict either CSCANCEL, MEMBERCANCEL, ACTIVE)

ourModel.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy', auroc]) #auroc used here

Once this model is made like this you just compile/run it per normal, etc. I've found very little difference in overall performance with this implementation, but figured I'd share nonetheless.一旦这个模型是这样制作的,你只需按照正常的方式编译/运行它,等等。我发现这个实现的整体性能差别很小,但我还是想分享一下。 Best of luck to you祝你好运

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

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