Click4Ai

117.

Easy

Padding Strategies (Zero Padding)

Zero padding is a technique used in CNNs to add rows and columns of zeros around the border of an input tensor before applying convolution. This preserves spatial dimensions, allows the network to learn features at the edges, and gives control over the output size.

Formula:

For an input of shape (H, W) with padding size p:

output shape = (H + 2*p, W + 2*p)

The padded tensor has:

- p rows of zeros added to the top and bottom

- p columns of zeros added to the left and right

- Original values preserved in the center

Example:

Input (2x2): padding_size = 1

[[1, 2],

[3, 4]]

Output (4x4):

[[0, 0, 0, 0],

[0, 1, 2, 0],

[0, 3, 4, 0],

[0, 0, 0, 0]]

Zero padding is essential in deep CNN architectures to maintain spatial resolution across layers. Without padding, each convolution layer shrinks the feature map, limiting network depth. The two common strategies are "valid" (no padding) and "same" (pad so output has same spatial size as input).

Constraints:

  • Input tensor is a 2D numpy array of shape (height, width)
  • Padding size is a non-negative integer
  • Padding values are always zero
  • Output shape is (height + 2*padding_size, width + 2*padding_size)
  • Test Cases

    Test Case 1
    Input: [[1, 2], [3, 4]]
    Expected: [[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]]
    Test Case 2
    Input: [[5, 6], [7, 8]]
    Expected: [[0, 0, 0, 0], [0, 5, 6, 0], [0, 7, 8, 0], [0, 0, 0, 0]]
    + 3 hidden test cases