tf.keras.ops.unfold
Stay organized with collections
Save and categorize content based on your preferences.
Extract sliding local blocks from a 4-D input (batched image).
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.
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.
|
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.
|
Example:
x = keras.ops.ones((1, 2, 4, 4))
patches = keras.ops.unfold(x, kernel_size=2, stride=2)
patches.shape
(1, 8, 4)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2026-10-31 UTC.
[null,null,["Last updated 2026-10-31 UTC."],[],[]]