class torch.nn.Fold(output_size: Union[T, Tuple[T, ...]], kernel_size: Union[T, Tuple[T, ...]], dilation: Union[T, Tuple[T, ...]] = 1, padding: Union[T, Tuple[T, ...]] = 0, stride: Union[T, Tuple[T, ...]] = 1)
Combines an array of sliding local blocks into a large containing tensor.
Consider a batched
input tensor containing sliding local blocks, e.g., patches of images, of shape , where is batch dimension, is the number of values within a block (a block has spatial locations each containing a -channeled vector), and is the total number of blocks. (This is exactly the same specification as the output shape of
Unfold.) This operation combines these local blocks into the large
output tensor of shape by summing the overlapping values. Similar to
Unfold, the arguments must satisfy
where is over all spatial dimensions.
output_sizedescribes the spatial shape of the large containing tensor of the sliding local blocks. It is useful to resolve the ambiguity when multiple input shapes map to same number of sliding blocks, e.g., with
stride > 0.
dilation arguments specify how the sliding blocks are retrieved.
stridecontrols the stride for the sliding blocks.
paddingcontrols the amount of implicit zero-paddings on both sides for
paddingnumber of points for each dimension before reshaping.
dilationcontrols the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what
strideis an int or a tuple of length 1 then their values will be replicated across all spatial dimensions.
Fold calculates each combined value in the resulting large tensor by summing all values from all containing blocks.
Unfold extracts the values in the local blocks by copying from the large tensor. So, if the blocks overlap, they are not inverses of each other.
>>> fold_params = dict(kernel_size=..., dilation=..., padding=..., stride=...) >>> fold = nn.Fold(output_size=..., **fold_params) >>> unfold = nn.Unfold(**fold_params)
Then for any (supported)
input tensor the following equality holds:
fold(unfold(input)) == divisor * input
divisor is a tensor that depends only on the shape and dtype of the
>>> input_ones = torch.ones(input.shape, dtype=input.dtype) >>> divisor = fold(unfold(input_ones))
divisor tensor contains no zero elements, then
unfold operations are inverses of each other (up to constant divisor).
Currently, only 4-D output tensors (batched image-like tensors) are supported.
>>> fold = nn.Fold(output_size=(4, 5), kernel_size=(2, 2)) >>> input = torch.randn(1, 3 * 2 * 2, 12) >>> output = fold(input) >>> output.size() torch.Size([1, 3, 4, 5])
© 2019 Torch Contributors
Licensed under the 3-clause BSD License.