An instruction dataset contains explicit instruction–input–output pairs for adapting a pretrained GPT-style language model to follow human requests. It connects supervised examples to chatbot, assistant, and conversational behavior. section 7.0 section 7.2
Figure 7.1 The three main stages of coding an LLM. This chapter focuses on step 9 of stage 3: fine-tuning a pretrained LLM to follow human instructions.
Use the fine-tuning pipeline figure to place instruction-dataset preparation between pretrained-model loading and instruction fine-tuning. section 7.0
Prepare the dataset as a model-ready data flow:
- Download a human-readable JSON file containing 1,100 dictionary entries with instruction, input, and output fields; the input may be empty. section 7.2
- Convert each entry into a prompt format, such as Alpaca’s labeled instruction, optional input, and response structure; Phi-3 provides an alternative style. section 7.2
- Partition the entries into training, validation, and test sets using 85%, 5%, and 10% slices, yielding 935, 55, and 110 examples. section 7.2
- Batch formatted examples with a custom collate function that pads sequences, creates target token IDs, masks padding tokens, truncates to 1,024 tokens, and transfers tensors to the selected device. section 7.10 section 7.4
Worked formatted entry
The instruction “Identify the correct spelling of the following word.” with input “Ocassion” becomes a labeled prompt whose response is “The correct spelling is 'Occasion.'” An entry asking for an antonym can omit the input field. section 7.2
PyTorch data loaders shuffle training batches and preserve validation and test batches for evaluation. The resulting input and target tensors share sequence shapes, while variable prompt lengths produce batches such as 8×61, 8×76, and 8×73. section 7.4
The prepared dataset fine-tunes a pretrained GPT-2 model through a familiar loss-based training loop. Training and validation losses are monitored across two epochs, and generated test responses can be scored with another language model. section 7.6 section 7.10
