|
|
from transformers import Trainer, is_torch_tpu_available |
|
|
from transformers.trainer_utils import PredictionOutput |
|
|
|
|
|
class QuestionAnsweringTrainer(Trainer): |
|
|
def __init__(self, *args, post_process_function = None, **kwargs): |
|
|
super().__init__(*args, **kwargs) |
|
|
self.post_process_function = post_process_function |
|
|
|
|
|
def evaluate(self, eval_dataset = None, ignore_keys = None, metric_key_prefix: str = "eval"): |
|
|
eval_dataset = self.eval_dataset if eval_dataset is None else eval_dataset |
|
|
eval_dataloader = self.get_eval_dataloader(eval_dataset) |
|
|
|
|
|
|
|
|
compute_metrics = self.compute_metrics |
|
|
self.compute_metrics = None |
|
|
eval_loop = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop |
|
|
try: |
|
|
output = eval_loop( |
|
|
eval_dataloader, |
|
|
description="Evaluation", |
|
|
|
|
|
|
|
|
prediction_loss_only=True if compute_metrics is None else None, |
|
|
ignore_keys=ignore_keys, |
|
|
) |
|
|
finally: |
|
|
self.compute_metrics = compute_metrics |
|
|
|
|
|
if self.post_process_function is not None and self.compute_metrics is not None: |
|
|
eval_preds = self.post_process_function(eval_dataset, self.tokenizer, output.predictions) |
|
|
metrics = self.compute_metrics(eval_preds) |
|
|
|
|
|
|
|
|
for key in list(metrics.keys()): |
|
|
if not key.startswith(f"{metric_key_prefix}_"): |
|
|
metrics[f"{metric_key_prefix}_{key}"] = metrics.pop(key) |
|
|
|
|
|
self.log(metrics) |
|
|
else: |
|
|
metrics = {} |
|
|
|
|
|
self.control = self.callback_handler.on_evaluate(self.args, self.state, self.control, metrics) |
|
|
return metrics |
|
|
|
|
|
def predict(self, predict_dataset, ignore_keys=None, metric_key_prefix: str = "test"): |
|
|
predict_dataloader = self.get_test_dataloader(predict_dataset) |
|
|
|
|
|
|
|
|
compute_metrics = self.compute_metrics |
|
|
self.compute_metrics = None |
|
|
eval_loop = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop |
|
|
try: |
|
|
output = eval_loop( |
|
|
predict_dataloader, |
|
|
description="Prediction", |
|
|
|
|
|
|
|
|
prediction_loss_only=True if compute_metrics is None else None, |
|
|
ignore_keys=ignore_keys, |
|
|
) |
|
|
finally: |
|
|
self.compute_metrics = compute_metrics |
|
|
|
|
|
if self.post_process_function is None or self.compute_metrics is None: |
|
|
return output |
|
|
|
|
|
predictions = self.post_process_function(predict_dataset, self.tokenizer, output.predictions, "predict") |
|
|
metrics = self.compute_metrics(predictions) |
|
|
|
|
|
|
|
|
for key in list(metrics.keys()): |
|
|
if not key.startswith(f"{metric_key_prefix}_"): |
|
|
metrics[f"{metric_key_prefix}_{key}"] = metrics.pop(key) |
|
|
|
|
|
return PredictionOutput(predictions=predictions.predictions, label_ids=predictions.label_ids, metrics=metrics) |