# How to compute f1 in tensorflow

I have code that computes the accuracy, but now I would like to compute the f1 score.

accuracy_1 = tf.reduce_mean(tf.cast(tf.equal(
tf.argmax(output_1, axis=-1),
tf.argmax(y_1, axis=-1)), tf.float32), name="accuracy_1")
accuracy_2 = tf.reduce_mean(tf.cast(tf.equal(
tf.argmax(output_2, axis=-1),
tf.argmax(y_2, axis=-1)), tf.float32), name="accuracy_2")


How can I compute f1 equivalent for the above code? I'm finding it difficult as I am very new to tensorflow.

TF Addons computes the F1 score and more generally the FBeta Score

To compute f1_score, first, use this function of python sklearn library to produce confusion matrix, after that, from the confusion matrix generate TP,TN,FP,FN then use them to calculate:

Recall = TP/TP+FN and Precision = TP/TP+FP

and then from the above 2 metrics you can easly calculate:

f1_score = 2 * (precision * recall) / (precision + recall)

OR

you can use another function of the same library here to compute f1_score directly from the generated y_true and y_pred like below:

F1 = f1_score(y_true, y_pred, average='binary')


• Hi, thanks a lot for the reply. I know how to compute f1, but i want it in tensorflow, i basically want to replace the above code mentioned with f1. – William Scott Mar 30 '19 at 9:01
• tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics – Hunar A.Ahmed Mar 30 '19 at 9:09
• i saw that, but it doesnt' seem to work. That's the reason i posted here. – William Scott Mar 30 '19 at 10:18

f1 can be Defined as a custom metric. Keras will evaluate this metric on each batch/epoch as applicable.

import keras.backend as K

def f1_metric(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
recall = true_positives / (possible_positives + K.epsilon())
f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
return f1_val

model.compile(...,metrics=['accuracy', f1_metric])

• Hi, thanks a lot for the reply. I know how to do this in keras, but i want it in tensorflow, i basically want to replace the above code mentioned with f1. – William Scott Mar 30 '19 at 9:01
• TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score – Shamit Verma Mar 30 '19 at 9:08
• i saw that, but it doesnt seem to work. That's the reason i posted here. – William Scott Mar 30 '19 at 10:18

In tf 2.0+:

f1 = 2*(tf.compat.v1.metrics.recall(labels, predictions) * tf.compat.v1.metrics.precision(labels, predictions)) / ( tf.compat.v1.metrics.recall(labels, predictions) + tf.compat.v1.metrics.precision(labels, predictions))


In previous versions you can use the contrib.metrics submodule (deprecated in 1.14):

tf.contrib.metrics.f1_score(labels, predictions)