我一直在尝试在TensorFlow中使用recall_at_thresholds函数。我正在做的步骤如下:
import tensorflow as tf
a = tf.constant([[1.0, 0.0], [0.0, 1.0], [0.0, 1.0])
b = tf.constant([[0.1, 0.9], [0.1, 0.9], [0.3, 0.7])
c = tf.metrics.recall_at_thresholds(a,b,[0.5])
with sess as tf.Session():
print(sess.run(c))
然而,在运行时,我得到以下内容:
2017-11-08 16:14:03.914462: W tensorflow/core/framework/op_kernel.cc:1158] Failed precondition: Attempting to use uninitialized value recall_at_thresholds/true_positives
[[Node: recall_at_thresholds/true_positives/read = Identity[T=DT_FLOAT, _class=["loc:@recall_at_thresholds/true_positives"], _device="/job:localhost/replica:0/task:0/gpu:0"](recall_at_thresholds/true_positives)]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value recall_at_thresholds/true_positives
[[Node: recall_at_thresholds/true_positives/read = Identity[T=DT_FLOAT, _class=["loc:@recall_at_thresholds/true_positives"], _device="/job:localhost/replica:0/task:0/gpu:0"](recall_at_thresholds/true_positives)]]
[[Node: recall_at_thresholds/Cast/_1 = _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_33_recall_at_thresholds/Cast", tensor_type=DT_BOOL, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'recall_at_thresholds/true_positives/read', defined at:
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/metrics_impl.py", line 2059, in recall_at_thresholds
labels, predictions, thresholds, weights, includes=('tp', 'fn'))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/metrics_impl.py", line 514, in _confusion_matrix_at_thresholds
true_p = _create_local('true_positives', shape=[num_thresholds])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/metrics_impl.py", line 196, in _create_local
validate_shape=validate_shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1679, in variable
caching_device=caching_device, name=name, dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 200, in __init__
expected_shape=expected_shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 319, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1303, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
self._traceback = _extract_stack()
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value recall_at_thresholds/true_positives
[[Node: recall_at_thresholds/true_positives/read = Identity[T=DT_FLOAT, _class=["loc:@recall_at_thresholds/true_positives"], _device="/job:localhost/replica:0/task:0/gpu:0"](recall_at_thresholds/true_positives)]]
[[Node: recall_at_thresholds/Cast/_1 = _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_33_recall_at_thresholds/Cast", tensor_type=DT_BOOL, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]`
我对Tensorflow相对较新,错误信息对我来说有点吓人。谁能让我知道我在这里做错了什么?
答案 0 :(得分:1)
这是一个有效的例子:
ps -e|grep cups
2552 ? 00:00:00 cupsd
2559 ? 00:00:00 cups-browsed
问题是import tensorflow as tf
a = tf.constant([[1.0, 0.0], [0.0, 1.0], [0.0, 1.0]])
b = tf.constant([[0.1, 0.9], [0.1, 0.9], [0.3, 0.7]])
c = tf.metrics.recall_at_thresholds(a, b, [0.5])
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
print(sess.run(c))
创建了http://192.168.1.120:631中所述的局部变量。需要初始化TensorFlow变量。 recall_at_thresholds
行执行此操作。