如何使用Tensorflow和scikit-learn绘制ROC曲线?

jws*_*ers 6 python roc scikit-learn tensorflow

我试图从tensorflow提供的CIFAR-10示例的修改版本绘制ROC曲线.它现在是2班而不是10班.

网络的输出称为logits并采用以下形式:

[[-2.57313061 2.57966399] [0.04221377 -0.04033273] [-1.42880082 1.43337202] [-2.7692945 2.78173304] [-2.48195744 2.49331546] [2.0941515 -2.10268974] [-3.51670194 3.53267646] [-2.74760485 2.75617766] ...]

首先,这些logits实际代表什么?网络中的最后一层是WX + b形式的"softmax linear".

该模型能够通过调用计算准确性

top_k_op = tf.nn.in_top_k(logits, labels, 1)
Run Code Online (Sandbox Code Playgroud)

然后,一旦图形被初始化:

predictions = sess.run([top_k_op])
predictions_int = np.array(predictions).astype(int)
true_count += np.sum(predictions) 
...
precision = true_count / total_sample_count
Run Code Online (Sandbox Code Playgroud)

这很好用.

但是现在如何从中绘制ROC曲线?

我一直在尝试"sklearn.metrics.roc_curve()"函数(http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve)但我不喜欢不知道该用什么作为我的"y_score"参数.

任何帮助,将不胜感激!

fin*_*fin 1

这里的“y_score”应该是一个数组,对应于每个样本被分类为阳性的概率(如果阳性在 y_true 数组中被标记为 1)

实际上,如果您的网络使用 Softmax 作为最后一层,那么模型应该输出此实例的每个类别的概率。但是您在这里给出的数据不符合这种格式。我检查了示例代码: https: //github.com/tensorflow/tensorflow/blob/r0.10/tensorflow/models/image/cifar10/cifar10.py 似乎使用了名为 softmax_linear 的层,我对此示例知之甚少但我想你应该用逻辑函数之类的东西处理输出,将其转化为概率。

然后只需将其与真实标签“y_true”一起提供给 scikit-learn 函数:

y_score = np.array(output)[:,1]
roc_curve(y_true, y_score)
Run Code Online (Sandbox Code Playgroud)