FailedPreconditionError while using tensorlfow.metrics.recal



我正在尝试使用tensorlfow.metrics.recalltensorlfow.metrics.precision但张量流引发了FailedPreconditionError。请注意,我使用了sess.run(tf.global_variables_initializer())

代码如下:

import numpy as np
import tensorflow as tf
y_true = np.array([[1, 1, 0], [0, 1, 1], [0, 0, 1]])
y_pred = np.array([[1, 0, 0], [1, 0, 1], [0, 0, 1]])
predictions = tf.placeholder(tf.int64, shape=[3, 3])
labels = tf.placeholder(tf.int64, shape=[3, 3])
prec = tf.metrics.precision(labels, predictions)
rec = tf.metrics.recall(labels, predictions)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
p, r = sess.run([prec, rec], feed_dict={predictions: y_pred, labels: y_true})
print("precision: {}, recall: {}".format(p, r))
  • 张量流版本:1.2.1
  • 蟒蛇版本:3.5.2

tf.metrics.precisiontf.metrics.recall内部创建local variable(使用collections=[tf.GraphKeys.LOCAL_VARIABLES]创建的变量(。所以你需要做:sess.run(tf.local_variables_initializer())也是。

最新更新