Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.layers.maxPooling1d() Function

Tensorflow.js tf.layers.maxPooling1d() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

Tensorflow.js tf.layers.maxPooling1d() function is used for max pooling operation on temporal data.

Syntax:

tf.layers.maxPooling1d( args );

Parameters:

  • args: It specifies the given config object:
    • poolSize: It is a number or array of numbers. It specifies the size of the windows to pool over.
    • strides: It is a number or array of numbers. It specifies the period at which to sample the pooled values.
    • padding: It should be one of these three values: ‘valid’, ‘same’, and ‘casual’. It specifies how to fill in data that are not an integer multiple of poolSize. 
    • inputSize: It should be null or an array of numbers. It is used for creating the input layer to insert before these layers. 
    • batchInputShape: It should be null or an array of numbers. I defined, it as used for creating the input layer to insert before these layers. batchInputShape have more priority that inputSize, so we prefer batchInputSize over inputSize.
    • batchSize: It should be a number. In the absence of batchInputShape, this field is used to create batchInputShape with inputShape. batchInputShape : [ batchSize ,  …inputShape].
    • dtype: If this layer is used as the input layer, then this field is used as data type for this layer.
    • name: It should be a string type. this field defines the name for this layer.
    • trainable: It should be boolean. This field defines whether the weights of this layer are trainable with fit or not.
    • weights: This should be a tensor that defines the initial weight value for this layer.
    • inputDType: This is the data- type which is used for Legacy support. 

Return Value: It returns MaxPooling1d. 

Example 1: In this example, we will add tf.layers.maxPooling1d() function to sequential model and print summary of the model.

Javascript




import * as tf from "@tensorflow.js/tfjs"
  
const model = tf.sequential();
  
// First layer must have a defined input shape
model.add(tf.layers.maxPooling1d({
    poolSize: 4, 
    strides: 5, 
    padding: 'valid',
    inputShape: [4,3]
}));
  
// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.maxPooling1d({
    poolSize: 4, 
    strides: 5, 
    padding: 'valid'
}));
  
// Printing the summary of model
model.summary();


Output:

​__________________________________________________________________________________________
Layer (type)                Input Shape               Output shape              Param #   
==========================================================================================
max_pooling1d_MaxPooling1D2 [[null,4,3]]              [null,1,3]                0         
__________________________________________________________________________________________
max_pooling1d_MaxPooling1D2 [[null,1,3]]              [null,0,3]                0         
==========================================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
__________________________________________________________________________________________

Example 2:< In this example, we will make the maxPooling1d layer to our model and inspect the model shape.

Javascript




import * as tf from "@tensorflow.js/tfjs"
  
const model = tf.sequential();
  
// First layer must have a defined input shape
model.add(tf.layers.maxPooling1d({
    poolSize: 4, 
    strides: 5, 
    padding: 'valid',
    inputShape: [4,3]
}));
  
// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 3}));
  
model.add(tf.layers.maxPooling1d({
    poolSize: 4, 
    strides: 5, 
    padding: 'valid'
}));
  
// Inspect the inferred shape of the model's output.
console.log(JSON.stringify(model.outputs[0].shape));


Output:

[null,0,3]

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

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!

RELATED ARTICLES

Most Popular

Recent Comments