class

ConstantPad2d

extends_ConstantPadNd
ConstantPad2d(padding: int | tuple[int, int, int, int], value: float)
source

Pad a 4-D tensor (N, C, H, W) on all four spatial sides with a constant.

Adds padding around the height and width dimensions. The padding tuple follows the convention (left, right, top, bottom):

Hout=H+ptop+pbottomWout=W+pleft+pright\text{H}_{\text{out}} = H + p_{\text{top}} + p_{\text{bottom}} \qquad \text{W}_{\text{out}} = W + p_{\text{left}} + p_{\text{right}}

Parameters

paddingint or tuple[int, int, int, int]
(left, right, top, bottom) padding sizes. A single int applies the same amount on all four sides.
valuefloat
Constant fill value.

Attributes

paddingtuple[int, int, int, int]
Normalised (left, right, top, bottom) padding.
valuefloat
Fill value.

Notes

  • Input: (N,C,H,W)(N, C, H, W).
  • Output: (N,C,H+ptop+pbottom,W+pleft+pright)(N, C, H + p_{\text{top}} + p_{\text{bottom}}, W + p_{\text{left}} + p_{\text{right}}).

Examples

**Uniform 2-pixel border padding:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> pad = nn.ConstantPad2d(padding=2, value=0.0)
>>> x = lucid.zeros(1, 3, 28, 28)
>>> pad(x).shape
(1, 3, 32, 32)
**Asymmetric padding (e.g. to adjust receptive field alignment):**
>>> pad = nn.ConstantPad2d(padding=(1, 2, 0, 3), value=-999.0)
>>> x = lucid.zeros(2, 16, 10, 10)
>>> pad(x).shape
(2, 16, 13, 13)    # H: 10+0+3=13,  W: 10+1+2=13

Methods (1)

dunder

__init__

None
__init__(padding: int | tuple[int, int, int, int], value: float)
source

Initialise the ConstantPad2d module. See the class docstring for parameter semantics.