class

ReplicationPad2d

extendsModule
ReplicationPad2d(padding: int | tuple[int, int, int, int])
source

Pad a 4-D tensor (N, C, H, W) by replicating the edge pixels.

Each out-of-bounds position is filled with the value of the nearest in-bounds pixel. Corner out-of-bounds positions replicate the nearest corner pixel.

output[n,c,i,j]=x ⁣[n,c,  clip(ipt,0,H1),  clip(jpl,0,W1)]\text{output}[n, c, i, j] = x\!\left[n, c,\; \text{clip}(i - p_t, 0, H-1),\; \text{clip}(j - p_l, 0, W-1) \right]

Parameters

paddingint or tuple[int, int, int, int]
(left, right, top, bottom) replication padding sizes. A single int pads equally on all four sides.

Attributes

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

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}}).
  • Replication padding is preferred over zero padding in networks that process images with meaningful content close to the borders (e.g. panoramic or satellite imagery) because it avoids introducing a dark border artefact that the network would need to learn to ignore.

Examples

**Replicate borders before a 5×5 convolution:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> pad = nn.ReplicationPad2d(padding=2)
>>> conv = nn.Conv2d(3, 32, kernel_size=5)
>>> x = lucid.zeros(1, 3, 64, 64)
>>> conv(pad(x)).shape
(1, 32, 64, 64)    # same-size output
**Asymmetric replication:**
>>> pad = nn.ReplicationPad2d(padding=(0, 0, 1, 1))   # top and bottom only
>>> x = lucid.zeros(4, 16, 30, 30)
>>> pad(x).shape
(4, 16, 32, 30)

Methods (3)

dunder

__init__

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

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

fn

forward

Tensor
forward(x: Tensor)
source

Pad the input tensor according to the configured padding.

Parameters

inputTensor
Input tensor of shape (N,C,)(N, C, *).

Returns

Tensor

Padded tensor with spatial dimensions expanded by the configured padding amounts.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.