6
$\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$

5 Answers 5

3
$\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$
4
$\begingroup$

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

$\endgroup$
1
  • 1
    $\begingroup$ Alas, TF Addons is deprecated and will be End-of-Life'd in May 2024. Keras is supposed to support it but its currently only available in the nightly-builds at this time: tf.keras.metrics.F1Score (link). $\endgroup$
    – Hephaestus
    Commented Apr 22, 2023 at 17:35
1
$\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
  • 3
    $\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$ Commented 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
    Commented Mar 30, 2019 at 9:09
  • 3
    $\begingroup$ i saw that, but it doesnt' seem to work. That's the reason i posted here. $\endgroup$ Commented Mar 30, 2019 at 10:18
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$ Commented Mar 30, 2019 at 9:01
  • $\begingroup$ TF has builtin metric for F1 . tensorflow.org/api_docs/python/tf/contrib/metrics/f1_score $\endgroup$ Commented Mar 30, 2019 at 9:08
  • $\begingroup$ i saw that, but it doesnt seem to work. That's the reason i posted here. $\endgroup$ Commented Mar 30, 2019 at 10:18
0
$\begingroup$

Keras is in early stages of supporting the F1Score and FBetaScore score, but it currently only in nightly-builds. Example from the docs:

metric = tf.keras.metrics.FBetaScore(beta=2.0, threshold=0.5)
y_true = np.array([[1, 1, 1],
                   [1, 0, 0],
                   [1, 1, 0]], np.int32)
y_pred = np.array([[0.2, 0.6, 0.7],
                   [0.2, 0.6, 0.6],
                   [0.6, 0.8, 0.0]], np.float32)
metric.update_state(y_true, y_pred)
result = metric.result()
result.numpy()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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