Upload metrics.py with huggingface_hub
Browse files123345672 unmodified lines10111212131415337 unmodified lines353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445import uuidfrom abc import ABC, abstractmethodfrom collections import Counterfrom dataclasses import dataclass, fieldfrom dataclasses import fieldfrom typing import Any, Dict, Generator, List, Optional
import evaluate2 unmodified lines
from .operator import ( MultiStreamOperator, SequntialOperator, SingleStreamOperator, StreamingOperator, StreamInstanceOperator,337 unmodified lines metric_name = "bleu" main_score = "bleu" scale = 1.0
class CustomF1(GlobalMetric): main_score = "f1_micro"
@abstractmethod def get_element_group(self, element): pass
@abstractmethod def get_element_representation(self, element): pass
def group_elements(self, l): return { k: Counter([self.get_element_representation(value) for value in l if self.get_element_group(value) == k]) for k in set([self.get_element_group(e) for e in l]) }
def calculate_groups_ratio(self, actual_group, total_group): return sum([min(actual_group[k], total_group[k]) for k in actual_group.keys()]), sum(actual_group.values())
def f1(self, pn, pd, rn, rd): precision = 1.0 if pn == 0 and pd == 0 else pn / pd recall = 1.0 if rn == 0 and rd == 0 else rn / rd try: return 2 * precision * recall / (precision + recall) except ZeroDivisionError: return 0.0
def compute(self, references: List[Any], predictions: List[Any]) -> dict: # in case reference are List[List[List[Any]]] and predictions are List[List[Any]]: if isinstance(references[0], list) and isinstance(references[0][0], list): references = [element[0] for element in references]
assert len(references) == len(predictions), ( f"references size ({len(references)})" f" doesn't mach predictions sise ({len(references)})." ) groups_statistics = dict() for references_batch, predictions_batch in zip(references, predictions): grouped_references = self.group_elements(references_batch) grouped_predictions = self.group_elements(predictions_batch) all_groups = set(grouped_references.keys()).union(grouped_predictions.keys()) for group in all_groups: if group not in groups_statistics: groups_statistics[group] = { "precision_numerator": 0, "precision_denominator": 0, "recall_numerator": 0, "recall_denominator": 0, } references_by_group = grouped_references.get(group, Counter([])) predictions_by_group = grouped_predictions.get(group, Counter([])) pn, pd = self.calculate_groups_ratio( actual_group=predictions_by_group, total_group=references_by_group ) rn, rd = self.calculate_groups_ratio( actual_group=references_by_group, total_group=predictions_by_group ) groups_statistics[group]["precision_numerator"] += pn groups_statistics[group]["precision_denominator"] += pd groups_statistics[group]["recall_numerator"] += rn groups_statistics[group]["recall_denominator"] += rd
result = {} pn_total = pd_total = rn_total = rd_total = 0 for group in groups_statistics.keys(): pn, pd, rn, rd = ( groups_statistics[group]["precision_numerator"], groups_statistics[group]["precision_denominator"], groups_statistics[group]["recall_numerator"], groups_statistics[group]["recall_denominator"], ) result[f"f1_{group}"] = self.f1(pn, pd, rn, rd) pn_total, pd_total, rn_total, rd_total = pn_total + pn, pd_total + pd, rn_total + rn, rd_total + rd try: result["f1_macro"] = sum(result.values()) / len(result.keys()) except ZeroDivisionError: result["f1_macro"] = 1.0
result[f"f1_micro"] = self.f1(pn_total, pd_total, rn_total, rd_total) return result
class NER(CustomF1): def get_element_group(self, element): return element[1]
def get_element_representation(self, element): return str(element)metrics.py+92-2