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:
- poolSize: It is used for downscaling factors in each dimension i.e [vertical, horizontal]. It is an integer or a two-int array is expected.
- strides: In each dimension of the pooling window, the stride size. It is an integer or a two-int array is required.
- padding: For the pooling layer, the padding type to utilize.
- dataFormat: For the pooling layer, the data format to utilize.
- 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.
Returns: It returns on object (MaxPooling2D).
Example 1:
Javascript
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:
Javascript
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