Skip to content
Related Articles

Related Articles

Tensorflow.js tf.layers.averagePooling1d() Function

Improve Article
Save Article
  • Last Updated : 28 Apr, 2022
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.averagePooling1d() function is used to apply the average pooling operation on data.

Syntax:

tf.layers.averagePooling1d (args)

Input shape: [batchSize, inLength, channels]

Output shape: [batchSize, pooledLength, channels]

Parameters: It accepts the args object which can have the following properties:

  • args: It is an object type value that can accept the following values –
    • poolSize: It is the size of the pooling window. This should be an integer.
    • strides: The period for sampling the pooled values. Defaults to poolSize if null.
    • padding: This specifies how to fill in data that are not an integral multiple of poolSize
    • inputShape: If this property is set, it will be utilized to construct an input layer that will be inserted before this layer. 
    • batchInputShape: If this property is set, an input layer will be created and inserted before this layer. 
    • batchSize: If batchInputShape isn’t supplied and inputShape is, batchSize is utilized to build the batchInputShape.
    • dtype: It is the kind of data type for this layer. float32 is the default value. This parameter applies exclusively to input layers.
    • name: This is the layer’s name and is of string type.
    • trainable: If the weights of this layer may be changed by fit. True is the default value.
    • weights: The layer’s initial weight values.
    • inputDType: It is the legacy support. It will not be used with new code.

Returns: It returns an object (AveragePooling1D).

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const input = tf.input({ shape: [4, 4] });
const averagePoolingLayer = tf.layers.averagePooling1d({ 
    poolSize: 2, 
    strides: 1, 
    padding: 'valid' 
});
  
const output = averagePoolingLayer.apply(input);
  
const model = tf.model({ 
    inputs: input, 
    outputs: output 
});
  
model.predict(tf.ones([1, 4, 4])).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: [4, 3] });
  
const averagePoolingLayer = tf.layers.averagePooling1d({ 
    poolSize: 2, 
    strides: 2, 
    padding: 'valid' 
});
      
const output = averagePoolingLayer.apply(input);
  
const model = tf.model({ inputs: input, outputs: output });
  
const x = tf.tensor3d(
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 
    [1, 4, 3]
);
  
model.predict(x).print();


Output:

Tensor
   [[[2.5, 3.5, 4.5 ],
     [8.5, 9.5, 10.5]]]

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!