Skip to content Skip to sidebar Skip to footer

Using Sklearn Macro F1-score As A Metric In Tensorflow.keras

I have defined custom metric for tensorflow.keras to compute macro-f1-score after every epoch as follows: from tensorflow import argmax as tf_argmax from sklearn.metric import f1_s

Solution 1:

sklearn is not TensorFlow code - it is always recommended to avoid using arbitrary Python code in TF that gets executed inside TF's execution graph.

TensorFlow addons already has an implementation of the F1 score (tfa.metrics.F1Score), so change your code to use that instead of your custom metric

Make sure you pip install tensorflow-addons first and then

import tensorflow_addons as tfa

model_4.compile(loss = 'categorical_crossentropy',
                optimizer = Adam(lr=init_lr, decay=init_lr / num_epochs),
                metrics = [Recall(name='recall') #, weighted_f1
                           tfa.metrics.F1Score(average='macro')])

Post a Comment for "Using Sklearn Macro F1-score As A Metric In Tensorflow.keras"