2016-04-29 97 views
5

我試圖從tensorflow提供的CIFAR-10示例的修改版本繪製ROC曲線。它現在爲2類,而不是10如何用Tensorflow和scikit-learn繪製ROC曲線?

的網絡的輸出被稱爲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) 

然後計算精度,一旦圖形已經被初始化:

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

這工作得很好。

但現在我怎麼能從這個繪製ROC曲線?

我一直在嘗試「sklearn.metrics.roc_curve()」函數(http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve),但我不知道用什麼作爲我的「y_score」參數。

任何幫助,將不勝感激!

+0

請參閱此處[鏈接](http://stackoverflow.com/questions/35811446/classification-accuracy-after-recall-and-precision/37275638#37275638)以獲得計算並繪製ROC曲線的代碼。 –

回答

1

「y_score」這裏應該是對應於每個將被歸類爲陽性樣品的概率的陣列(如果正在您的y_true陣列標記爲1)

實際上,如果網絡使用使用SoftMax作爲最後一層,那麼模型應該輸出這個實例的每個類別的概率。但是你在這裏給出的數據不符合這種格式。我檢查了示例代碼:https://github.com/tensorflow/tensorflow/blob/r0.10/tensorflow/models/image/cifar10/cifar10.py 它似乎使用了一個名爲softmax_linear的圖層,我對此示例知之甚少,但我想您應該使用Logistic Function之類的東西處理輸出以將其轉換爲概率。

然後,只需用你的真實標籤, 'y_true' 喂其傳遞給scikit學習功能:

y_score = np.array(output)[:,1] 
roc_curve(y_true, y_score) 
0
import tensorflow as tf 
tp = [] # the true positive rate list 
fp = [] # the false positive rate list 
total = len(fp) 
writer = tf.train.SummaryWriter("/tmp/tensorboard_roc") 
for idx in range(total): 
    summt = tf.Summary() 
    summt.value.add(tag="roc", simple_value = tp[idx]) 
    writer.add_summary (summt, tp[idx] * 100) #act as global_step 
    writer.flush() 

然後開始tensorboard:

tensorboard --logdir=/tmp/tensorboard_roc 

tensorboard_roc

有關詳細信息和代碼,您可以訪問我的博客:http://blog.csdn.net/mao_feng/article/details/54731098

+0

我在我的模型中使用了這段代碼,但在張量板上我只看到了一條從(0,0)開始的直線。 – Kyrol