tf.nn.max_pool2d
Stay organized with collections
Save and categorize content based on your preferences.
Performs max pooling on 2D spatial data such as images.
tf.nn.max_pool2d(
input, ksize, strides, padding, data_format='NHWC', name=None
)
This is a more specific version of tf.nn.max_pool
where the input tensor
is 4D, representing 2D spatial data such as images. Using these APIs are
equivalent
Downsamples the input images along theirs spatial dimensions (height and
width) by taking its maximum over an input window defined by ksize
.
The window is shifted by strides
along each dimension.
For example, for strides=(2, 2)
and padding=VALID
windows that extend
outside of the input are not included in the output:
x = tf.constant([[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.]])
# Add the `batch` and `channels` dimensions.
x = x[tf.newaxis, :, :, tf.newaxis]
result = tf.nn.max_pool2d(x, ksize=(2, 2), strides=(2, 2),
padding="VALID")
result[0, :, :, 0]
<tf.Tensor: shape=(1, 2), dtype=float32, numpy=
array([[6., 8.]], dtype=float32)>
With padding=SAME
, we get:
x = tf.constant([[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.]])
x = x[tf.newaxis, :, :, tf.newaxis]
result = tf.nn.max_pool2d(x, ksize=(2, 2), strides=(2, 2),
padding='SAME')
result[0, :, :, 0]
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 6., 8.],
[10.,12.]], dtype=float32)>
We can also specify padding explicitly. The following example adds width-1
padding on all sides (top, bottom, left, right):
x = tf.constant([[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.]])
x = x[tf.newaxis, :, :, tf.newaxis]
result = tf.nn.max_pool2d(x, ksize=(2, 2), strides=(2, 2),
padding=[[0, 0], [1, 1], [1, 1], [0, 0]])
result[0, :, :, 0]
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[ 1., 3., 4.],
[ 9., 11., 12.]], dtype=float32)>
For more examples and detail, see tf.nn.max_pool
.
Args |
input
|
A 4-D Tensor of the format specified by data_format .
|
ksize
|
An int or list of ints that has length 1 , 2 or 4 . The size of
the window for each dimension of the input tensor. If only one integer is
specified, then we apply the same window for all 4 dims. If two are
provided then we use those for H, W dimensions and keep N, C dimension
window size = 1.
|
strides
|
An int or list of ints that has length 1 , 2 or 4 . The
stride of the sliding window for each dimension of the input tensor. If
only one integer is specified, we apply the same stride to all 4 dims. If
two are provided we use those for the H, W dimensions and keep N, C of
stride = 1.
|
padding
|
Either the string "SAME" or "VALID" indicating the type of
padding algorithm to use, or a list indicating the explicit paddings at
the start and end of each dimension. See
here
for more information. When explicit padding is used and data_format is
"NHWC" , this should be in the form [[0, 0], [pad_top, pad_bottom],
[pad_left, pad_right], [0, 0]] . When explicit padding used and
data_format is "NCHW" , this should be in the form [[0, 0], [0, 0],
[pad_top, pad_bottom], [pad_left, pad_right]] . When using explicit
padding, the size of the paddings cannot be greater than the sliding
window size.
|
data_format
|
A string. 'NHWC', 'NCHW' and 'NCHW_VECT_C' are supported.
|
name
|
Optional name for the operation.
|
Returns |
A Tensor of format specified by data_format .
The max pooled output tensor.
|
Raises |
ValueError
|
If explicit padding is used with data_format='NCHW_VECT_C'.
|
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 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.nn.max_pool2d\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/nn_ops.py#L4971-L5079) |\n\nPerforms max pooling on 2D spatial data such as images.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.nn.max_pool2d`](https://www.tensorflow.org/api_docs/python/tf/nn/max_pool2d)\n\n\u003cbr /\u003e\n\n tf.nn.max_pool2d(\n input, ksize, strides, padding, data_format='NHWC', name=None\n )\n\nThis is a more specific version of [`tf.nn.max_pool`](../../tf/nn/max_pool) where the input tensor\nis 4D, representing 2D spatial data such as images. Using these APIs are\nequivalent\n\nDownsamples the input images along theirs spatial dimensions (height and\nwidth) by taking its maximum over an input window defined by `ksize`.\nThe window is shifted by `strides` along each dimension.\n\nFor example, for `strides=(2, 2)` and `padding=VALID` windows that extend\noutside of the input are not included in the output: \n\n x = tf.constant([[1., 2., 3., 4.],\n [5., 6., 7., 8.],\n [9., 10., 11., 12.]])\n # Add the `batch` and `channels` dimensions.\n x = x[tf.newaxis, :, :, tf.newaxis]\n result = tf.nn.max_pool2d(x, ksize=(2, 2), strides=(2, 2),\n padding=\"VALID\")\n result[0, :, :, 0]\n \u003ctf.Tensor: shape=(1, 2), dtype=float32, numpy=\n array([[6., 8.]], dtype=float32)\u003e\n\nWith `padding=SAME`, we get: \n\n x = tf.constant([[1., 2., 3., 4.],\n [5., 6., 7., 8.],\n [9., 10., 11., 12.]])\n x = x[tf.newaxis, :, :, tf.newaxis]\n result = tf.nn.max_pool2d(x, ksize=(2, 2), strides=(2, 2),\n padding='SAME')\n result[0, :, :, 0]\n \u003ctf.Tensor: shape=(2, 2), dtype=float32, numpy=\n array([[ 6., 8.],\n [10.,12.]], dtype=float32)\u003e\n\nWe can also specify padding explicitly. The following example adds width-1\npadding on all sides (top, bottom, left, right): \n\n x = tf.constant([[1., 2., 3., 4.],\n [5., 6., 7., 8.],\n [9., 10., 11., 12.]])\n x = x[tf.newaxis, :, :, tf.newaxis]\n result = tf.nn.max_pool2d(x, ksize=(2, 2), strides=(2, 2),\n padding=[[0, 0], [1, 1], [1, 1], [0, 0]])\n result[0, :, :, 0]\n \u003ctf.Tensor: shape=(2, 3), dtype=float32, numpy=\n array([[ 1., 3., 4.],\n [ 9., 11., 12.]], dtype=float32)\u003e\n\nFor more examples and detail, see [`tf.nn.max_pool`](../../tf/nn/max_pool).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | A 4-D `Tensor` of the format specified by `data_format`. |\n| `ksize` | An int or list of `ints` that has length `1`, `2` or `4`. The size of the window for each dimension of the input tensor. If only one integer is specified, then we apply the same window for all 4 dims. If two are provided then we use those for H, W dimensions and keep N, C dimension window size = 1. |\n| `strides` | An int or list of `ints` that has length `1`, `2` or `4`. The stride of the sliding window for each dimension of the input tensor. If only one integer is specified, we apply the same stride to all 4 dims. If two are provided we use those for the H, W dimensions and keep N, C of stride = 1. |\n| `padding` | Either the `string` `\"SAME\"` or `\"VALID\"` indicating the type of padding algorithm to use, or a list indicating the explicit paddings at the start and end of each dimension. See [here](https://www.tensorflow.org/api_docs/python/tf/nn#notes_on_padding_2) for more information. When explicit padding is used and data_format is `\"NHWC\"`, this should be in the form `[[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]`. When explicit padding used and data_format is `\"NCHW\"`, this should be in the form `[[0, 0], [0, 0], [pad_top, pad_bottom], [pad_left, pad_right]]`. When using explicit padding, the size of the paddings cannot be greater than the sliding window size. |\n| `data_format` | A string. 'NHWC', 'NCHW' and 'NCHW_VECT_C' are supported. |\n| `name` | Optional name for the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of format specified by `data_format`. The max pooled output tensor. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------------|\n| `ValueError` | If explicit padding is used with data_format='NCHW_VECT_C'. |\n\n\u003cbr /\u003e"]]