问题:
使用tf.metrics.accuracy
评估模型时,抛出异常Attempting to use uninitialized value accuracy/count
解决方案:
第一点:
初始化变量: 由于metrics.accuracy创建了两个局部变量total和count,我们需要调用local_variables_initializer()
来初始化它们.
# y为真实值,y_pre为模型预测值
# accuracy
accuracy = tf.metrics.accuracy(tf.argmax(y, 1), tf.argmax(y_pre, 1))
# 初始化全局变量和局部变量
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(accuracy[1])
第二点:
避免在初始化变量后再定义accuracy :
错误操作:
# 初始化全局变量和局部变量
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
# y为真实值,y_pre为模型预测值
# accuracy
accuracy = tf.metrics.accuracy(tf.argmax(y, 1), tf.argmax(y_pre, 1))
sess.run(accuracy[1])
tf.metrics.accuracy
可以用如下操作代替:
# y为真实值,y_pre为模型预测值
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_pre, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
tf.metrics
详细内容可参考:深入理解TensorFlow中的tf.metrics算子
参考:https://codeday.me/bug/20180901/236600.html
--------------------- 本文来自 1273545169 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/baidu_27643275/article/details/82845550?utm_source=copy