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

fix Bug in BertScore calculation: pred target misalignment #2347

Merged
merged 17 commits into from
Jul 19, 2024
15 changes: 6 additions & 9 deletions src/torchmetrics/functional/text/bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,15 @@ def bert_score(
preds_loader, preds_dataset.max_length, model, device, num_layers, all_layers, idf, verbose, user_forward_fn
)

preds_embeddings = preds_embeddings[preds_loader.dataset.sorting_indices]
target_embeddings = target_embeddings[target_loader.dataset.sorting_indices]
gxy-gxy marked this conversation as resolved.
Show resolved Hide resolved

preds_idf_scale = preds_idf_scale[preds_loader.dataset.sorting_indices]
target_idf_scale = target_idf_scale[target_loader.dataset.sorting_indices]

precision, recall, f1_score = _get_precision_recall_f1(
preds_embeddings, target_embeddings, preds_idf_scale, target_idf_scale
)
# Sort predictions
if len(precision.shape) == 1: # i.e. when all_layers = False
precision = precision[preds_loader.dataset.sorting_indices]
recall = recall[preds_loader.dataset.sorting_indices]
f1_score = f1_score[preds_loader.dataset.sorting_indices]
elif len(precision.shape) == 2: # i.e. when all_layers = True
precision = precision[:, preds_loader.dataset.sorting_indices]
recall = recall[:, preds_loader.dataset.sorting_indices]
f1_score = f1_score[:, preds_loader.dataset.sorting_indices]

if baseline is not None:
precision, recall, f1_score = _rescale_metrics_with_baseline(
Expand Down
Loading