liveBook · Manning

Batching

As explained in Build a Text-to-Image Generator (from Scratch)

A batch is a group of training examples processed together in one model update or evaluation step.

Batching groups training examples so a model processes them together during one update or evaluation step. It improves computational efficiency by enabling parallel work on GPUs and CPUs. section 5.3.2 section 6.3.3

A practical batching workflow usually follows these stages:

  • Transform raw examples into tensors or numerical token representations, then choose a batch size and optionally shuffle training data. section 3.2.2 section 5.3.2
  • Group examples into batches; for sequence data, sort examples by length when possible so each batch contains similarly sized inputs. section 2.2.2
  • Pad shorter sequences to the longest sequence in the batch, then create masks so padding does not affect model attention or loss counts. section 2.2.2 section 8.2.2
  • Iterate through each batch, compute the loss, update model parameters, and repeat across epochs. section 6.3.3

Two common batch layouts

A vision pipeline loads Fashion-MNIST images with DataLoader(batch_size=128, shuffle=True). A translation pipeline groups German and English token sequences, pads them, and wraps them in a Batch object containing source masks, shifted targets, and token counts. section 5.3.2 section 2.2.2

def seq_padding(X, padding=PAD):
    L = [len(x) for x in X]
    ML = max(L)
    padded_seq = np.array([np.concatenate([x, 
                   [padding] * (ML - len(x))])
        if len(x) < ML else x for x in X])
    return padded_seq  

Shows a sequence-padding function that finds the batch maximum length and appends padding values to shorter arrays. section 2.2.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains tensor conversion, grouping, shuffling, padding, masks, and batch iteration, but does not develop batch-size optimization theory.
Examples
Uses translation sequences, Fashion-MNIST images, clothing-item diffusion data, and paired image-caption batches.
What it assumes
Assumes basic tensors and model training, while teaching batch construction from token or image datasets.
What you can do after
Operate PyTorch and TorchVision data pipelines that transform, shuffle, pad, mask, and iterate through training batches.
How it connects
Connects batching with padding, attention masks, data augmentation, DataLoader, transformers, diffusion models, and GPU parallel computation.

Also explained in

Compare how 15 books explain batching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.