Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Kullback-Leibler divergence #308

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/evaluation/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ def get_chrf_meta(task=None):
}


def get_kullback_leibler_divergence_meta(task=None):
return {
"unit": "%",
"pretty_name": "Kullback-Leibler Divergence",
"utility_direction": -1,
"offset": 0,
}


def get_dataperf_fraction_of_fixes(required_fixes, total_fixes):
fraction_of_fixes = required_fixes / total_fixes
return fraction_of_fixes
Expand Down Expand Up @@ -290,6 +299,15 @@ def get_chrf(predictions: list, targets: list):
return chrf.score


def get_kullback_leibler_divergence(y_true, y_pred, epsilon=0.00001):
y = np.clip(y_true, epsilon, 1 - epsilon)
x = np.clip(y_pred, epsilon, 1 - epsilon)

kl = np.sum(y * np.log(y / x), axis=1)

return np.mean(kl)


def get_bleu_meta(task=None):
return {"unit": "", "pretty_name": "BLEU", "utility_direction": 1, "offset": 0}

Expand Down
2 changes: 2 additions & 0 deletions api/evaluation/metrics/metrics_dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dataperf_fraction_of_fixes": metrics.get_dataperf_fraction_of_fixes,
"dataperf_balanced_accuracy": metrics.get_dataperf_balanced_accuracy,
"chrf": metrics.get_chrf,
"kullback_leibler_divergence": metrics.get_kullback_leibler_divergence,
}

delta_metrics_dict = {
Expand Down Expand Up @@ -53,4 +54,5 @@
"dataperf_fraction_of_fixes": metrics.get_dataperf_fraction_of_fixes_meta,
"dataperf_balanced_accuracy": metrics.get_dataperf_balanced_accuracy_meta,
"chrf": metrics.get_chrf_meta,
"kullback_leibler_divergence": metrics.get_kullback_leibler_divergence_meta,
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ def get_chrf_meta(task=None):
}


def get_kullback_leibler_divergence_meta(task=None):
return {
"unit": "%",
"pretty_name": "Kullback-Leibler Divergence",
"utility_direction": -1,
"offset": 0,
}


def get_chrf_pp(predictions: list, targets: list):
"""Chrf++ metric.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get_f1,
get_f1_meta,
get_fairness_meta,
get_kullback_leibler_divergence_meta,
get_macro_f1,
get_macro_f1_meta,
get_matthews_correlation,
Expand Down Expand Up @@ -95,4 +96,5 @@
"dataperf_fraction_of_fixes": get_dataperf_fraction_of_fixes_meta,
"dataperf_balanced_accuracy": get_dataperf_balanced_accuracy_meta,
"chrf": get_chrf_meta,
"kullback_leibler_divergence": get_kullback_leibler_divergence_meta,
}
Loading