Open In App

Tensorflow.js tf.layers.averagePooling3d() 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.averagePooling3d() function is used for applying the average Pooling operation for 3D data. If its dataFormat field is set to CHANNEL_LAST it takes the tensor as input with 5d shape [ batchSize, depths, rows, cols, channels] and outputs the tensor with 5d shape: [ batchSize, pooledDepths, pooledRows, pooledCols, channels]. Its dataFormat filed is set to CHANNEL_FIRST it takes the tensor as input with 4d shape: [ batchSize, channels, depths, rows, cols ] and outputs the tensor with shape: [ batchSize, channels, pooledDepths, pooledRows, pooledCols ].



Syntax: 

tf.layers.averagePooling3d( args )

Parameters:



Returns: It returns AveragePooling3D

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.averagePooling3d({
    poolSize: 4,
    strides: 5,
    padding: 'valid',
    inputShape: [4, 3, 5, 2],
    batchSize: 2,
    dataFormat: 'channelsFirst'
}));
 
// Afterwards, TF.js does automatic shape inference
model.add(tf.layers.dense({ units: 5 }));
 
// Inspect the inferred shape of the model's output
console.log(JSON.stringify(model.outputs[0].shape));

Output:

[2,4,0,1,5]

Example 2:




import * as tf from "@tensorflow/tfjs";
const Input = tf.input({ shape: [2, 2, 2, 3] });
const averagePooling3dLayer =
    tf.layers.averagePooling3d({
        poolSize: 3,
        strides: 2,
        padding: 'same',
        dataFormat: 'channelsLast',
        batchSize: 3
    });
 
const Output = averagePooling3dLayer.apply(Input);
const model = tf.model({ inputs: Input, outputs: Output });
const Data = tf.tensor5d([8, 2, 2, 6, 8, 9, 9, 4, 8, 9,
    3, 8, 3, 8, 9, 4, 5, 2, 5, 9, 6, 8, 9, 3],
    [1, 2, 2, 2, 3]
);
 
model.predict(Data).print();

Output:

Tensor
     [ [ [ [[6.5, 6, 5.875],]]]]

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


Article Tags :