class
ReflectionPad2d
extends
ModuleReflectionPad2d(padding: int | tuple[int, int, int, int])Pad a 4-D tensor (N, C, H, W) by reflecting at all four spatial edges.
Reflection padding mirrors the pixel values at each boundary, producing a smooth, artefact-free extension of the image. This is particularly effective for convolutional image processing tasks where constant-value borders would introduce ringing or aliasing.
The reflected value at an out-of-bounds index (with respect to input size and left-pad ) is:
Parameters
paddingint or tuple[int, int, int, int](left, right, top, bottom) reflection padding sizes. Each value
must be strictly less than the corresponding spatial input size.
A single int pads equally on all four sides.Attributes
paddingtuple[int, int, int, int]Normalised
(left, right, top, bottom) padding.Notes
- Input: .
- Output: .
- Widely used in Neural Style Transfer and image-to-image translation architectures (e.g. Johnson et al., 2016) to avoid border colour bleeding caused by zero padding.
Examples
**Style-transfer encoder with reflection padding:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> pad = nn.ReflectionPad2d(padding=3)
>>> conv = nn.Conv2d(3, 64, kernel_size=7)
>>> x = lucid.zeros(1, 3, 256, 256)
>>> conv(pad(x)).shape
(1, 64, 256, 256) # 256 + 2*3 - 6 = 256 (same-size output)
**Asymmetric reflection:**
>>> pad = nn.ReflectionPad2d(padding=(1, 2, 0, 3))
>>> x = lucid.zeros(2, 8, 32, 32)
>>> pad(x).shape
(2, 8, 35, 35)Methods (3)
dunder
__init__
→None__init__(padding: int | tuple[int, int, int, int])Initialise the ReflectionPad2d 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.