簡體   English   中英

tf.confusion_matrix和InvalidArgumentError

[英]tf.confusion_matrix and InvalidArgumentError

我正在嘗試從此處運行train.py 它基於本教程 我想找到混淆矩陣,並將其添加到train.py的最后一行train.py

confusionMatrix = tf.confusion_matrix(labels=y_true_cls,predictions=y_pred_cls)

with session.as_default():
    print confusionMatrix.eval()

但是,出現以下錯誤:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x' with dtype float and shape [?,128,128,3]
     [[Node: x = Placeholder[dtype=DT_FLOAT, shape=[?,128,128,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

這是為什么? 如何找到混淆矩陣?

謝謝。

說明

該tensorflow計算圖形需要計算的值y_true_clsy_pred_cls ,以便計算您的confusionMatrix

要計算y_true_clsy_pred_cls ,代碼中定義的圖形需要xy_true占位符的值。 這些值在運行會話時以字典的形式提供。

在為這些占位符提供值之后,tensorflow圖具有必要的輸入來計算最終confusionMatrix的值。

我希望以下代碼能對您有所幫助。

>>> confusionMatrix = tf.confusion_matrix(labels=y_true_cls,predictions=y_pred_cls)
>>> 
>>> # fetch a chunk of data
>>> batch_size = 100
>>> x_batch, y_batch, _, cls_batch = data.valid.next_batch(batch_size)
>>> 
>>> # make a dictionary to be fed for placeholders `x` and `y_true`
>>> feed_dict_testing = {x: x_batch, y_true: y_batch}
>>> 
>>> # now evaluate by running the session by feeding placeholders
>>> result=session.run(confusionMatrix, feed_dict=feed_dict_testing)
>>> 
>>> print result

預期產量

如果分類器運行良好,則輸出應為對角矩陣。

                  predicted
                  red  blue
originally red  [[ 15,  0],
originally blue  [  0, 15]]


PS:現在,我不在帶有Tensorflow的機器前。 這就是為什么我自己無法驗證的原因。 變量名等可能存在一些錯誤。

該錯誤表明您的模型需要輸入x才能使其按照您引用的代碼的第39行運行:

x = tf.placeholder(tf.float32, shape=[None, img_size,img_size,num_channels], name='x')

基本上,如果您不輸入任何內容,它就無法計算預測值,更不用說混淆矩陣了! 您還需要在同一位置按照第42行的y_true值:

y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')

所以這樣做:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) 
    print( sess.run( confusionMatrix ,
           feed_dict = { x : [some value],
                         y_true: [ some other value ] } ) )

您可能應該具有[一些值]和[一些其他值],或者如果沒有,則只需生成一些隨機值以進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM