Upload metrics.py with huggingface_hub
Browse files- metrics.py +34 -18
metrics.py
CHANGED
@@ -156,8 +156,8 @@ class Metric(Artifact):
|
|
156 |
return self._parsed_prediction_type
|
157 |
|
158 |
def get_metric_name(self):
|
159 |
-
if self.
|
160 |
-
return self.
|
161 |
return self.__class__.__name__
|
162 |
|
163 |
def consume_stream(self, stream: Stream):
|
@@ -1606,7 +1606,8 @@ class CustomF1(GlobalMetric):
|
|
1606 |
prediction_type = "Any"
|
1607 |
single_reference_per_prediction = True
|
1608 |
groups = None
|
1609 |
-
zero_division = 0.0
|
|
|
1610 |
|
1611 |
@abstractmethod
|
1612 |
def get_element_group(self, element, additional_input):
|
@@ -1737,6 +1738,35 @@ class CustomF1(GlobalMetric):
|
|
1737 |
num_of_unknown_class_predictions += pd
|
1738 |
|
1739 |
result = f1_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1740 |
try:
|
1741 |
result["f1_macro"] = sum(f1_result.values()) / len(result.keys())
|
1742 |
result["recall_macro"] = sum(recall_result.values()) / len(
|
@@ -1750,20 +1780,6 @@ class CustomF1(GlobalMetric):
|
|
1750 |
result["recall_macro"] = self.zero_division
|
1751 |
result["precision_macro"] = self.zero_division
|
1752 |
|
1753 |
-
amount_of_predictions = pd_total
|
1754 |
-
if amount_of_predictions == 0:
|
1755 |
-
result["in_classes_support"] = 1.0
|
1756 |
-
else:
|
1757 |
-
result["in_classes_support"] = (
|
1758 |
-
1.0 - num_of_unknown_class_predictions / amount_of_predictions
|
1759 |
-
)
|
1760 |
-
result["f1_micro"] = self.f1(pn_total, pd_total, rn_total, rd_total)
|
1761 |
-
result["recall_micro"] = self.recall(pn_total, pd_total, rn_total, rd_total)
|
1762 |
-
result["precision_micro"] = self.precision(
|
1763 |
-
pn_total, pd_total, rn_total, rd_total
|
1764 |
-
)
|
1765 |
-
return result
|
1766 |
-
|
1767 |
|
1768 |
class NER(CustomF1):
|
1769 |
prediction_type = "List[Tuple[str,str]]"
|
@@ -2566,7 +2582,7 @@ class RemoteMetric(SingleStreamOperator, Metric):
|
|
2566 |
) # To avoid unintentional changes to the catalog contents
|
2567 |
metric_pipeline.metric = RemoteMetric(
|
2568 |
main_score=local_inner_metric.main_score,
|
2569 |
-
metric_name=local_inner_metric.
|
2570 |
endpoint=remote_metrics_endpoint,
|
2571 |
)
|
2572 |
return metric_pipeline
|
|
|
156 |
return self._parsed_prediction_type
|
157 |
|
158 |
def get_metric_name(self):
|
159 |
+
if self.__id__ is not None:
|
160 |
+
return self.__id__
|
161 |
return self.__class__.__name__
|
162 |
|
163 |
def consume_stream(self, stream: Stream):
|
|
|
1606 |
prediction_type = "Any"
|
1607 |
single_reference_per_prediction = True
|
1608 |
groups = None
|
1609 |
+
zero_division: float = 0.0
|
1610 |
+
report_per_group_scores: bool = True
|
1611 |
|
1612 |
@abstractmethod
|
1613 |
def get_element_group(self, element, additional_input):
|
|
|
1738 |
num_of_unknown_class_predictions += pd
|
1739 |
|
1740 |
result = f1_result
|
1741 |
+
self.add_macro_scores(f1_result, recall_result, precision_result, result)
|
1742 |
+
self.add_in_class_support_scores(
|
1743 |
+
num_of_unknown_class_predictions, pd_total, result
|
1744 |
+
)
|
1745 |
+
self.add_micro_scores(rd_total, rn_total, pd_total, pn_total, result)
|
1746 |
+
if not self.report_per_group_scores:
|
1747 |
+
for group in groups:
|
1748 |
+
del result[f"f1_{group}"]
|
1749 |
+
return result
|
1750 |
+
|
1751 |
+
def add_micro_scores(self, rd_total, rn_total, pd_total, pn_total, result):
|
1752 |
+
result["f1_micro"] = self.f1(pn_total, pd_total, rn_total, rd_total)
|
1753 |
+
result["recall_micro"] = self.recall(pn_total, pd_total, rn_total, rd_total)
|
1754 |
+
result["precision_micro"] = self.precision(
|
1755 |
+
pn_total, pd_total, rn_total, rd_total
|
1756 |
+
)
|
1757 |
+
|
1758 |
+
def add_in_class_support_scores(
|
1759 |
+
self, num_of_unknown_class_predictions, pd_total, result
|
1760 |
+
):
|
1761 |
+
amount_of_predictions = pd_total
|
1762 |
+
if amount_of_predictions == 0:
|
1763 |
+
result["in_classes_support"] = 1.0
|
1764 |
+
else:
|
1765 |
+
result["in_classes_support"] = (
|
1766 |
+
1.0 - num_of_unknown_class_predictions / amount_of_predictions
|
1767 |
+
)
|
1768 |
+
|
1769 |
+
def add_macro_scores(self, f1_result, recall_result, precision_result, result):
|
1770 |
try:
|
1771 |
result["f1_macro"] = sum(f1_result.values()) / len(result.keys())
|
1772 |
result["recall_macro"] = sum(recall_result.values()) / len(
|
|
|
1780 |
result["recall_macro"] = self.zero_division
|
1781 |
result["precision_macro"] = self.zero_division
|
1782 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1783 |
|
1784 |
class NER(CustomF1):
|
1785 |
prediction_type = "List[Tuple[str,str]]"
|
|
|
2582 |
) # To avoid unintentional changes to the catalog contents
|
2583 |
metric_pipeline.metric = RemoteMetric(
|
2584 |
main_score=local_inner_metric.main_score,
|
2585 |
+
metric_name=local_inner_metric.__id__,
|
2586 |
endpoint=remote_metrics_endpoint,
|
2587 |
)
|
2588 |
return metric_pipeline
|