TensorFlow:变量初始化中的“尝试使用未初始化的值" [英] TensorFlow: “Attempting to use uninitialized value” in variable initialization

查看:44
本文介绍了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 并收到以下错误消息.

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

推荐答案

你还需要初始化隐藏在tf.metrics.recall方法中的局部变量.

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)

这篇关于TensorFlow:变量初始化中的“尝试使用未初始化的值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