20

Here's my code.

import tensorflow as tf

a=tf.Variable(tf.constant([0,1,2],dtype=tf.int32))
b=tf.Variable(tf.constant([1,1,1],dtype=tf.int32))
recall=tf.metrics.recall(b,a)

init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    rec=sess.run(recall)
    print(rec)

I tried to test tf.metrics.precision and got the following error message.

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value recall/true_positives/count
     [[Node: recall/true_positives/count/read = Identity[T=DT_FLOAT, _class=["loc:@recall/true_positives/count"], _device="/job:localhost/replica:0/task:0/gpu:0"](recall/true_positives/count)]]
     [[Node: recall/value/_15 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_73_recall/value", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Baoquan Feng
  • 395
  • 1
  • 2
  • 7
  • Welcome to SO! Please don't add screenshots of code or errors. Please take some time to read [help pages](http://stackoverflow.com/help), especially sections ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour](http://stackoverflow.com/tour) and read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Markus Jun 19 '17 at 07:38
  • @Markus Thank you for the comment. I just re-edit my question. Is it OK? – Baoquan Feng Jun 19 '17 at 08:43
  • That's not just okay, it's really appreciated. ;) A last remark: You should make your question a bit more clear. Maybe adding details about what you have tried to resolve the error will help people knowing the answer to help you. – Markus Jun 19 '17 at 09:51

1 Answers1

45

You also need to initialise the local variables hidden in the tf.metrics.recallmethod.

For example, this piece of code would work:

init_g = tf.global_variables_initializer()
init_l = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run(init_g)
    sess.run(init_l)
pfm
  • 6,210
  • 4
  • 39
  • 44
  • 3
    @npf thanks for the answer, but could you please explain what the local_variables_initializer() does? It seems counterintuitive to me, since all the values needed to calculate recall are computed through the graph. What do the local_variables_initializer() do? – Random Certainty May 12 '18 at 01:14
  • The `local_variables_initializer` will initialize the variables belonging to the `LOCAL_VARIABLES` collection. This is best described in the answers to this question https://stackoverflow.com/q/38910198/4282745 – pfm May 13 '18 at 12:23