class
ReplicationPad2d
extends
ModuleReplicationPad2d(padding: int | tuple[int, int, int, int])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.
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: .
- Output: .
- 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])Initialise the ReplicationPad2d module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Pad the input tensor according to the configured padding.
Parameters
inputTensorInput tensor of shape .
Returns
TensorPadded tensor with spatial dimensions expanded by the configured padding amounts.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.