liveBook · Manning

Batch size

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

The number of training examples processed together in one update or evaluation step.

Batch size is the number of training examples processed together in one update or evaluation step. In language-model pretraining, a batch combines input and next-token target sequences; in classification, it combines padded token sequences with class labels. section 2.6 section 6.3

A PyTorch DataLoader uses batch_size to assemble dataset items into tensors. For GPT training, inputs and targets have shape [batch size, context length], with targets shifted one token after inputs. For classification, inputs have shape [batch size, sequence length], while labels have shape [batch size].

Inspecting an eight-example classification batch

With batch_size=8, iterating over the training loader produces input_batch with shape [8, 120] and target_batch with shape [8]. The first dimension is the batch size; the second records the fixed token length for each message.

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

How deep it goes
Explains tensor shapes and DataLoader options with runnable configurations, but does not analyze optimization or hardware-performance trade-offs.
Examples
Uses batch sizes of 1 and 8, shifted GPT input-target tensors, and classification batches shaped [8, 120] with labels shaped [8].
What it assumes
Assumes basic tokenization and PyTorch Dataset familiarity while demonstrating how datasets yield token sequences, targets, and labels.
What you can do after
Operate/configure PyTorch DataLoader instances with batch_size, shuffle, drop_last, and worker settings, then verify returned tensor dimensions.
How it connects
Connects batch size with PyTorch DataLoader, tokenization, context length, padding, truncation, labels, and sliding-window stride.

Also explained in

Compare how 12 books explain batch size →

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.