Подтвердить что ты не робот

TensorFlow: "Попытка использовать неинициализированное значение" в переменной инициализации

Вот мой код.

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)

Я попытался проверить tf.metrics.precision и получил следующее сообщение об ошибке.

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

Ответ 1

Вам также необходимо инициализировать локальные переменные, скрытые в методе tf.metrics.recall.

Например, этот кусок кода будет работать:

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)