tf.confusion_matrix 和 InvalidArgumentError

tf.confusion_matrix and InvalidArgumentError

我正在尝试 运行 train.py 来自 here. It is based on this tutorial。我想找到混淆矩阵,并在 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 占位符的值。当 运行 个会话时,这些值以字典的形式提供。

在为这些占位符提供值后,张量流图具有计算最终 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 的机器前。这就是为什么我不能自己验证它。变量名等可能有一些错误

根据 the code you reference 的第 39 行:

,该错误表明您的模型需要输入 x 才能 运行
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 ] } ) )

[一些值] 和 [一些其他值] 你可能应该有,或者如果没有,只是生成一些随机值用于测试。