Tensorflow.js tf.layers.averagePooling3d() Function
Last Updated :
12 Dec, 2022
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();
model.add(tf.layers.averagePooling3d({
poolSize: 4,
strides: 5,
padding: 'valid' ,
inputShape: [4, 3, 5, 2],
batchSize: 2,
dataFormat: 'channelsFirst'
}));
model.add(tf.layers.dense({ units: 5 }));
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
Share your thoughts in the comments
Please Login to comment...