|
| 1 | +import collections |
| 2 | +import time |
| 3 | +import json |
| 4 | +import paddle |
| 5 | +from paddlenlp.metrics.squad import squad_evaluate, compute_prediction |
| 6 | + |
| 7 | + |
| 8 | +def prepare_train_features(examples,tokenizer,doc_stride,max_seq_length): |
| 9 | + # Tokenize our examples with truncation and maybe padding, but keep the overflows using a stride. This results |
| 10 | + # in one example possible giving several features when a context is long, each of those features having a |
| 11 | + # context that overlaps a bit the context of the previous feature. |
| 12 | + contexts = [examples[i]['context'] for i in range(len(examples))] |
| 13 | + questions = [examples[i]['question'] for i in range(len(examples))] |
| 14 | + |
| 15 | + tokenized_examples = tokenizer( |
| 16 | + questions, |
| 17 | + contexts, |
| 18 | + stride=doc_stride, |
| 19 | + max_seq_len=max_seq_length) |
| 20 | + |
| 21 | + # Let's label those examples! |
| 22 | + for i, tokenized_example in enumerate(tokenized_examples): |
| 23 | + # We will label impossible answers with the index of the CLS token. |
| 24 | + input_ids = tokenized_example["input_ids"] |
| 25 | + cls_index = input_ids.index(tokenizer.cls_token_id) |
| 26 | + |
| 27 | + # The offset mappings will give us a map from token to character position in the original context. This will |
| 28 | + # help us compute the start_positions and end_positions. |
| 29 | + offsets = tokenized_example['offset_mapping'] |
| 30 | + |
| 31 | + # Grab the sequence corresponding to that example (to know what is the context and what is the question). |
| 32 | + sequence_ids = tokenized_example['token_type_ids'] |
| 33 | + |
| 34 | + # One example can give several spans, this is the index of the example containing this span of text. |
| 35 | + sample_index = tokenized_example['overflow_to_sample'] |
| 36 | + answers = examples[sample_index]['answers'] |
| 37 | + answer_starts = examples[sample_index]['answer_starts'] |
| 38 | + |
| 39 | + # Start/end character index of the answer in the text. |
| 40 | + start_char = answer_starts[0] |
| 41 | + end_char = start_char + len(answers[0]) |
| 42 | + |
| 43 | + # Start token index of the current span in the text. |
| 44 | + token_start_index = 0 |
| 45 | + while sequence_ids[token_start_index] != 1: |
| 46 | + token_start_index += 1 |
| 47 | + |
| 48 | + # End token index of the current span in the text. |
| 49 | + token_end_index = len(input_ids) - 1 |
| 50 | + while sequence_ids[token_end_index] != 1: |
| 51 | + token_end_index -= 1 |
| 52 | + # Minus one more to reach actual text |
| 53 | + token_end_index -= 1 |
| 54 | + |
| 55 | + # Detect if the answer is out of the span (in which case this feature is labeled with the CLS index). |
| 56 | + if not (offsets[token_start_index][0] <= start_char and |
| 57 | + offsets[token_end_index][1] >= end_char): |
| 58 | + tokenized_examples[i]["start_positions"] = cls_index |
| 59 | + tokenized_examples[i]["end_positions"] = cls_index |
| 60 | + else: |
| 61 | + # Otherwise move the token_start_index and token_end_index to the two ends of the answer. |
| 62 | + # Note: we could go after the last offset if the answer is the last word (edge case). |
| 63 | + while token_start_index < len(offsets) and offsets[ |
| 64 | + token_start_index][0] <= start_char: |
| 65 | + token_start_index += 1 |
| 66 | + tokenized_examples[i]["start_positions"] = token_start_index - 1 |
| 67 | + while offsets[token_end_index][1] >= end_char: |
| 68 | + token_end_index -= 1 |
| 69 | + tokenized_examples[i]["end_positions"] = token_end_index + 1 |
| 70 | + |
| 71 | + return tokenized_examples |
| 72 | + |
| 73 | +def prepare_validation_features(examples,tokenizer,doc_stride,max_seq_length): |
| 74 | + # Tokenize our examples with truncation and maybe padding, but keep the overflows using a stride. This results |
| 75 | + # in one example possible giving several features when a context is long, each of those features having a |
| 76 | + # context that overlaps a bit the context of the previous feature. |
| 77 | + contexts = [examples[i]['context'] for i in range(len(examples))] |
| 78 | + questions = [examples[i]['question'] for i in range(len(examples))] |
| 79 | + |
| 80 | + tokenized_examples = tokenizer( |
| 81 | + questions, |
| 82 | + contexts, |
| 83 | + stride=doc_stride, |
| 84 | + max_seq_len=max_seq_length) |
| 85 | + |
| 86 | + # For validation, there is no need to compute start and end positions |
| 87 | + for i, tokenized_example in enumerate(tokenized_examples): |
| 88 | + # Grab the sequence corresponding to that example (to know what is the context and what is the question). |
| 89 | + sequence_ids = tokenized_example['token_type_ids'] |
| 90 | + |
| 91 | + # One example can give several spans, this is the index of the example containing this span of text. |
| 92 | + sample_index = tokenized_example['overflow_to_sample'] |
| 93 | + tokenized_examples[i]["example_id"] = examples[sample_index]['id'] |
| 94 | + |
| 95 | + # Set to None the offset_mapping that are not part of the context so it's easy to determine if a token |
| 96 | + # position is part of the context or not. |
| 97 | + tokenized_examples[i]["offset_mapping"] = [ |
| 98 | + (o if sequence_ids[k] == 1 else None) |
| 99 | + for k, o in enumerate(tokenized_example["offset_mapping"]) |
| 100 | + ] |
| 101 | + |
| 102 | + return tokenized_examples |
| 103 | + |
0 commit comments