liveBook · Manning

Padding

As explained in Natural Language Processing in Action, Second Edition

Padding is the addition of extra positions around an input sequence so boundary regions can be processed and output dimensions controlled.

Padding adds filler positions to sequences that are too short for a fixed-length convolutional neural network. Together with clipping, it lets an NLP pipeline convert texts of different lengths into equal-size vectors, so patterns can be compared at consistent dimensions. section 7.4.1

Think of padding as extending a short strip of text until it reaches the model’s required length. Convolution and pooling otherwise shorten sequences predictably: a kernel of size K removes K−1 positions before pooling reduces them further. A padding token such as <PAD> fills the unused positions; the token must also exist in the embedding vocabulary. section 7.4.1 section 7.5.1

Pad or clip a sequence

def pad(sequence, pad_value, seq_len): padded = list(sequence)[:seq_len] padded = padded + [pad_value] * (seq_len - len(padded)) return padded This keeps the first seq_len positions and appends the pad value when fewer positions are available. For a CNN configured with seq_len = 40, every token sequence enters the embedding layer with the same length.

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains filler tokens, clipping, sequence-length effects, and implementation, but does not develop padding strategies beyond fixed-length sequences.
Examples
Uses a reusable Python padding function, a ten-word convolution example, and a CNN configured for sequences of length 40.
What it assumes
Assumes token sequences and basic convolution terminology while teaching why variable-length text needs fixed-size processing.
What you can do after
Build it from scratch by clipping sequences, appending a pad token, and sizing the downstream CNN tensors.
How it connects
Connects padding with clipping, tokenization, embeddings, convolution, pooling, kernels, strides, and fixed-length vector representations.

Also explained in

Compare how 19 books explain padding →

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.