Open In App

Tensorflow.js tf.layers.averagePooling3d() Function

Last Updated : 12 Dec, 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.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:

  • args: It is an object with the following properties:
    • poolSize: It is used for downscaling factors in each dimension i.e [ depth, height, width ]. It is an integer or an array of three integers is expected. 
    • strides: In each dimension of the pooling window, the stride size. It is an integer or an array of three 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, It is used for creating the input layer which is inserted before this layer.
    • batchInputShape: If it is specified it is used for creating the input layer which is inserted before this layer.
    • batchSize: It is used for creating batchInputShape with the help of inputShape.
    • dtype: It is the data type for these layers. These parameters are applied exclusively to input layers. 
    • name: It is of string type. It is the name of these layers. 
    • 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 AveragePooling3D

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.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:

Javascript




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads