繁体   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