Fold#
- classtorch.nn.Fold(output_size,kernel_size,dilation=1,padding=0,stride=1)[source]#
Combines an array of sliding local blocks into a large containing tensor.
Consider a batched
inputtensor 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 hasspatial locations each containing a-channeled vector), and is the total number of blocks. (This is exactly thesame specification as the output shape ofUnfold.) Thisoperation combines these local blocks into the largeoutputtensorof shapeby summing the overlapping values. Similar toUnfold, thearguments must satisfywhere is over all spatial dimensions.
output_sizedescribes the spatial shape of the large containingtensor of the sliding local blocks. It is useful to resolve the ambiguitywhen multiple input shapes map to same number of sliding blocks, e.g.,withstride>0.
The
padding,strideanddilationarguments specifyhow the sliding blocks are retrieved.stridecontrols the stride for the sliding blocks.paddingcontrols the amount of implicit zero-paddings on bothsides forpaddingnumber of points for each dimension beforereshaping.dilationcontrols the spacing between the kernel points; also known as the à trous algorithm.It is harder to describe, but thislink has a nice visualization of whatdilationdoes.
- Parameters
output_size (int ortuple) – the shape of the spatial dimensions of theoutput (i.e.,
output.sizes()[2:])dilation (int ortuple,optional) – a parameter that controls thestride of elements within theneighborhood. Default: 1
padding (int ortuple,optional) – implicit zero padding to be added onboth sides of input. Default: 0
stride (int ortuple) – the stride of the sliding blocks in the inputspatial dimensions. Default: 1
If
output_size,kernel_size,dilation,paddingorstrideis an int or a tuple of length 1 thentheir values will be replicated across all spatial dimensions.For the case of two output spatial dimensions this operation is sometimescalled
col2im.
Note
Foldcalculates each combined value in the resultinglarge tensor by summing all values from all containing blocks.Unfoldextracts the values in the local blocks bycopying from the large tensor. So, if the blocks overlap, they are notinverses of each other.In general, folding and unfolding operations are related asfollows. Consider
FoldandUnfoldinstances created with the sameparameters:>>>fold_params=dict(kernel_size=...,dilation=...,padding=...,stride=...)>>>fold=nn.Fold(output_size=...,**fold_params)>>>unfold=nn.Unfold(**fold_params)
Then for any (supported)
inputtensor the followingequality holds:fold(unfold(input))==divisor*input
where
divisoris a tensor that depends only on the shapeand dtype of theinput:>>>input_ones=torch.ones(input.shape,dtype=input.dtype)>>>divisor=fold(unfold(input_ones))
When the
divisortensor contains no zero elements, thenfoldandunfoldoperations are inverses of eachother (up to constant divisor).Warning
Currently, only unbatched (3D) or batched (4D) image-like output tensors are supported.
- Shape:
Input: or
Output:or as described above
Examples:
>>>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])