Implementing Keras metrics F1 for a class

Ashutosh Tiwari
1 min readAug 1, 2020

F1 is an important metrics to monitor training of your model. Keras 2.0 doesn’t provide an F1 metrics for models to be shown with every training epoch.

While there are lot of examples online which help you print out F1, I found it difficult to find one when your are only concerned with F1 score of a particular class in an unbalanced class distribution. Here I’ll try to take an example and show you how to calculate and print F1 scores of a class after every class. For this experiment we will be using an NER dataset from Kaggle (https://www.kaggle.com/abhinavwalia95/entity-annotated-corpus/).

This next step is just transforming data into a form which we can use to train our model and we pickle it into a file.

In next step we will load this file and then after creating a simple model we will try training this model. This gist has the code to print F1 metrics both for a class and overall.

--

--

Ashutosh Tiwari

MS in Computational Data Science, TA @IUB | ex Flipkart, Swiggy, Groupon | NIT. Find me here https://cutt.ly/ASHU