DataCollatorForLanguageModeling prepares batches for language-model training and supplies the interface expected by a Hugging Face Trainer pipeline. For causal language modeling, configure it with mlm=False so training uses tokens in left-to-right order; use mlm=True for datasets containing randomly masked tokens, as used with bidirectional models such as BERT. section 10.2.2
A typical fine-tuning setup connects the collator to:
- A tokenizer and tokenized PyTorch training and evaluation datasets. section 10.2.2
- TrainingArguments, which specify batch size, epochs, output location, and checkpoint strategy. section 10.2.2
- Trainer, which receives the collator alongside the model and datasets. section 10.2.2
Causal GPT-2 fine-tuning
collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)\ntrainer = Trainer(ft_model, training_args, data_collator=collator, train_dataset=train_dataset, eval_dataset=eval_dataset)\ntrainer.train() section 10.2.2
Listing 10.11 Defining training arguments for GPT-2 fine-tuning
>>> from nlpia2.constants import DATA_DIR #1 >>> from transformers import TrainingArguments >>> from transformers import DataCollatorForLanguageModeling >>> training_args = TrainingArguments( ... output_dir=DATA_DIR / 'ch10_checkpoints', ... per_device_train_batch_size=5, ... num_train_epochs=5, ... save_strategy='epoch') >>> collator = DataCollatorForLanguageModeling( ... tokenizer=tokenizer, mlm=False) #2
Inspect the complete TrainingArguments and DataCollatorForLanguageModeling configuration used for GPT-2 fine-tuning. section 10.2.2
