Skip to content
Related Articles

Related Articles

Tensorflow.js tf.layers.globalAveragePooling1d() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 24 Apr, 2022
View Discussion
Improve Article
Save 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.

Syntax: 

tf.layers.globalAveragePooling1d( args )

Parameters: 

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

Javascript




import * as tf from "@tensorflow.js/tfjs"
  
const model = tf.sequential();
  
// First layer must have a defined input shape
model.add(tf.layers.globalAveragePooling1d({
    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
model.summary();


Output:

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

Javascript




import * as tf from "@tensorflow/tfjs";
  
const Input = tf.input({ shape: [2, 5] });
const globalAveragePooling1d =
    tf.layers.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 });
  
model.predict(Data).print();


Output:

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

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!