Open In App

Tensorflow.js tf.layers.averagePooling2d() Function

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:



Returns: It returns AveragePooling2D

Example 1:




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:




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


Article Tags :