Training Examples from Text
Goal
Construct deterministic context windows and shifted targets from a token stream while protecting a train/validation boundary before overlapping examples are created.
Turn one long stream into bounded training examples
The model can learn from a very long text corpus, but each training example must fit inside its configured context window. So the data pipeline turns the long stream into bounded windows. Each window must contain enough source tokens to build both the inputs and their one-step-ahead targets.
A model with block_size = 4 therefore needs five consecutive source tokens to create four next-token training positions:
source: [10,11,12,13,14]
input: [10,11,12,13]
target: [11,12,13,14]
That extra fifth token supplies the final target.
See why overlap can fool evaluation
Suppose the source tokens are:
0 1 2 3 4 5 6 7
With block_size=3, adjacent windows overlap heavily:
0 1 2 → target includes 3
1 2 3 → target includes 4
2 3 4 → target includes 5
If you create all windows first and then randomly place them into train and validation, a training window and validation window may share most of their tokens. The model is then evaluated on examples that are not independent of what it saw during updates.
Instead, split the source stream first. For example, tokens 0..5 might be training and 6..7 validation. Only then should each region produce its own valid windows.
This is the same principle you learned earlier with tabular data leakage, but sequence overlap makes the boundary easier to violate accidentally.
Block size changes how much prefix each example can use
Suppose the token stream is:
A B C D E F
With a short block size, a training example may contain:
A B C D
and shifted targets:
B C D E
A later window can start farther into the stream.
Longer blocks let later positions train with more preceding context, but they also require more memory and attention computation.
This means block_size is not only a file-chunking setting. It defines the maximum training context available to positions inside those examples.