<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/ops/nn.py#L3300-L3342) |

Extract sliding local blocks from a 4-D input (batched image).

#### View aliases


**Main aliases**

[`tf.keras.ops.nn.unfold`](https://www.tensorflow.org/api_docs/python/tf/keras/ops/unfold)

<br />

    tf.keras.ops.unfold(
        x, kernel_size, dilation=1, padding=0, stride=1
    )

This operation is known as **im2col** when used with convolution.
It rearranges the image into overlapping or non-overlapping patches
and returns a tensor whose *depth* (last axis) contains the flattened
patches.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | A 4-D tensor of shape `(N, C, H, W)` (**channels-first** format). |
| `kernel_size` | int or tuple of two ints, the size of the sliding window `(kH, kW)`. If a single int is given, it is used for both dimensions. |
| `dilation` | int or tuple of two ints, the spacing between kernel points (a.k.a. **dilation** or **atrous** convolution). Default: 1. |
| `padding` | int or tuple of two ints, the amount of zero-padding to apply to both spatial dimensions. Default: 0. |
| `stride` | int or tuple of two ints, the step size of the sliding window. Default: 1. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A 3-D tensor of shape `(N, C * kH * kW, L)` where `L = num_patches_H * num_patches_W` is the total number of patches extracted. ||

<br />

#### Example:

    x = keras.ops.ones((1, 2, 4, 4))
    patches = keras.ops.unfold(x, kernel_size=2, stride=2)
    patches.shape
    (1, 8, 4)