Postprocess model output, to form predicton results.
(all_examples,
all_features,
all_results,
n_best_size,
max_answer_length,
do_lower_case,
version_2_with_negative=False,
null_score_diff_threshold=0.0,
xlnet_format=False,
verbose=False)
| 583 | |
| 584 | |
| 585 | def postprocess_output(all_examples, |
| 586 | all_features, |
| 587 | all_results, |
| 588 | n_best_size, |
| 589 | max_answer_length, |
| 590 | do_lower_case, |
| 591 | version_2_with_negative=False, |
| 592 | null_score_diff_threshold=0.0, |
| 593 | xlnet_format=False, |
| 594 | verbose=False): |
| 595 | """Postprocess model output, to form predicton results.""" |
| 596 | |
| 597 | example_index_to_features = collections.defaultdict(list) |
| 598 | for feature in all_features: |
| 599 | example_index_to_features[feature.example_index].append(feature) |
| 600 | unique_id_to_result = {} |
| 601 | for result in all_results: |
| 602 | unique_id_to_result[result.unique_id] = result |
| 603 | |
| 604 | _PrelimPrediction = collections.namedtuple( # pylint: disable=invalid-name |
| 605 | "PrelimPrediction", |
| 606 | ["feature_index", "start_index", "end_index", "start_logit", "end_logit"]) |
| 607 | |
| 608 | all_predictions = collections.OrderedDict() |
| 609 | all_nbest_json = collections.OrderedDict() |
| 610 | scores_diff_json = collections.OrderedDict() |
| 611 | |
| 612 | for (example_index, example) in enumerate(all_examples): |
| 613 | features = example_index_to_features[example_index] |
| 614 | |
| 615 | prelim_predictions = [] |
| 616 | # keep track of the minimum score of null start+end of position 0 |
| 617 | score_null = 1000000 # large and positive |
| 618 | min_null_feature_index = 0 # the paragraph slice with min mull score |
| 619 | null_start_logit = 0 # the start logit at the slice with min null score |
| 620 | null_end_logit = 0 # the end logit at the slice with min null score |
| 621 | for (feature_index, feature) in enumerate(features): |
| 622 | if feature.unique_id not in unique_id_to_result: |
| 623 | logging.info("Skip eval example %s, not in pred.", feature.unique_id) |
| 624 | continue |
| 625 | result = unique_id_to_result[feature.unique_id] |
| 626 | |
| 627 | # if we could have irrelevant answers, get the min score of irrelevant |
| 628 | if version_2_with_negative: |
| 629 | if xlnet_format: |
| 630 | feature_null_score = result.class_logits |
| 631 | else: |
| 632 | feature_null_score = result.start_logits[0] + result.end_logits[0] |
| 633 | if feature_null_score < score_null: |
| 634 | score_null = feature_null_score |
| 635 | min_null_feature_index = feature_index |
| 636 | null_start_logit = result.start_logits[0] |
| 637 | null_end_logit = result.end_logits[0] |
| 638 | for (start_index, start_logit, |
| 639 | end_index, end_logit) in _get_best_indexes_and_logits( |
| 640 | result=result, |
| 641 | n_best_size=n_best_size, |
| 642 | xlnet_format=xlnet_format): |
no test coverage detected