class

ReflectionPad1d

extendsModule
ReflectionPad1d(padding: _Size2d)
source

Pad a 3-D tensor (N, C, L) by reflecting at the sequence boundaries.

Unlike constant padding, reflection padding mirrors the input signal itself at the boundary rather than filling with a fixed value. For a left pad of size pp, the new values at positions 0,1,,p10, 1, \dots, p-1 are set to x[,p],x[,p1],,x[,1]x[\dots, p], x[\dots, p-1], \dots, x[\dots, 1] respectively (the boundary pixel is excluded).

output[n,c,i]=x ⁣[n,c,  ipleft]for i<pleft\text{output}[n, c, i] = x\!\left[n, c,\; |i - p_{\text{left}}|\right] \quad \text{for } i < p_{\text{left}}

Parameters

paddingint or tuple[int, int]
(left, right) reflection padding sizes. Each value must be strictly less than the corresponding input dimension. A single int pads equally on both sides.

Attributes

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

Notes

  • Input: (N,C,L)(N, C, L).
  • Output: (N,C,L+pleft+pright)(N, C, L + p_{\text{left}} + p_{\text{right}}).
  • Reflection padding avoids the abrupt discontinuity introduced by zero or constant padding, making it preferable for tasks sensitive to border artefacts (image style transfer, super-resolution).
  • The padding size must be strictly less than the input size on the padded dimension.

Examples

**Mirror the ends of an audio waveform to avoid boundary clicks:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> pad = nn.ReflectionPad1d(padding=4)
>>> x = lucid.zeros(2, 1, 1024)     # (N, channels, T)
>>> pad(x).shape
(2, 1, 1032)
**Asymmetric reflection for causal-ish temporal convolution:**
>>> pad = nn.ReflectionPad1d(padding=(8, 0))
>>> x = lucid.zeros(4, 16, 200)
>>> pad(x).shape
(4, 16, 208)

Methods (3)

dunder

__init__

None
__init__(padding: _Size2d)
source

Initialise the ReflectionPad1d 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.