Create `TrainingInstance`s from raw text.
(
input_files,
tokenizer,
processor_text_fn,
max_seq_length,
dupe_factor,
short_seq_prob,
masked_lm_prob,
max_predictions_per_seq,
rng,
do_whole_word_mask=False,
max_ngram_size=None,
)
| 218 | |
| 219 | |
| 220 | def create_training_instances( |
| 221 | input_files, |
| 222 | tokenizer, |
| 223 | processor_text_fn, |
| 224 | max_seq_length, |
| 225 | dupe_factor, |
| 226 | short_seq_prob, |
| 227 | masked_lm_prob, |
| 228 | max_predictions_per_seq, |
| 229 | rng, |
| 230 | do_whole_word_mask=False, |
| 231 | max_ngram_size=None, |
| 232 | ): |
| 233 | """Create `TrainingInstance`s from raw text.""" |
| 234 | all_documents = [[]] |
| 235 | |
| 236 | # Input file format: |
| 237 | # (1) One sentence per line. These should ideally be actual sentences, not |
| 238 | # entire paragraphs or arbitrary spans of text. (Because we use the |
| 239 | # sentence boundaries for the "next sentence prediction" task). |
| 240 | # (2) Blank lines between documents. Document boundaries are needed so |
| 241 | # that the "next sentence prediction" task doesn't span between documents. |
| 242 | for input_file in input_files: |
| 243 | with tf.io.gfile.GFile(input_file, "rb") as reader: |
| 244 | for line in reader: |
| 245 | line = processor_text_fn(line) |
| 246 | |
| 247 | # Empty lines are used as document delimiters |
| 248 | if not line: |
| 249 | all_documents.append([]) |
| 250 | tokens = tokenizer.tokenize(line) |
| 251 | if tokens: |
| 252 | all_documents[-1].append(tokens) |
| 253 | |
| 254 | # Remove empty documents |
| 255 | all_documents = [x for x in all_documents if x] |
| 256 | rng.shuffle(all_documents) |
| 257 | |
| 258 | vocab_words = list(tokenizer.vocab.keys()) |
| 259 | instances = [] |
| 260 | for _ in range(dupe_factor): |
| 261 | for document_index in range(len(all_documents)): |
| 262 | instances.extend( |
| 263 | create_instances_from_document( |
| 264 | all_documents, document_index, max_seq_length, short_seq_prob, |
| 265 | masked_lm_prob, max_predictions_per_seq, vocab_words, rng, |
| 266 | do_whole_word_mask, max_ngram_size)) |
| 267 | |
| 268 | rng.shuffle(instances) |
| 269 | return instances |
| 270 | |
| 271 | |
| 272 | def create_instances_from_document( |
no test coverage detected