I tried to add some additional measurements to my training code for a CNN by utilising the functions from the tf.metrics
submodule, such as tf.metrics.accuracy(y_labels, y_predicted)
and equivalents for precision or recall. This is done in contrast to most of their tutorials where they suggest the convoluted:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Whereas my implementation replaces this line with:
accuracy = tf.metrics.accuracy(y_labels, y_predicted)
Now, even though I do the sess.run(tf.initialize_all_variables())
within my with tf.Session() as sess:
block, I still get the following error when trying to use tf.metrics.accuracy
function:
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)]]
Most notably, replacing the accuracy = tf.metrics.accuracy(y_labels, y_predicted)
line with accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
fixes the problem, however, I would like to implement other metrics such as precision, recall, etc. without doing it by hand.
TL;DR: Add the following line at the beginning of your session:
sess.run(tf.local_variables_initializer())
The confusion arises from the name of the (as frankyjuang points out) deprecated tf.initialize_all_variables()
function. This function was deprecated in part because it is misnamed: it doesn't actually initialize all variables, and instead it only initializes global (not local) variables.
According to the documentation for the tf.metrics.accuracy()
function (emphasis added):
The
accuracy
function creates two local variables,total
andcount
that are used to compute the frequency with whichpredictions
matcheslabels
.
Therefore you need to add an explicit initialization step for the local variables, which can be done using tf.local_variables_initializer()
, as suggested above.