Tensorflow.js tf.layers.masking() 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. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or Node.js.
The tf.layers.masking() function is used to mask a sequence by using a mask value just to skip time steps.
Syntax:
tf.layers.masking(arguments)
Parameters
- inputShape : It is an optional parameter which is used to create the input layer, and it takes values like number and null.
- batchInputShape : It is an optional parameter which is used to create the input layer before the main layer, and it takes values like number and null.
- batchSize : It is an optional parameter used to make batchInputShape, and, and it accepts only numbers.
- dtype : It is an optional parameter, and it stands for data type. By default, it has ‘float32’ and also supports other values like ‘int32’, ‘bool’ etc.
- name: It is an optional parameter and is used to define the name of the layer, and it accepts strings.
- trainable : It is an optional parameter that determines the provided input layers are updated or not. It accepts boolean values.
- weights : It possesses the starting weights of the layer. It is also an optional parameter.
- inputDType : It is an optional parameter used for input data type. Like dtype it also supports all its values.
Return Value: It returns Masking.
Example 1:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs"// Initializing the 1d tensorconst geek= tf.tensor1d([1, 2, 3, 4]);// Reshaping tensorconst geek1 = tf.reshape(geek,[2,2]);// Creating masking object of poolSize 2*2const mask = tf.layers.masking({poolSize:[2,2]});// Applying masking on img1 tensorconst result=mask.apply(geek1);// Printing the result tensorresult.print(); |
Output:
Tensor
[[1, 2],
[3, 4]]
Example 2:
Javascript
// Importing the tensorflow.js libraryimport * as tf from "@tensorflow/tfjs"// Reshaping tensorconst geek1 = tf.reshape(tf.tensor1d([5, 6, 7, 8]),[2,2]);// Applying masking on geek1 tensortf.layers.masking( { poolSize:[2,2] }).apply( geek1).print(); |
Output:
Tensor
[[5, 6],
[7, 8]]
Reference: https://js.tensorflow.org/api/3.6.0/#layers.masking
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



