liveBook · Manning

Fine-tuning

As explained in Natural Language Processing in Action, Second Edition

Fine-tuning is the continued training of a pretrained model on a narrower dataset or task so its parameters adapt to that target domain or behavior.

Fine-tuning adapts a pretrained model to a narrower task or domain by continuing training on relevant data. Instead of starting from scratch, it reuses parameters learned during pretraining. For BERT, a common approach is to supply task-specific inputs and outputs and train all parameters end to end; this is generally less expensive than pretraining. section 9.3.2 section 10.2.2

The training data depends on the desired behavior. A classifier can learn from comments paired with labels. A generative model can instead continue learning from a domain-specific text corpus, shifting its generation patterns. Instruction tuning uses examples of tasks paired with expected outputs. section 9.3.4 section 10.2.2

Detecting toxic comments

A BERT classifier can assign several labels to a social media comment, including toxic, obscene, threat, and insult. Start by inspecting the labeled examples, then split the training data into training and validation sets. Format each row as text plus a list of labels, load pretrained bert-base-cased, and train a multi-label classifier. section 9.3.4

Listing 9.35 Loading the pretrained model and fine-tuning
>>> from sklearn.metrics import roc_auc_score
>>> from simpletransformers.classification
 import MultiLabelClassificationModel
>>> model = MultiLabelClassificationModel(
...     model_type, model_name, num_labels=len(labels),
...     args=model_args)
You should probably TRAIN this model on a downstream task
 to be able to use it
for predictions and inference
>>> model.train_model(train_df=train_df)    #1

Load the pretrained multi-label classifier and start training on the prepared comments. section 9.3.4

Evaluate on held-out examples rather than judging the model by its training loss alone. A second run preprocesses comments, evaluates during training, uses early stopping, and reloads the best saved model. Its reported validation metrics improve over the first run, but those scores alone do not establish reliable performance on new social media data. section 9.3.4

Adapting GPT-2 to a text corpus

For generation, load pretrained GPT-2 and its matching tokenizer. Prepare a dataset of text sentences, set aside an evaluation split, and use a language-model data collator and Hugging Face Trainer to continue training. Comparing generations from the original and fine-tuned models provides a practical check on how the corpus changed their output patterns. section 10.2.1 section 10.2.2

Listing 10.12 Fine-tuning GPT-2 with Hugging Face’s Trainer class
>>> from transformers import Trainer
>>> ft_model = GPT2LMHeadModel.from_pretrained("gpt2")    #1

>>> trainer = Trainer(
...        ft_model,
...        training_args,
...        data_collator=collator,           #2
...        train_dataset=train_dataset,    #3
...        eval_dataset=eval_dataset)
>>> trainer.train()

See how the Trainer combines the pretrained model, training arguments, collator, and dataset splits. section 10.2.2

Fine-tuning can be substantially cheaper than pretraining, but large-model training may still exceed local hardware capacity. Compare local hardware costs with pay-per-use compute. Domain adaptation also does not make generated answers inherently factual or unbiased: review outputs and consider grounding them with semantic search or retrieval-augmented generation. section 9.3.3 section 10.0 section 10.3.13

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains end-to-end BERT fine-tuning and walks through dataset preparation, training, evaluation, and a GPT-2 Trainer workflow.
Examples
Uses labeled toxic comments for multi-label classification and a sentence corpus to adapt GPT-2 generation.
What you can do after
Apply it via Simple Transformers and Hugging Face Trainer to train and evaluate pretrained models.
How it connects
Connects fine-tuning with transfer learning, instruction tuning, early stopping, and retrieval-augmented generation.

Also explained in

Compare how 38 books explain fine-tuning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.