Implement F1-Measure with Masking in TensorFlow – TensorFlow Tutorial

By | August 25, 2020

Accuracy and F1 measure are two important metrics to evaluate the performance of deep learning model. We have introduced how to calcuate the accuracy with masking in tensorflow.

Implement Accuracy with Masking in TensorFlow – TensorFlow Tutorial

In this tutorial, we will introduce how to calculate F1-Measure with masking in tensorflow.

F1-Measure

F1-Measure can be computed as:

F1-Measure formula

How to calculate F1-Measure with masking in TensorFlow?

We will write a tensorflow function to implement it.

Here is an example:

def micro_f1(logits, labels, mask):
        """F1-measure with masking."""
        predicted = tf.round(tf.nn.sigmoid(logits))

        # Use integers to avoid any nasty FP behaviour
        predicted = tf.cast(predicted, dtype=tf.int32)
        labels = tf.cast(labels, dtype=tf.int32)
        mask = tf.cast(mask, dtype=tf.int32)

        # expand the mask so that broadcasting works ([nb_nodes, 1])
        mask = tf.expand_dims(mask, -1)
        
        # Count true positives, true negatives, false positives and false negatives.
        tp = tf.count_nonzero(predicted * labels * mask)
        tn = tf.count_nonzero((predicted - 1) * (labels - 1) * mask)
        fp = tf.count_nonzero(predicted * (labels - 1) * mask)
        fn = tf.count_nonzero((predicted - 1) * labels * mask)

        # Calculate accuracy, precision, recall and F1 score.
        precision = tp / (tp + fp)
        recall = tp / (tp + fn)
        fmeasure = (2 * precision * recall) / (precision + recall)
        fmeasure = tf.cast(fmeasure, tf.float32)
        return fmeasure

Then you can modify this function to make it suitable for you model.

Leave a Reply