使用 tf.metric 模块中的变量时出现 TensorFlow FailedPreconditionError
TensorFlow FailedPreconditionError when using variables from the tf.metric module
我试图通过使用 tf.metrics
子模块中的函数(例如 tf.metrics.accuracy(y_labels, y_predicted)
和精确度或召回率等价物)向我的 CNN 训练代码添加一些额外的测量值。这与他们建议的大多数教程形成鲜明对比:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
而我的实现将此行替换为:
accuracy = tf.metrics.accuracy(y_labels, y_predicted)
现在,即使我在 with tf.Session() as sess:
块中执行了 sess.run(tf.initialize_all_variables())
,我在尝试使用 tf.metrics.accuracy
函数时仍然遇到以下错误:
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value performance/accuracy/count
[[Node: performance/accuracy/count/read = Identity[T=DT_FLOAT, _class=["loc:@performance/accuracy/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](performance/accuracy/count)]]
最值得注意的是,将 accuracy = tf.metrics.accuracy(y_labels, y_predicted)
行替换为 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
可以解决问题,但是,我想实现其他指标,例如精度、召回率等,而无需手动执行。
sess.run(tf.initialize_all_variables())
已弃用。
改用 sess.run(tf.global_variables_initializer())
来解决您的问题。
参考
根据 tf.initialize_all_variables 的文档,
THIS FUNCTION IS DEPRECATED. It will be removed after 2017-03-02. Instructions for updating: Use tf.global_variables_initializer instead.
TL;DR: 在会话开头添加以下行:
sess.run(tf.local_variables_initializer())
混淆来自(如)弃用的tf.initialize_all_variables()
函数的名称。这个函数被弃用的部分原因是它被错误命名:它实际上并没有初始化 all 变量,而是它只初始化 global (不是 local) 变量。
根据 tf.metrics.accuracy()
函数的文档(重点添加):
The accuracy
function creates two local variables, total
and count
that are used to compute the frequency with which predictions
matches labels
.
因此您需要为局部变量添加一个显式初始化步骤,这可以使用 tf.local_variables_initializer()
完成,如上所述。
我试图通过使用 tf.metrics
子模块中的函数(例如 tf.metrics.accuracy(y_labels, y_predicted)
和精确度或召回率等价物)向我的 CNN 训练代码添加一些额外的测量值。这与他们建议的大多数教程形成鲜明对比:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
而我的实现将此行替换为:
accuracy = tf.metrics.accuracy(y_labels, y_predicted)
现在,即使我在 with tf.Session() as sess:
块中执行了 sess.run(tf.initialize_all_variables())
,我在尝试使用 tf.metrics.accuracy
函数时仍然遇到以下错误:
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value performance/accuracy/count
[[Node: performance/accuracy/count/read = Identity[T=DT_FLOAT, _class=["loc:@performance/accuracy/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](performance/accuracy/count)]]
最值得注意的是,将 accuracy = tf.metrics.accuracy(y_labels, y_predicted)
行替换为 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
可以解决问题,但是,我想实现其他指标,例如精度、召回率等,而无需手动执行。
sess.run(tf.initialize_all_variables())
已弃用。
改用 sess.run(tf.global_variables_initializer())
来解决您的问题。
参考
根据 tf.initialize_all_variables 的文档,
THIS FUNCTION IS DEPRECATED. It will be removed after 2017-03-02. Instructions for updating: Use tf.global_variables_initializer instead.
TL;DR: 在会话开头添加以下行:
sess.run(tf.local_variables_initializer())
混淆来自(如tf.initialize_all_variables()
函数的名称。这个函数被弃用的部分原因是它被错误命名:它实际上并没有初始化 all 变量,而是它只初始化 global (不是 local) 变量。
根据 tf.metrics.accuracy()
函数的文档(重点添加):
The
accuracy
function creates two local variables,total
andcount
that are used to compute the frequency with whichpredictions
matcheslabels
.
因此您需要为局部变量添加一个显式初始化步骤,这可以使用 tf.local_variables_initializer()
完成,如上所述。