liveBook · Manning

Dataset

As explained in Build a Large Language Model (From Scratch)

A collection of examples used for training or experimentation.

A dataset is a collection of examples arranged for model training or experimentation. In a GPT pipeline, it becomes tokenized input–target pairs: each input context contains a token sequence, while its target sequence is shifted one position to represent next-token prediction. section 2.6

Prepare a dataset by turning raw text or labeled records into uniform batches:

  • Tokenize raw text and generate overlapping or non-overlapping windows with a configurable maximum length and stride. section 2.6
  • Store inputs and shifted targets as tensors; a PyTorch Dataset exposes individual pairs through __len__ and __getitem__. section 2.6
  • Balance labeled data when one class dominates, map labels to integers, and split records into training, validation, and test sets. section 6.2
  • Pad variable-length messages to a shared maximum length, then use DataLoader to batch, shuffle, and optionally drop incomplete batches. section 6.3

Next-token and classification batches

With context_size=4, an input such as [290, 4920, 2241, 287] pairs with the target [4920, 2241, 287, 257]. For classification, a batch can contain eight padded messages of 120 token IDs and eight labels. section 2.6 section 6.3

Figure 2.12 Given a text sample, extract input blocks as subsamples that serve as input to the LLM, and the LLM’s prediction task during training is to predict the next word that follows the input block. During training, we mask out all words that are past the target. Note that the text shown in this figure must undergo tokenization before the LLM can process it; however, this figure omits the tokenization step for clarity.
figure

Shows how tokenized input blocks define the next-token prediction targets and mask later positions during training. section 2.6

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains tokenization, windowing, padding, balancing, splitting, Dataset indexing, and DataLoader batching without covering storage systems or distributed pipelines.
Examples
Uses The Verdict token stream, an SMS spam dataset, shifted token arrays, padded message batches, and train–validation–test splits.
What it assumes
Assumes basic tokenization and tensor familiarity while teaching windowing, label preparation, padding, batching, and dataset splitting.
What you can do after
Build it from scratch using tiktoken, PyTorch Dataset, and DataLoader to produce training, validation, and test batches.
How it connects
Connects tokenization, next-token prediction, PyTorch Dataset, DataLoader, padding, class balancing, and fine-tuning.

Also explained in

Compare how 24 books explain dataset →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.