mindspore.mint.nn.functional.fold
- mindspore.mint.nn.functional.fold(input, output_size, kernel_size, dilation=1, padding=0, stride=1)[source]
Combines an array of sliding local blocks into a larger containing tensor.
Consider a batched input tensor of shape \((N, C \times \prod(\text{kernel_size}), L)\) , where \(N\) is the batch dimension, \(C \times \prod(\text{kernel_size})\) is the total number of values within each block (a block has \(\prod(\text{kernel_size})\) spatial locations, each containing a C-channel vector), and \(L\) is the total number of such blocks:
\[L = \prod_d \left\lfloor\frac{\text{output_size}[d] + 2 \times \text{padding}[d] % - \text{dilation}[d] \times (\text{kernel_size}[d] - 1) - 1}{\text{stride}[d]} + 1\right\rfloor,\]where \(d\) is over all spatial dimensions.
Therefore, output_size is the spatial shape of the large containing tensor of the sliding local blocks.
The dilation, padding and stride arguments specify how the sliding blocks are retrieved.
Warning
Currently, only unbatched (3D) or batched (4D) image-like output tensors are supported.
- Parameters
input (Tensor) – 2-D or 3-D Tensor.
output_size (Union[int, tuple[int], list[int]]) – The shape of the spatial dimensions of the output (i.e., output.shape[2:]).
kernel_size (Union[int, tuple[int], list[int]]) – The size of the kernel, should be two int for height and width. If type is int, it means that height equal with width. Must be specified.
dilation (Union[int, tuple[int], list[int]], optional) – The size of the dilation, should be two int for height and width. If type is int, it means that height equal with width. Default:
1.padding (Union[int, tuple[int], list[int]], optional) – The size of the padding, should be two int for height and width. If type is int, it means that height equal with width. Default:
0.stride (Union[int, tuple[int], list[int]], optional) – The size of the stride, should be two integers for height and width. If type is int, it means that height equal with width. Default:
1.
- Returns
A Tensor, with same type as input .
- Shape:
Input: \((N, C \times \prod(\text{kernel_size}), L)\) or \((C \times \prod(\text{kernel_size}), L)\)
Output: \((N, C, output\_size[0], output\_size[1], ...)\) or \((C, output\_size[0], output\_size[1], ...)\)
- Raises
TypeError – If output_size, kernel_size, stride, dilation, or padding is not an int, tuple, or list.
ValueError – If output_size, kernel_size, dilation, or stride is not greater than zero or has an invalid number of elements.
ValueError – If padding is less than zero or has an invalid number of elements.
ValueError – If input.shape[-2] is not divisible by the product of kernel_size.
ValueError – If input.shape[-1] is not equal to the calculated number of sliding blocks L.
- Supported Platforms:
Ascend
Examples
>>> import numpy as np >>> from mindspore import Tensor, mint >>> x = Tensor(np.random.rand(16, 64, 25).astype(np.float32)) >>> output = mint.nn.functional.fold(x, (8, 8), [2, 2], [2, 2], [2, 2], [2, 2]) >>> print(output.shape) (16, 16, 8, 8)