on
7 mins to read.
Tensorflow Metrics - Accuracy/AUC
tf.metrics
Tensorflow has many built-in evaluation-related metrics which can be seen here. However, sometimes, Calculation those metrics can be tricky and a bit counter-intuitive. In this post, I will briefly talk about accuracy and AUC measures…
tf.metrics.accuracy
tf.metrics.accuracy
calculates how often predictions matches labels. tf.metrics.accuracy
has many arguments and in the end returns two tensorflow operations: accuracy value and an update operation (whose purpose is to collect samples and build up your statistics). Two running variables are created and placed into the computational graph: total
and count
which are equivalent to number of correctly classified observations and number of observations, respectively.
How to use tf.metrics.accuracy
is pretty straightforward:
However, one needs to be sure to initialize and/or reset the running variables correctly. tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES)
will print all the local variables. However, resetting local variables by running sess.run(tf.local_variables_initializer())
can be a terrible idea because one might accidentally reset other local variables unintentionally. By being explicit about which variables to reset, we can avoid having troubles later with other local variables in our computational graph. Two running variables created for tf.metrics.accuracy
and can be called using scope
argument of tf.get.collection()
.
running_vars_auc
will contain:
Now, we need to specify an operation that will perform the initialization/resetting of those running variables.
Now, we need to this operation in out Tensorflow session in order to initialize/re-initalize/reset local variables of tf.metrics.accuracy
, using:
Full Example
NOTE: One must be careful how to reset variables when processing the data in batches. Arranging the operations while calculating overall accuracy and batch accuracy is different. One needs to reset the running variables to zero before calculating accuracy values of each new batch of data.
tf.metrics.auc
Using tf.metrics.auc
is completely similar. It computes the approximate AUC via a Riemann sum. tf.metrics.auc
has many arguments and in the end returns two tensorflow operations: AUC value and an update operation. Four running variables are created and placed into the computational graph: true_positives
, true_negatives
, false_positives
and false_negatives
.
One must be aware that AUC result of tf.metrics.auc
does not necessarily need to be matched with sklearn’s because Tensorflow AUC is an approximate AUC via a Riemann sum. So, one might expect that the results will be a slightly different.
Full Example
NOTE Tensorflow’s AUC metric supports only binary classification. Its first argument is labels
which is a Tensor whose shape matches predictions and will be cast to bool. Its second argument is is predictions
which is a floating point Tensor of arbitrary shape and whose values are in the range [0, 1]
. That is, each element in labels
states whether the class is “positive” or “negative” for a single observation. It is not a 1-hot encoded vector. Therefore, if you use softmax layer at the end of network, you can slice the predictions tensor to only consider the positive (or negative) class, which will represent the binary class: