Open In App

Tensorflow.js tf.layers.maxPooling2d() 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.maxPooling2d() function is used to apply max pooling operation on spatial data.



Syntax: 

tf.layers.maxPooling2d (args)

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



Returns: It returns on object (MaxPooling2D).

Example 1:




import * as tf from "@tensorflow/tfjs";
  
const input = tf.input({ shape: [4, 4, 4] });
const maxPoolingLayer = tf.layers.maxPooling2d({ poolSize: [2, 2] });
const output = maxPoolingLayer.apply(input);
  
const model = tf.model({ inputs: input, outputs: output });
model.predict(tf.ones([1, 4, 4, 4])).print();

Output:

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

Example 2:




import * as tf from "@tensorflow/tfjs";
  
const input = tf.input({ shape: [4, 4, 1] });
  
const maxPoolingLayer = 
    tf.layers.maxPooling2d({ poolSize: [3, 3] });
      
const output = maxPoolingLayer.apply(input);
  
const model = tf.model({ inputs: input, outputs: output });
  
const x = tf.tensor4d([1, 2, 3, 4, 5, 6, 7, 
    8, 9, 10, 11, 12, 13, 14, 15, 16], [1, 4, 4, 1]);
  
model.predict(x).print();

Output:

Tensor
    [ [ [[11],]]]

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


Article Tags :