Make predictions.
(sp_processor, features_map_fn, logits_fn, decode_logits_fn,
split_and_pad_fn, distribute_strategy, dataset)
| 83 | |
| 84 | |
| 85 | def predict(sp_processor, features_map_fn, logits_fn, decode_logits_fn, |
| 86 | split_and_pad_fn, distribute_strategy, dataset): |
| 87 | """Make predictions.""" |
| 88 | predictions = collections.defaultdict(list) |
| 89 | for _, features in dataset.enumerate(): |
| 90 | token_ids = features['token_ids'] |
| 91 | x = split_and_pad_fn(features_map_fn(features)) |
| 92 | logits = tf.concat( |
| 93 | distribute_strategy.experimental_local_results(logits_fn(x)), 0) |
| 94 | logits = logits[:features['token_ids'].shape[0]] |
| 95 | end_limit = token_ids.row_lengths() - 1 # inclusive |
| 96 | begin, end, scores = decode_logits_fn(logits, end_limit) |
| 97 | answers = prediction.decode_answer(features['context'], begin, end, |
| 98 | features['token_offsets'], |
| 99 | end_limit).numpy() |
| 100 | for j, (qid, token_id, offset, score, answer) in enumerate( |
| 101 | zip(features['qid'].numpy(), |
| 102 | tf.gather(features['token_ids'], begin, batch_dims=1).numpy(), |
| 103 | tf.gather(features['token_offsets'], begin, batch_dims=1).numpy(), |
| 104 | scores, answers)): |
| 105 | if not answer: |
| 106 | logging.info('%s: %s | NO_ANSWER, %f', |
| 107 | features['id'][j].numpy().decode('utf-8'), |
| 108 | features['question'][j].numpy().decode('utf-8'), score) |
| 109 | continue |
| 110 | if sp_processor.IdToPiece(int(token_id)).startswith('▁') and offset > 0: |
| 111 | answer = answer[1:] |
| 112 | logging.info('%s: %s | %s, %f', features['id'][j].numpy().decode('utf-8'), |
| 113 | features['question'][j].numpy().decode('utf-8'), |
| 114 | answer.decode('utf-8'), score) |
| 115 | predictions[qid.decode('utf-8')].append((score, answer.decode('utf-8'))) |
| 116 | predictions = { |
| 117 | qid: evaluation.normalize_answer( |
| 118 | sorted(answers, key=operator.itemgetter(0), reverse=True)[0][1]) |
| 119 | for qid, answers in predictions.items() |
| 120 | } |
| 121 | return predictions |
| 122 | |
| 123 | |
| 124 | def main(argv): |
nothing calls this directly
no test coverage detected