Thursday, October 23, 2025
HomeLanguagesJavascriptTensorflow.js tf.layers.globalAveragePooling2d() Function

Tensorflow.js tf.layers.globalAveragePooling2d() 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.globalAveragePooling2d() function is used for applying global average pooling operation for spatial data.

Syntax: 

tf.layers.globalAveragePooling2d( args )

Parameters: 

  • args: It accepts an object with the following parameters: 
    • dataFormat: The data format to utilize for the pooling layer. It should be ‘channelsFirst’ or ‘channelsLast’. 
    • 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.

Return Value: It returns GobalAveragePooling2D

Example 1: 

Javascript




import * as tf from "@tensorflow/tfjs";
  
const Input = tf.input({ shape: [3, 3, 3] });
const globalAveragePooling2d =
    tf.layers.globalAveragePooling2d({  
        dataFormat: 'channelsFirst',
        batchInputShape:[4,3, 3], 
        trainable: true 
    });
  
const Output = globalAveragePooling2d.apply(Input);
  
const Data = tf.ones([4, 3, 3, 3]);
const model =
    tf.model({ inputs: Input, outputs: Output });
  
model.predict(Data).print();


Output: 

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

Example 2:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const Input = tf.input({ shape: [2,  2, 3] });
  
const globalAveragePooling2d =
    tf.layers.globalAveragePooling2d({
        dataFormat: 'channelsLast', 
        batchInputShape: [4, 3, 3], 
        trainable:true
    });
      
const Output = globalAveragePooling2d.apply(Input);
  
const model = tf.model({ inputs: Input, outputs: Output });
  
const Data = tf.tensor4d([8, 2, 2, 6, 8, 9, 9, 
    4, 8, 9, 3, 8, 5, 2, 5, 2, 8, 6, 4, 5, 9, 
    12, 8, 11], [2, 2 ,2, 3]);
      
model.predict(Data).print();


Output:

Tensor
    [[8   , 4.25, 6.75],
     [5.75, 5.75, 7.75]]

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS