5
$\begingroup$

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.

$\endgroup$

4 Answers 4

3
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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)
$\endgroup$
0
$\begingroup$

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])
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Mar 30, 2019 at 9:01
  • $\begingroup$ TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score $\endgroup$ Mar 30, 2019 at 9:08
  • $\begingroup$ i saw that, but it doesnt seem to work. That's the reason i posted here. $\endgroup$ Mar 30, 2019 at 10:18
0
$\begingroup$

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')

Finally, the library links consist of a helpful explanation. You should read them carefully.

$\endgroup$
3
  • 2
    $\begingroup$ 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. $\endgroup$ Mar 30, 2019 at 9:01
  • 1
    $\begingroup$ tensorflow has a tf.metrics function, see here tensorflow.org/api_docs/python/tf/metrics $\endgroup$
    – Hunar
    Mar 30, 2019 at 9:09
  • 2
    $\begingroup$ i saw that, but it doesnt' seem to work. That's the reason i posted here. $\endgroup$ Mar 30, 2019 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.