## How to compute f1 in TensorFlow

4

1

I have a 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.

3

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

2

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)


0

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 and then use them to calculate:

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

And then from the above two metrics, you can easily 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')


2Hi, 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 – 2019-03-30T09:01:16.147

1

tensorflow has a tf.metrics function, see here https://www.tensorflow.org/api_docs/python/tf/metrics

– Hunar – 2019-03-30T09:09:01.663

2i saw that, but it doesnt' seem to work. That's the reason i posted here. – William Scott – 2019-03-30T10:18:16.157

0

F1 score 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 – 2019-03-30T09:01:00.030

TF has builtin metric for F1 . https://www.tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score

– Shamit Verma – 2019-03-30T09:08:13.440

i saw that, but it doesnt seem to work. That's the reason i posted here. – William Scott – 2019-03-30T10:18:21.140