liveBook · Manning

Padding

As explained in Learn Generative AI with PyTorch

A setting or operation that adds, fills, or ignores boundary or placeholder positions in tensor or sequence data.

Padding adds boundary values or placeholder positions so an operation can handle edges and variable-length data. In convolutional layers, zero padding adds rows of zeros around an image; in sequence batches, padding appends zeros to shorter token sequences. section 4.2.1 section 10.1.2

For images, padding changes how a filter reaches the border and helps control feature-map dimensions. With a 2 × 2 kernel, stride 2, and padding=1, a 3 × 3 input becomes a 5 × 5 padded image before convolution. For text, padding makes every sequence in a batch equally long, while masks prevent padded positions from affecting the Transformer. section 4.2.2 section 10.1.2

A practical padding workflow includes:

  • Choose the padding amount for a convolution; padding=0 leaves the input unchanged, while padding=1 adds one zero row around every side. section 4.2.1 section 4.2.2
  • Pad each sequence to the longest sequence in its batch, appending zeros to shorter numerical representations. section 10.1.2
  • Sort sequences by length before batching to reduce the amount of padding and accelerate training. section 10.1.2

A padded convolution

Using the image [[1,1,1],[0,1,2],[8,7,6]], weights [[1,2],[3,4]], stride=2, and padding=1 produces the output [[4,7],[32,50]]. The top-left value uses the padded region [[0,0],[0,1]], so only the input value 1 contributes to that corner calculation. section 4.2.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains convolution and sequence padding through parameterized examples, code, tensor shapes, masks, and calculations.
Examples
Uses a 3 × 3 image, a 2 × 2 filter, padded convolution outputs, token batches, and NumPy sequence-padding code.
What it assumes
Assumes basic tensors, convolution arguments, token sequences, batching, and attention, while defining padding through concrete examples.
What you can do after
Apply padding with PyTorch Conv2d and NumPy batch preparation, then configure masks that ignore padded sequence positions.
How it connects
Connects padding with convolution, stride, feature maps, batching, attention masks, and Transformer encoder-decoder inputs.

Also explained in

Compare how 19 books explain padding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.