liveBook · Manning

Batching

As explained in Natural Language Processing in Action, Second Edition

Batch learning is a training technique that computes parameter updates from a selected group of training examples at a time.

Batching updates model parameters from a selected group of training examples rather than one example at a time. Each batch produces its own estimate of the error surface, so training can progress without treating any single sample as a complete guide. section 5.3.2 section 5.2.5

Training uses several batch granularities:

  • Individual-example training calculates a gradient from one sample at a time and can wander because each sample provides an incomplete signal. section 5.3.2
  • Full batch learning uses the entire training dataset for an update, while batch learning more generally uses a subset such as 0.1%, 1%, 10%, or 20% of the data. section 5.3.2
  • Mini-batch learning balances noisy individual gradients with larger, more directed updates; batch sizes around 16 to 64 are described as effective for many neural-network problems. section 5.3.2

A PyTorch training loop

A neural network trains by clearing old gradients, running a forward pass on training data, calculating loss, backpropagating the error, and applying the optimizer step. The same forward pass can be organized around batches instead of individual samples. section 5.2.5 section 8.3

>>> from tqdm import tqdm
>>> num_epochs = 200
>>> pbar_epochs = tqdm(range(num_epochs), desc='Epoch:', total=num_epochs, ascii=' =')

>>> for epoch in pbar_epochs:
...      optimizer.zero_grad()    #1
...      outputs = model(X_train)
...      loss_train = loss_func_train(outputs, y_train)     #2
...      loss_train.backward()         #3
...      optimizer.step()           #4

Epoch:: 100%|================================| 200/200 [00:04<00:00, 42.84it/s]
 96.26it/s]

Inspect the executable PyTorch loop that repeats optimization across epochs. section 5.2.5

Batching is not only a speed choice: changing the selected examples changes the estimated error surface. Learning rate and momentum still control how aggressively the optimizer responds to those estimates. section 5.3.2 section 5.2.5

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains batch-size tradeoffs and gradient behavior, then stops short of detailing backpropagation internals.
Examples
Uses ski-slope intuition, percentage-based dataset subsets, and a PyTorch neural-network training loop.
What it assumes
Assumes familiarity with training examples, loss, gradients, optimizers, and neural-network forward passes.
What you can do after
Apply it via PyTorch by organizing forward passes, loss calculations, and optimizer updates around training data.
How it connects
Connects batching to stochastic gradient descent, gradient descent, learning rate, momentum, loss, and backpropagation.

Also explained in

Compare how 15 books explain batching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.