liveBook · Manning

Sliding window

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

A data-sampling method that moves a fixed-size window across text to create overlapping training examples.

A sliding window moves a fixed-length window across tokenized text to create overlapping input–target pairs for next-token prediction. Each input contains a token sequence, while its target sequence is shifted one position to the right. section 2.6

A typical implementation follows this data flow:

  • Tokenize the text and represent it as integer token IDs. section 2.6
  • Slice an input chunk of max_length tokens and create its target chunk by shifting the slice one position. section 2.6
  • Advance the next window by stride positions; stride 1 creates overlap, while stride equal to the window size prevents overlap. section 2.6
  • Collect the pairs in a PyTorch Dataset and return batches through DataLoader. section 2.6

Shifted context and target

With max_length 4, an input such as [290, 4920, 2241, 287] receives [4920, 2241, 287, 257] as its targets. Moving the window by stride 1 makes the next input begin with the previous input’s second token. section 2.6

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 input blocks become next-token prediction tasks, including the portion beyond the target that is masked during training. section 2.6

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

How deep it goes
Explains token slicing, target shifting, stride behavior, batching, and PyTorch implementation without covering embedding internals.
Examples
Uses The Verdict, token-ID slices, decoded contexts, tensor batches, and stride comparisons.
What it assumes
Assumes tokenized text and integer token IDs while establishing how shifted sequences support next-token prediction.
What you can do after
Builds batched input–target tensors with a tokenizer, PyTorch Dataset, and DataLoader.
How it connects
Connects tokenization, BPE, next-token prediction, input–target pairs, embeddings, and PyTorch data loading.

Also explained in

Compare how 5 books explain sliding window →

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.