## Debug School

rakesh kumar

Posted on • Updated on

# list out the checklist of Tensor Pooling Operation in CNN in tensor flow

Max Pooling - tf.nn.max_pool:

Example:

``````input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
pool_output = tf.nn.max_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
``````

Output:
Applying max pooling with a 2x2 pooling window and 2x2 strides.

Explanation
Input Tensor (input_data):

``````[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
`````` Position (0, 0) in the output tensor (pool_output[0, 0]):

``````pool_output[0, 0] = max(1, 2, 4, 5) = 5
``````

Position (0, 1) in the output tensor (pool_output[0, 1]):

``````pool_output[0, 1] = max(2, 3, 5, 6) = 6
``````

Position (1, 0) in the output tensor (pool_output[1, 0]):

``````pool_output[1, 0] = max(4, 5, 7, 8) = 8
``````

Position (1, 1) in the output tensor (pool_output[1, 1]):

``````pool_output[1, 1] = max(5, 6, 8, 9) = 9
``````

So, the resulting pool_output tensor is:

``````[[5, 6],
[8, 9]]
``````

Each element in the output tensor represents the maximum value in the corresponding 2x2 region of the input tensor. This is how max pooling works in this specific example. I appreciate your understanding, and I apologize for any confusion in my previous responses

Average Pooling - tf.nn.avg_pool:

Example:

``````input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
pool_output = tf.nn.avg_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
``````

Output:
Applying average pooling with a 2x2 pooling window and 2x2 strides.
Explanation

``````import tensorflow as tf

input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
pool_output = tf.nn.avg_pool(input_data, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
``````

Here, you are performing average pooling on a 3x3 input tensor (input_data).

Input Tensor (input_data):

``````[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
``````

Average Pooling Operation:

The ksize parameter specifies the size of the pooling window for each dimension. In this case, it's set to [1, 2, 2, 1], meaning a 2x2 window for both height and width.
The strides parameter defines the step size of the pooling window. It's set to [1, 2, 2, 1], indicating a stride of 2 in both height and width.
Output Tensor (pool_output):

The average pooling operation is applied with the specified window size and stride, resulting in a downsampled output tensor.
The output tensor dimensions are determined by the input size, pooling window size, and stride, following the 'VALID' padding strategy.
Let's compute the average pooling operation at each position:

Position (0, 0) in the output tensor (pool_output[0, 0]):

``````pool_output[0, 0] = average(1, 2, 4, 5) = 3.0
``````

Position (0, 1) in the output tensor (pool_output[0, 1]):

``````pool_output[0, 1] = average(2, 3, 5, 6) = 4.0
``````

Position (1, 0) in the output tensor (pool_output[1, 0]):

``````pool_output[1, 0] = average(4, 5, 7, 8) = 5.5
``````

Position (1, 1) in the output tensor (pool_output[1, 1]):

``````pool_output[1, 1] = average(5, 6, 8, 9) = 7.0
``````

So, the resulting pool_output tensor is:

``````[[3.0, 4.0],
[5.5, 7.0]]
``````

Each element in the output tensor represents the average value in the corresponding 2x2 region of the input tensor. This is how average pooling works in this specific example. If you have further questions or if anything is unclear, feel free to ask!

Global Average Pooling - tf.reduce_mean:

Example:

``````input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
global_avg_pool_output = tf.reduce_mean(input_data, axis=[1, 2], keepdims=True)
``````

Output:
Performing global average pooling to get a single value for the entire feature map.

Explanation

``````import tensorflow as tf

input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
global_avg_pool_output = tf.reduce_mean(input_data, axis=[1, 2], keepdims=True)
``````

Input Tensor (input_data):

``````[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
``````

Global Average Pooling Operation:

tf.reduce_mean is used to calculate the mean (average) along specified axes of the input tensor.
axis=[1, 2] indicates that the mean is calculated along the second and third dimensions (height and width). This effectively computes the mean across all spatial dimensions, performing global average pooling.
keepdims=True ensures that the dimensions that are reduced are retained with size 1.
Output Tensor (global_avg_pool_output):

The output tensor is a result of global average pooling, where each element represents the average value across the entire spatial dimensions of the input tensor.
Let's compute the global average pooling operation:

Global Average Pooling (global_avg_pool_output):

``````global_avg_pool_output = mean([1, 2, 3, 4, 5, 6, 7, 8, 9]) = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) / 9
= 45 / 9
= 5.0
``````

So, the resulting global_avg_pool_output tensor is:

``````[[5.0]]
``````

Global Max Pooling - tf.reduce_max:

Example:

``````input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
global_max_pool_output = tf.reduce_max(input_data, axis=[1, 2], keepdims=True)
``````

Output:
Performing global max pooling to get a single value for the entire feature map.

Explanation

``````import tensorflow as tf

input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
global_max_pool_output = tf.reduce_max(input_data, axis=[1, 2], keepdims=True)
``````

Input Tensor (input_data):

``````[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
``````

Global Max Pooling Operation:

tf.reduce_max is used to calculate the maximum value along specified axes of the input tensor.
axis=[1, 2] indicates that the maximum value is calculated along the second and third dimensions (height and width). This effectively computes the maximum value across all spatial dimensions, performing global max pooling.
keepdims=True ensures that the dimensions that are reduced are retained with size 1.
Output Tensor (global_max_pool_output):

The output tensor is a result of global max pooling, where each element represents the maximum value across the entire spatial dimensions of the input tensor.
Let's compute the global max pooling operation:

Global Max Pooling (global_max_pool_output):

``````global_max_pool_output = max([1, 2, 3, 4, 5, 6, 7, 8, 9]) = 9
``````

So, the resulting global_max_pool_output tensor is:

[[9.0]]
Fractional Max Pooling - tf.nn.fractional_max_pool:

Example:

``````input_data = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=tf.float32)
pool_output = tf.nn.fractional_max_pool(input_data, pool_ratio=[1.0, 1.4, 1.4, 1.0], pseudo_random=True)
``````

Output:
Applying fractional max pooling with specified pool ratios and pseudo-random sampling.

``````import tensorflow as tf

input_data = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=tf.float32)
pool_output = tf.nn.fractional_max_pool(input_data, pool_ratio=[1.0, 1.4, 1.4, 1.0], pseudo_random=True)
``````

Input Tensor (input_data):

``````[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
``````

Fractional Max Pooling Operation:

tf.nn.fractional_max_pool is used for fractional max pooling.
pool_ratio=[1.0, 1.4, 1.4, 1.0] specifies the pooling ratio for each dimension (batch, height, width, channels).
pseudo_random=True enables the use of pseudorandom values during pooling, introducing stochasticity.
Output Tensor (pool_output):

The output tensor is the result of fractional max pooling applied to the input tensor.
Fractional max pooling involves dividing each pooling window into grid cells and selecting the maximum value from these cells based on the specified pooling ratio. The pooling ratio determines the size of each grid cell.

In this case, since the pooling ratio is set to [1.0, 1.4, 1.4, 1.0], it means that the height and width of the pooling window will be downscaled by a factor of 1.4 while keeping the batch and channel dimensions unchanged.

The specific values in the output tensor would depend on the pseudorandom values generated during the pooling process. The stochasticity introduced by pseudo_random=True means that the pooling operation may select different values during each run.

Unpooling (Up-Pooling) - Custom Operation:

Example:

``````pooled_data = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
unpool_output = unpooled_data  # Custom implementation of up-pooling.
``````

Output:
Performing unpooling to upsample the pooled data.

``````input_data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)