TensorFlow global average pooling

In this Python tutorial, we will learn how we can do global average pooling in Python TensorFlow. Also, we will cover the following topics.

  • TensorFlow global average pooling 1d
  • TensorFlow global average pooling 3d
  • TensorFlow global average pooling reduce_mean
  • Global average pooling layer TensorFlow

TensorFlow global average pooling

  • In this section, we will discuss how we can do global average pooling in Python TensorFlow.
  • To perform this particular task, we are going to use the tf.Keras.layers.GlobalAveragePooling2D() function and this function is used to operate global average pooling for given data.
  • For example, suppose we have an input feature map of dimensions height(h), width, and depth. When we pass this input layer into the global average pooling operation then it will calculate the average value of every single map and returns the average value to the output node.

Syntax:

Let’s have a look at the Syntax and understand the working of tf.Keras.layers.GlobalAveragePooling2D() in Python TensorFlow.

tf.keras.layers.GlobalAveragePooling2D(
    data_format=None, keepdims=False, **kwargs
)
  • It consists of a few parameters
    • data_format: This parameter specifies the ordering of the dimensions in the input channel and by default, it takes the image data_format value found in your Keras.
    • Keepdims: By default, it takes a false value that specifies the rank of the tensor is reduced and if it is true, the spatial dimension is retained with length 1.

Example:

Let’s take an example and check how we can do global average pooling in Python TensorFlow.

Source Code:

import tensorflow as tf
import numpy as np

new_tens = (12,23,45,6)
tens1 = tf.random.normal(new_tens)
tens2 = tf.keras.layers.GlobalAveragePooling2D()(tens1)
print(tens2.shape)
  • In the above code, we have created a tensor and then used the tf.random.normal() function and inside the function we assigned the tensor as an argument.
  • After that, we have used the tf.keras.layers.GlobalAveragePooling2D() function and this function return the output node.

Here is the Screenshot of the following given code.

TensorFlow global average pooling
TensorFlow global average pooling

Read: TensorFlow clip_by_value

TensorFlow global average pooling 1d

  • In this example, we will discuss how we can do global average pooling 1d in Python TensorFlow.
  • For this execution, we will be using the tf.Keras.layers.GlobalAveragePooling1D() function. This function is used to operate the global average pooling for temporal data.

Syntax:

Let’s have a look at the Syntax and understand the working tf.keras.layers.GlobalAveragePooling1D() in Python TensorFlow.

tf.keras.layers.GlobalAveragePooling1D(
    data_format='channels_last', **kwargs
)
  • It consists of a few parameters.
    • data_format: This parameter indicates the ordering of the dimension in the inputs and by default, it takes the ‘channels_last’ value with corresponds to the input with shape.
    • keepdims: By default, it takes a false value that specifies the rank of the tensor is reduced and if it is true, the spatial dimension is retained with length 1.

Example:

import tensorflow as tf
import numpy as np

tens_shape = (12, 34, 78)
tens1 = tf.random.normal(tens_shape)
tens2 = tf.keras.layers.GlobalAveragePooling1D()(tens1)
print(tens2.shape)

In the following code, we have used the tf.Keras.layers.GlobalAveragePooling1D() function and this function will return the value from the input tensor.

Here is the implementation of the following given code.

TensorFlow global average pooling 1d
TensorFlow global average pooling 1d

Read: Tensorflow embedding_lookup

TensorFlow global average pooling 3d

  • In this section, we will discuss how to use the tf.keras.layers.AveragePooling3D function in Python TensorFlow.
  • This function is used to operate the global average pooling for 3-dimensional data and it takes a 5D tensor with shape.

Syntax:

Let’s have a look at the Syntax and understand the working of tf.Keras.layers.AveragePooling3D() function in Python TensorFlow

tf.keras.layers.GlobalAveragePooling3D(
    data_format=None, keepdims=False, **kwargs
)
  • It consists of a few parameters
    • data_format: This parameter indicates the ordering of the dimension in the inputs and by default, it takes the ‘None’ value with corresponds to the input with shape.
    • Keepdims: By default, it takes a false value that specifies the rank of the tensor is reduced and if it is true, the spatial dimension is retained with length 1.

Example:

