Open In App
Related Articles

Tensorflow.js tf.layers.globalAveragePooling1d() Function

Improve Article
Save Article
Like Article

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.globalAveragePooling1d() function is used for applying global average pooling operation for temporal data.


tf.layers.globalAveragePooling1d( args )


  • args: It takes an object with the following properties:
    • 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.
    • InputDType: It is used for Legacy support.

Returns: It returns GlobalAveragePooling1D.

Example 1:


import * as tf from "@tensorflow.js/tfjs"
const model = tf.sequential();
// First layer must have a defined input shape
    batchInputShape:[4,3, 3], 
    trainable: true 
// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 3}));
// Printing the summary of model


Layer (type)                Input Shape               Output shape              Param #   
global_average_pooling1d_Gl [[4,3,3]]                 [4,3]                     0         
dense_Dense7 (Dense)        [[4,3]]                   [4,3]                     12        
Total params: 12
Trainable params: 12
Non-trainable params: 0

Example 2:


import * as tf from "@tensorflow/tfjs";
const Input = tf.input({ shape: [2, 5] });
const globalAveragePooling1d =
          InputShape:[4,3, 3], trainable: true 
const Output = globalAveragePooling1d.apply(Input);
const Data = tf.ones([2, 2, 5]);
const model =
    tf.model({ inputs: Input, outputs: Output });


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


Last Updated : 24 Apr, 2022
Like Article
Save Article
Similar Reads
Related Tutorials