t5_utils.py 880 Bytes
Newer Older
lintangsutawika's avatar
lintangsutawika committed
1
2
import sklearn.metrics

lintangsutawika's avatar
lintangsutawika committed
3

lintangsutawika's avatar
lintangsutawika committed
4
def mean_3class_f1(predictions, references):  # This is a passthrough function
lintangsutawika's avatar
lintangsutawika committed
5
    string_label = ["entailment", "contradiction", "neutral"]
lintangsutawika's avatar
update  
lintangsutawika committed
6
7
8
    predictions = (
        string_label.index(predictions[0]) if predictions[0] in string_label else 0
    )
lintangsutawika's avatar
lintangsutawika committed
9
10
11
12
    references = string_label.index(references[0])

    return (predictions, references)

lintangsutawika's avatar
lintangsutawika committed
13

lintangsutawika's avatar
lintangsutawika committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def agg_mean_3class_f1(items):
    predictions, references = zip(*items)

    """Computes the unweighted average of the F1 per class."""
    metric_str = "fbeta_score"
    metric_fn_kwargs = {
        "beta": 1,
        "labels": range(3),
        "average": "macro",
    }

    def _fn(predictions, references):
        metric_fn = getattr(sklearn.metrics, metric_str)
        metric_val = metric_fn(references, predictions, **metric_fn_kwargs)
        return metric_val

    return _fn(predictions, references)