liveBook · Manning

Padding

As explained in Deep Learning with R, Third Edition

Padding is the addition of rows and columns around an input feature map so convolution windows can cover boundary locations.

Padding adds rows and columns around an input feature map, or special tokens to a sequence, so fixed-size computations can include boundaries or run efficiently in batches. section 8.1.1 section 14.5

In convolutional image models, padding addresses border effects:

  • A 3 × 3 window applied without padding to a 5 × 5 feature map has only 9 centered locations, producing a 3 × 3 output. section 8.1.1
  • Adding one row and column on every side lets a 3 × 3 window cover every original tile and preserve spatial dimensions. section 8.1.1
  • Conv2D uses padding = "valid" for no padding and padding = "same" for an output with the input's width and height; "valid" is the default. section 8.1.1
Figure 8.6 Padding a 5 × 5 input in order to be able to extract 25 3 × 3 patches
figure

Shows how padding a 5 × 5 input enables extraction of 25 centered 3 × 3 patches. section 8.1.1

In text classification, padding makes variable-length inputs rectangular:

  • Tokenized sentences receive a special "[PAD]" token, usually at the end, until they reach a chosen sequence length. section 14.5
  • Padding allows batch calculations to be parallelized efficiently on a GPU, but padding tokens must not degrade model predictions. section 14.5
  • IMDb reviews can be truncated or padded to 600 words; the resulting batches have shape (batch_size, sequence_length), with many trailing zeros. section 14.5

Padding IMDb reviews in R/Keras

max_length <- 600 text_vectorization <- layer_text_vectorization( output_mode = "int", output_sequence_length = max_length ) text_vectorization |> adapt(train_ds_no_labels) sequence_train_ds <- train_ds |> dataset_map(\(x, y) tuple(text_vectorization(x), y))

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines boundary and sequence padding with concrete dimensions and API choices, but defers detailed masking and convolution arithmetic.
Examples
Uses 5 × 5 feature maps, 3 × 3 windows, padded IMDb reviews, and an R/Keras TextVectorization pipeline.
What it assumes
Assumes basic feature maps and tokenized sequences while teaching border effects, fixed-length batches, and padding choices concretely.
What you can do after
Apply padding through Conv2D modes or TextVectorization output_sequence_length when constructing image and text pipelines.
How it connects
Connects padding with convolution, strides, tokenization, truncation, batching, GPUs, and TextVectorization.

Also explained in

Compare how 19 books explain padding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.