3
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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

| improve this answer | |
$\endgroup$
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 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')

finally, the library links consists of a helpful explanation, you should read them carefully.

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

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

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.