ROC curve and AUC for a multiclass and multilabel problem. ValueError: Target scores need to be probabilities for multiclass roc_auc

ROC curve and AUC for a multiclass and multilabel problem. ValueError: Target scores need to be probabilities for multiclass roc_auc

所以,我想计算我的一个代码的 ROC 曲线和 AUC,我有 28 个 classes 并且我的图像可以同时存在多个。例如,一张图片可以同时属于class1、2、3。 我在 y_true 中有一个包含 28 个位置的向量作为标签,并且在 class 的位置上用 1 标记。例如,如果图像在向量的位置 2、3 和 5 中属于 class 2、3 和 5,则它们将被标记为 1 -> [0,0,1,1,0,1, 0,0,0 ,..., 0]

def data_validate(samples, loss, network, f1_class):

x, y_true = samples #x-->valor na matriz, y --> label
x = x.cuda() #to GPU
y_true = y_true.cuda() #to GPU
y_pred = network(x) #aqui executa o forward do model.py dos {batch_size} e retorna o fc
y_pred = torch.sigmoid(y_pred)
erro = loss(y_pred, y_true)
f1_class.acumulate(y_pred.cpu().detach(), y_true.cpu().detach(),th=0.5)
print(y_pred)
for i in range(28):
    auc_score = roc_auc_score(y_true[:][i].cpu().detach(), y_pred.cpu().detach(), multi_class='ovr')

return erro, y_pred.cpu().detach(), y_true.cpu().detach()

但我收到了那个错误 --> ValueError:目标分数需要是 multiclass roc_auc 的概率,即它们应该总和为 1.0 超过 classes

您可以在 PyTorch documentation 中看到 torch.sigmoid() 输出不能保证总和为 1 而不是 类。

>>> a = torch.randn(4)
>>> a
tensor([ 0.9213,  1.0887, -0.8858, -1.7683])
>>> torch.sigmoid(a)
tensor([ 0.7153,  0.7481,  0.2920,  0.1458])

您需要规范化 y_pred 以解决此错误。

可以找到执行此操作的代码 here。你需要这样的东西:

row_sums = torch.sum(y_pred, 1) # normalization 
row_sums = row_sums.repeat(1, num_classes) # expand to same size as out
y_pred = torch.div( y_pred , row_sums ) # these should be histograms

代码更改为:

def data_validate(samples, loss, network, f1_class):
    x, y_true = samples #x-->valor na matriz, y --> label
    x = x.cuda() #to GPU
    y_true = y_true.cuda() #to GPU
    y_pred = network(x) #aqui executa o forward do model.py dos {batch_size} e retorna o fc
    y_pred = torch.sigmoid(y_pred)
    erro = loss(y_pred, y_true)
    f1_class.acumulate(y_pred.cpu().detach(), y_true.cpu().detach(),th=0.5)

    row_sums = torch.sum(y_pred, 1)
    row_sums = row_sums.repeat(28)
    row_sums = row_sums.reshape(y_pred.shape)
    y_pred = torch.div( y_pred , row_sums ) 

    for i in range(len(y_pred)):
        auc_score = roc_auc_score(y_true[:][i].cpu().detach(), y_pred[:][i].cpu().detach(), multi_class='ovr')

    return erro, y_pred.cpu().detach(), y_true.cpu().detach()

就像 Priya 说的那样。