Fold
ModuleFold(output_size: _Size2d, kernel_size: _Size2d, dilation: _Size2d = 1, padding: _Size2d = 0, stride: _Size2d = 1)Combine an array of sliding local blocks back into a batched tensor.
Fold is the inverse of Unfold: it reconstructs a spatial tensor
from its column representation produced by the im2col (Unfold)
operation. This is also known as col2im.
When multiple blocks overlap a single output position, their contributions
are summed (accumulated), not averaged. Use a companion Fold of
all-ones to compute the overlap count if you need average pooling semantics.
where and are given by
output_size and the relationship
must hold consistently with the Unfold parameters used.
Parameters
output_sizeint or tuple[int, int]int is broadcast to both dimensions.kernel_sizeint or tuple[int, int]Unfold that produced the input).dilationint or tuple[int, int]= 11).paddingint or tuple[int, int]= 0Unfold
(default 0).strideint or tuple[int, int]= 1Unfold (default 1).Attributes
output_sizetuple[int, int](H_out, W_out) stored as a 2-tuple.kernel_sizetuple[int, int]dilationtuple[int, int]paddingtuple[int, int]stridetuple[int, int]Notes
- Input: .
- Output: .
- Overlapping patches accumulate (sum) their contributions; this is
the correct adjoint of
Unfoldfor gradient computation. - To reconstruct the average value at each position, fold a tensor of ones with the same parameters and divide element-wise.
- Backed by the C++ fold (col2im) op via
nn.functional.fold.
Examples
**Round-trip Unfold → Fold (non-overlapping patches):**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> H, W, kH, kW = 16, 16, 4, 4
>>> unfold = nn.Unfold(kernel_size=(kH, kW), stride=(kH, kW))
>>> fold = nn.Fold(output_size=(H, W), kernel_size=(kH, kW), stride=(kH, kW))
>>>
>>> x = lucid.randn(1, 3, H, W)
>>> patches = unfold(x) # (1, 48, 16)
>>> x_hat = fold(patches) # (1, 3, 16, 16) — exact reconstruction
>>> # (no overlap → no accumulation artefacts)
**Attention-weighted patch reconstruction (overlapping):**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> unfold = nn.Unfold(kernel_size=3, padding=1)
>>> fold = nn.Fold(output_size=(8, 8), kernel_size=3, padding=1)
>>> divisor_fold = nn.Fold(output_size=(8, 8), kernel_size=3, padding=1)
>>>
>>> x = lucid.randn(1, 2, 8, 8)
>>> cols = unfold(x) # (1, 18, 64)
>>> # ... apply per-patch attention weights ...
>>> ones = lucid.ones_like(cols)
>>> out = fold(cols) / divisor_fold(ones) # average over overlapsMethods (3)
__init__
→None__init__(output_size: _Size2d, kernel_size: _Size2d, dilation: _Size2d = 1, padding: _Size2d = 0, stride: _Size2d = 1)Initialise the Fold module. See the class docstring for parameter semantics.
forward
→Tensorforward(x: Tensor)Flatten (or unflatten) the specified dimensions of the input.
Parameters
inputTensorReturns
TensorTensor with the configured dimensions flattened or unflattened.
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.