auc

paddle.static. auc ( input, label, curve='ROC', num_thresholds=4095, topk=1, slide_steps=1, ins_tag_weight=None ) [source]

Area Under the Curve (AUC) Layer

This implementation computes the AUC according to forward output and label. It is used very widely in binary classification evaluation.

Note: If input label contains values other than 0 and 1, it will be cast to bool. Find the relevant definitions here.

There are two types of possible curves:

  1. ROC: Receiver operating characteristic;

  2. PR: Precision Recall

Parameters
  • input (Tensor) – A floating-point 2D Tensor, values are in the range [0, 1]. Each row is sorted in descending order. This input should be the output of topk. Typically, this Tensor indicates the probability of each label. A Tensor with type float32,float64.

  • label (Tensor) – A 2D int Tensor indicating the label of the training data. The height is batch size and width is always 1. A Tensor with type int32,int64.

  • curve (str, optional) – Curve type, can be ‘ROC’ or ‘PR’. Default ‘ROC’.

  • num_thresholds (int, optional) – The number of thresholds to use when discretizing the roc curve. Default 4095.

  • topk (int, optional) – only topk number of prediction output will be used for auc.

  • slide_steps (int, optional) – when calc batch auc, we can not only use step currently but the previous steps can be used. slide_steps=1 means use the current step, slide_steps=3 means use current step and the previous second steps, slide_steps=0 use all of the steps.

  • ins_tag_weight (Tensor, optional) – A 2D int Tensor indicating the data’s tag weight, 1 means real data, 0 means fake data. Default None, and it will be assigned to a tensor of value 1. A Tensor with type float32,float64.

Returns

A tuple representing the current AUC. Data type is Tensor, supporting float32, float64. The return tuple is auc_out, batch_auc_out, [batch_stat_pos, batch_stat_neg, stat_pos, stat_neg ]

auc_out: the result of the accuracy rate batch_auc_out: the result of the batch accuracy batch_stat_pos: the statistic value for label=1 at the time of batch calculation batch_stat_neg: the statistic value for label=0 at the time of batch calculation stat_pos: the statistic for label=1 at the time of calculation stat_neg: the statistic for label=0 at the time of calculation

Return type

Tensor

Examples

import paddle
import numpy as np
paddle.enable_static()

data = paddle.static.data(name="input", shape=[-1, 32,32], dtype="float32")
label = paddle.static.data(name="label", shape=[-1], dtype="int")
fc_out = paddle.static.nn.fc(x=data, size=2)
predict = paddle.nn.functional.softmax(x=fc_out)
result=paddle.static.auc(input=predict, label=label)

place = paddle.CPUPlace()
exe = paddle.static.Executor(place)

exe.run(paddle.static.default_startup_program())
x = np.random.rand(3,32,32).astype("float32")
y = np.array([1,0,1])
output= exe.run(feed={"input": x,"label": y},
                 fetch_list=[result[0]])
print(output)

#you can learn the usage of ins_tag_weight by the following code.
'''
import paddle
import numpy as np
paddle.enable_static()

data = paddle.static.data(name="input", shape=[-1, 32,32], dtype="float32")
label = paddle.static.data(name="label", shape=[-1], dtype="int")
ins_tag_weight = paddle.static.data(name='ins_tag', shape=[-1,16], lod_level=0, dtype='float64')
fc_out = paddle.static.nn.fc(x=data, size=2)
predict = paddle.nn.functional.softmax(x=fc_out)
result=paddle.static.auc(input=predict, label=label, ins_tag_weight=ins_tag_weight)

place = paddle.CPUPlace()
exe = paddle.static.Executor(place)

exe.run(paddle.static.default_startup_program())
x = np.random.rand(3,32,32).astype("float32")
y = np.array([1,0,1])
z = np.array([1,0,1])
output= exe.run(feed={"input": x,"label": y, "ins_tag_weight":z},
                 fetch_list=[result[0]])
print(output)
'''