Option to name MultiLabelFbeta by beta value

(Austin) #1

I’ve been using MultiLabelFbeta with multiple values of beta: 0.5, 1, 2. For all cases they get the same name, f'{self.average}_fbeta', which can be confusing. Also, it makes it difficult to regex my logs for specific beta values when adding SageMaker metric definitions to track. I was wondering if I could submit the following PR:

class MultiLabelFbeta(LearnerCallback):
    "Computes the fbeta score for multilabel classification"
    # https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html
    _order = -20 
    def __init__(self, learn, beta=2, eps=1e-15, thresh=0.3, sigmoid=True, average="micro", name_by_beta=False):
        self.eps, self.thresh, self.sigmoid, self.average, self.beta2, self.beta, self.name_by_beta = \
            eps, thresh, sigmoid, average, beta**2, beta, name_by_beta

    def on_train_begin(self, **kwargs):
        self.c = self.learn.data.c
        if self.average != "none" and self.name_by_beta: self.learn.recorder.add_metric_names([f'{self.average}_f_{self.beta}'])	
        elif self.average != "none": self.learn.recorder.add_metric_names([f'{self.average}_fbeta'])
        elif self.name_by_beta: self.learn.recorder.add_metric_names([f'f_{self.beta}_{c}' for c in self.learn.data.classes])
        else: self.learn.recorder.add_metric_names([f'fbeta_{c}' for c in self.learn.data.classes])

If the functionality to rename these callback metrics already exists, please let me know. Thanks!



You can just subclass with a new name:

class MultiLabelFbeta1(MultiLabelFbeta): pass
1 Like