Open In App

Tensorflow.js tf.layers.averagePooling2d() Function

Last Updated : 03 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The tf.layers.averagePooling2d() function is used for apply average pooling operation for spatial data. If its dataFormat field is set to CHANNEL_LAST it takes the tensor as input with 4d shape [ batchSize, rows, cols, channels] and outputs the tensor with 4d shape: [ batchSize, poolsRows, pooledCols, channels]. Its dataFormat filed is set to CHANNEL_FIRST it takes tensor as input with 4d shape: [ batchSize, channels, rows, cols ] and outputs the tensor with shape: [ batchSize, channels, poolsRows, pooledCols ]. 

Syntax:

tf.layers.averagePooling2d( args )

Parameters:

  • args: It is an object with the following properties:
    • poolSize: It is used for downscaling factors in each dimension i.e [vertical, horizontal]. It is an integer or an array of two integers is expected. 
    • strides: In each dimension of the pooling window, the stride size. It is an integer or an array of two integers is required. 
    • padding: The padding type to utilize for the pooling layer.
    • dataFormat: The data format to utilize for the pooling layer. 
    • inputShape: If it is specified then, it will be utilized to construct an input layer that will be inserted before this layer. 
    • batchInputShape: If it is specified it will be used for creating an input layer which will be inserted before this layer.
    • batchSize: It supports the inputShape to build the batchInputShape.
    • dtype: It is the kind of data type for this layer. This parameter applies exclusively to input layers.
    • name: It is of string type. It is the name of this layer.
    • trainable: If it is set to be true then only the weights of this layer will be changed by fit.
    • weights: The layer’s initial weight values.

Returns: It returns AveragePooling2D

Example 1:

Javascript




import * as tf from "@tensorflow.js/tfjs"
 
const model = tf.sequential();
// First layer must have a defined input shape
model.add(tf.layers.averagePooling2d({
    poolSize: 2,
    strides: 3,
    padding: 'valid',
    inputShape: [2, 3, 2]
}));
// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 3}));
model.add(tf.layers.averagePooling2d({
    poolSize: 2,
    strides: 3,
    padding: 'valid'
}));
// Inspect the inferred shape of the model's output.
model.summary();


Output:

__________________________________________________________________________________________
Layer (type)                Input Shape               Output shape              Param #   
==========================================================================================
average_pooling2d_AveragePo [[null,2,3,2]]            [null,1,1,2]              0         
__________________________________________________________________________________________
dense_Dense4 (Dense)        [[null,1,1,2]]            [null,1,1,3]              9         
__________________________________________________________________________________________
average_pooling2d_AveragePo [[null,1,1,3]]            [null,0,0,3]              0         
==========================================================================================
Total params: 9
Trainable params: 9
Non-trainable params: 0
__________________________________________________________________________________________

Example 2:

Javascript




import * as tf from "@tensorflow/tfjs";
 
const Input = tf.input({ shape: [2, 3, 5] });
const averagePooling2DLayer =
    tf.layers.averagePooling2d(
        { dataFormat: 'channelsLast' }
    );
const Output = averagePooling2DLayer.apply(Input);
const Data = tf.ones([2, 2, 3, 5]);
const model =
    tf.model({ inputs: Input, outputs: Output });
model.predict(Data).print();


Output:

Tensor
    [ [ [[1, 1, 1, 1, 1],]],


      [ [[1, 1, 1, 1, 1],]]]

Reference: https://js.tensorflow.org/api/latest/#layers.averagePooling2d



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads