-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetric_handling.py
52 lines (36 loc) · 1.91 KB
/
metric_handling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
from datasets import load_metric
def get_metric_compute_fn(tokenizer):
metric = load_metric("sacrebleu")
def compute_metrics(eval_preds):
preds, labels = eval_preds
# In case the model returns more than the prediction logits
if isinstance(preds, tuple):
preds = preds[0]
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
# Replace -100s in the labels as we can't decode them
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
# Some simple post-processing
decoded_preds = [pred.strip() for pred in decoded_preds]
decoded_labels = [[label.strip()] for label in decoded_labels]
result = metric.compute(predictions=decoded_preds, references=decoded_labels)
return {"bleu": result["score"]}
return compute_metrics
def get_wer_metric_compute_fn(tokenizer):
metric = load_metric("wer")
def compute_metrics(eval_preds):
preds, labels = eval_preds
# In case the model returns more than the prediction logits
if isinstance(preds, tuple):
preds = preds[0]
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
# Replace -100s in the labels as we can't decode them
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
# Some simple post-processing
decoded_preds = [tokenizer.sp_model.DecodePieces(pred.strip().split()) for pred in decoded_preds]
decoded_labels = [tokenizer.sp_model.DecodePieces(label.strip().split()) for label in decoded_labels]
result = metric.compute(predictions=decoded_preds, references=decoded_labels)
return {"wer": result}
return compute_metrics