def predictions(dev_dataset,<EOL>all_results,<EOL>tokenizer,<EOL>max_answer_length=<NUM_LIT:64>,<EOL>null_score_diff_threshold=<NUM_LIT:0.0>,<EOL>n_best_size=<NUM_LIT:10>,<EOL>version_2=False):
_PrelimPrediction = namedtuple('<STR_LIT>',<EOL>['<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>'])<EOL>_NbestPrediction = namedtuple(<EOL>'<STR_LIT>', ['<STR_LIT:text>', '<STR_LIT>', '<STR_LIT>'])<EOL>all_predictions = OrderedDict()<EOL>all_nbest_json = OrderedDict()<EOL>scores_diff_json = OrderedDict()<EOL>for features in dev_dataset:<EOL><INDENT>results = all_results[features[<NUM_LIT:0>].example_id]<EOL>example_qas_id = features[<NUM_LIT:0>].qas_id<EOL>prelim_predictions = []<EOL>score_null = <NUM_LIT>  <EOL>min_null_feature_index = <NUM_LIT:0>  <EOL>null_start_logit = <NUM_LIT:0>  <EOL>null_end_logit = <NUM_LIT:0>  <EOL>for features_id, (result, feature) in enumerate(zip(results, features)):<EOL><INDENT>start_indexes = _get_best_indexes(result.start_logits, n_best_size)<EOL>end_indexes = _get_best_indexes(result.end_logits, n_best_size)<EOL>if version_2:<EOL><INDENT>feature_null_score = result.start_logits[<NUM_LIT:0>] +result.end_logits[<NUM_LIT:0>]<EOL>if feature_null_score < score_null:<EOL><INDENT>score_null = feature_null_score<EOL>min_null_feature_index = features_id<EOL>null_start_logit = result.start_logits[<NUM_LIT:0>]<EOL>null_end_logit = result.end_logits[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>for start_index in start_indexes:<EOL><INDENT>for end_index in end_indexes:<EOL><INDENT>if start_index >= len(feature.tokens):<EOL><INDENT>continue<EOL><DEDENT>if end_index >= len(feature.tokens):<EOL><INDENT>continue<EOL><DEDENT>if start_index not in feature.token_to_orig_map:<EOL><INDENT>continue<EOL><DEDENT>if end_index not in feature.token_to_orig_map:<EOL><INDENT>continue<EOL><DEDENT>if not feature.token_is_max_context.get(start_index, False):<EOL><INDENT>continue<EOL><DEDENT>if end_index < start_index:<EOL><INDENT>continue<EOL><DEDENT>length = end_index - start_index + <NUM_LIT:1><EOL>if length > max_answer_length:<EOL><INDENT>continue<EOL><DEDENT>prelim_predictions.append(<EOL>_PrelimPrediction(<EOL>feature_index=features_id,<EOL>start_index=start_index,<EOL>end_index=end_index,<EOL>start_logit=result.start_logits[start_index],<EOL>end_logit=result.end_logits[end_index]))<EOL><DEDENT><DEDENT><DEDENT>if version_2:<EOL><INDENT>prelim_predictions.append(<EOL>_PrelimPrediction(<EOL>feature_index=min_null_feature_index,<EOL>start_index=<NUM_LIT:0>,<EOL>end_index=<NUM_LIT:0>,<EOL>start_logit=null_start_logit,<EOL>end_logit=null_end_logit))<EOL><DEDENT>prelim_predictions = sorted(<EOL>prelim_predictions,<EOL>key=lambda x: (x.start_logit + x.end_logit),<EOL>reverse=True)<EOL>seen_predictions = {}<EOL>nbest = []<EOL>for pred in prelim_predictions:<EOL><INDENT>if len(nbest) >= n_best_size:<EOL><INDENT>break<EOL><DEDENT>feature = features[pred.feature_index]<EOL>if pred.start_index > <NUM_LIT:0>:  <EOL><INDENT>tok_tokens = feature.tokens[pred.start_index:(<EOL>pred.end_index + <NUM_LIT:1>)]<EOL>orig_doc_start = feature.token_to_orig_map[pred.start_index]<EOL>orig_doc_end = feature.token_to_orig_map[pred.end_index]<EOL>orig_tokens = feature.doc_tokens[orig_doc_start:(<EOL>orig_doc_end + <NUM_LIT:1>)]<EOL>tok_text = '<STR_LIT:U+0020>'.join(tok_tokens)<EOL>tok_text = tok_text.replace('<STR_LIT>', '<STR_LIT>')<EOL>tok_text = tok_text.replace('<STR_LIT>', '<STR_LIT>')<EOL>tok_text = tok_text.strip()<EOL>tok_text = '<STR_LIT:U+0020>'.join(tok_text.split())<EOL>orig_text = '<STR_LIT:U+0020>'.join(orig_tokens)<EOL>final_text = get_final_text(tok_text, orig_text, tokenizer)<EOL>if final_text in seen_predictions:<EOL><INDENT>continue<EOL><DEDENT>seen_predictions[final_text] = True<EOL><DEDENT>else:<EOL><INDENT>final_text = '<STR_LIT>'<EOL>seen_predictions[final_text] = True<EOL><DEDENT>nbest.append(<EOL>_NbestPrediction(<EOL>text=final_text,<EOL>start_logit=pred.start_logit,<EOL>end_logit=pred.end_logit))<EOL><DEDENT>if version_2:<EOL><INDENT>if '<STR_LIT>' not in seen_predictions:<EOL><INDENT>nbest.append(<EOL>_NbestPrediction(<EOL>text='<STR_LIT>',<EOL>start_logit=null_start_logit,<EOL>end_logit=null_end_logit))<EOL><DEDENT><DEDENT>if not nbest:<EOL><INDENT>nbest.append(<EOL>_NbestPrediction(text='<STR_LIT>', start_logit=<NUM_LIT:0.0>, end_logit=<NUM_LIT:0.0>))<EOL><DEDENT>assert len(nbest) >= <NUM_LIT:1><EOL>total_scores = []<EOL>best_non_null_entry = None<EOL>for entry in nbest:<EOL><INDENT>total_scores.append(entry.start_logit + entry.end_logit)<EOL>if not best_non_null_entry:<EOL><INDENT>if entry.text:<EOL><INDENT>best_non_null_entry = entry<EOL><DEDENT><DEDENT><DEDENT>probs = nd.softmax(nd.array(total_scores)).asnumpy()<EOL>nbest_json = []<EOL>for (i, entry) in enumerate(nbest):<EOL><INDENT>output = OrderedDict()<EOL>output['<STR_LIT:text>'] = entry.text<EOL>output['<STR_LIT>'] = float(probs[i])<EOL>output['<STR_LIT>'] = entry.start_logit<EOL>output['<STR_LIT>'] = entry.end_logit<EOL>nbest_json.append(output)<EOL><DEDENT>if not version_2:<EOL><INDENT>all_predictions[example_qas_id] = nbest_json[<NUM_LIT:0>]['<STR_LIT:text>']<EOL><DEDENT>else:<EOL><INDENT>score_diff = score_null - best_non_null_entry.start_logit -best_non_null_entry.end_logit<EOL>scores_diff_json[example_qas_id] = score_diff<EOL>if score_diff > null_score_diff_threshold:<EOL><INDENT>all_predictions[example_qas_id] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>all_predictions[example_qas_id] = best_non_null_entry.text<EOL><DEDENT><DEDENT>all_nbest_json[example_qas_id] = nbest_json<EOL><DEDENT>return all_predictions, all_nbest_json, scores_diff_json<EOL>
Get prediction results

    Parameters
    ----------
    dev_dataset: dataset
        Examples of transform.
    all_results: dict
        A dictionary containing model prediction results.
    tokenizer: callable
        Tokenizer function.
    max_answer_length: int, default 64
        Maximum length of the answer tokens.
    null_score_diff_threshold: float, default 0.0
        If null_score - best_non_null is greater than the threshold predict null.
    n_best_size: int, default 10
        The total number of n-best predictions.
    version_2: bool, default False
        If true, the SQuAD examples contain some that do not have an answer.
    Returns
    -------
    all_predictions: dict
        All final predictions.
    all_nbest_json: dict
        All n-best predictions.
    scores_diff_json: dict
        Record the difference between null score and the score of best non-null.
        when version_2 is True.