Let’s take an example and check how to use the tf.Keras.layers.AveragePooling3D() function in Python TensorFlow.

Source Code:

import tensorflow as tf
import numpy as np

new_dept = 20
new_length = 20
new_width = 20
input_channels = 2

new_input_val = tf.keras.Input(shape=(new_dept, new_length, new_width, input_channels))
layer = tf.keras.layers.AveragePooling3D(pool_size=2)
new_output = layer(new_input_val)
print(new_output)

In the above code, we have created the height, width, and depth values for average pooling strides in the tf.Keras.Input() function and then we have used the tf.Keras.AveragePooling3d() function and assigned the size of a given input layer.

Here is the execution of the following given code.

TensorFlow global average pooling 3d
TensorFlow global average pooling 3d

Read: TensorFlow Multiplication – Helpful Guide

TensorFlow global average pooling reduce_mean

  • Here we are going to discuss how to use the reduce_mean in global average pooling TensorFlow.
  • If the height and width are not defined for average pooling then we can also use the tf.reduce_mean() function and when you reduce along the axis [1,2] then it specifies the second and first dimensions of the tensor along with a number of filters.

Syntax:

Here is the Syntax of tf.reduce_mean() function in Python TensorFlow.

tf.reduce_mean(
    input_tensor,
    axis=None,
    keepdims=None,
    name=None,
    reduction_indices=None,
    keep_dims=None
)
  • It consists of a few parameters
    • input_tensor: This parameter indicates the tensor which we want to reduce and it should always be a numeric type.
    • axis: By default, it takes none value, and once you will use this in function then by default all dimensions will reduce.
    • keepdims: This parameter will check the condition if it is true then it reduces the length of the rank tensor. By default, it takes a False value.
    • name: It is an optional parameter and it indicates the name for the operation.

Example:

Let’s take an example and check how to use the reduce_mean in global average pooling TensorFlow.

Source Code:

import tensorflow as tf

new_tens = (12,23,45,6)
tens1 = tf.random.normal(new_tens)
output = tf.reduce_mean(tens1, [1, 2], name='global_pool') 
print(output)

In the above code, we have used the tf.reduce_mean() function instead of using the height and width. We have used the [1,2] axis and it specifies the first and second dimensions.

Here is the implementation of the following given code.

TensorFlow global average pooling reduce_mean
TensorFlow global average pooling reduce_mean

Read: TensorFlow Get Variable + Examples

Global average pooling layer TensorFlow

  • In this example, we will discuss how to use the average pooling layer in Python TensorFlow.
  • To do this task, we are going to use the tf.Keras.layers.AveragePooling2d() function and this function is used for average pooling operation for spatial data.

Syntax:

Here is the Syntax of tf.Keras.layers.AveragePooling2d() in Python TensorFlow.

tf.keras.layers.AveragePooling2D(
    pool_size=(2, 2),
    strides=None,
    padding='valid',
    data_format=None,
    **kwargs
)
  • It consists of a few parameters
    • pool_size: This parameter specifies the size of pooling and it must be an integer of(2,2) and the input in both spatial dimensions.
    • strides: By default, it takes none value and it specifies the strides value.
    • padding: By default, it takes ‘valid’ which means no padding and if the value is ‘same’ then results in padding evenly to the right/left of the input.
    • data_format: This parameter indicates the ordering of the dimension in the inputs and by default, it takes the ‘None’ value with corresponds to the input with shape.

Example:

Let’s take an example and check how to use the average pooling layer in Python TensorFlow.

Source Code:

import tensorflow as tf

new_tens = tf.constant([[2., 2., 1.],
                 [6., 7., 2.],
                 [1., 2., 5.]])
new_tens = tf.reshape(new_tens, [1, 3, 3, 1])
new_result_avg = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
   strides=(1, 1))
new_result_avg(new_tens)

Here is the Output of the following given code.

global average pooling layer TensorFlow
global average pooling layer TensorFlow

You may also like to read the following TensorFlow.

In this Python tutorial, we have learned how we can do global average pooling in Python TensorFlow. Also, we have covered the following topics.

  • TensorFlow global average pooling 1d
  • TensorFlow global average pooling 3d
  • TensorFlow global average pooling reduce_mean
  • Global average pooling layer TensorFlow