tf.nn.max_pool

Performs max pooling on the input.

For a given window of ksize, takes the maximum value within that window. Used for reducing computation and preventing overfitting.

Consider an example of pooling with 2x2, non-overlapping windows:

matrix = tf.constant([
    [0, 0, 1, 7],
    [0, 2, 0, 0],
    [5, 2, 0, 0],
    [0, 0, 9, 8],
])
reshaped = tf.reshape(matrix, (1, 4, 4, 1))
tf.nn.max_pool(reshaped, ksize=2, strides=2, padding="SAME")
<tf.Tensor: shape=(1, 2, 2, 1), dtype=int32, numpy=
array([[[[2],
         [7]],
        [[5],
         [9]]]], dtype=int32)>

We can adjust the window size using the ksize parameter. For example, if we were to expand the window to 3:

tf.nn.max_pool(reshaped, ksize=3, strides=2, padding="SAME")
<tf.Tensor: shape=(1, 2, 2, 1), dtype=int32,