简体   繁体   中英

tf.confusion_matrix and InvalidArgumentError

I'm trying to run train.py from, here . It is based on this tutorial . I wanted to find the confusion matrix, and added that after the last line in train.py :

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

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

I'm however getting the following error:

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"]()]]

Why is that? How can I find the confusion matrix?

Thanks.

Explanation

The tensorflow computation graph needs to compute the values for y_true_cls and y_pred_cls in order to compute your confusionMatrix .

To compute y_true_cls and y_pred_cls , the graph defined in the code needs the values for x and y_true placeholders. These values are provided in form of a dictionary when running a session.

After providing these placeholders with values, the tensorflow graph has the requisite input to compute the value of the final confusionMatrix .

Code

I hope that the following code helps.

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

Expected output

If the classifier is working excellently then the output should be a diagonal matrix.

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


PS: Right now, I am not in front of a machine with Tensorflow on it. That's why I can't verify it myself. There might be some mistakes with variable names etc.

The error says that your model needs an input x for it to run as per line 39 of the code you reference :

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

Basically, if you don't give an input, it cannot calculate the predicted values, much less the confusion matrix! You also need the values for y_true as per line 42 at the same place:

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

So do it like this:

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 ] } ) )

[some value] and [some other value] you should probably have, or if not, just generate some random values for testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM