Thursday, July 4, 2024
HomeLanguagesJavascriptTensorflow.js tf.booleanMaskAsync() Function

Tensorflow.js tf.booleanMaskAsync() Function

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

The .booleanMaskAsync() function is used to implement boolean mask to the stated input tensor.

Syntax :  

tf.booleanMaskAsync(tensor, mask, axis?)

Parameters:  

  • tensor: It is the stated N-D tensor and it can be of type tf.Tensor, TypedArray, or Array.
  • mask: It is the stated K-D boolean tensor. Where, K <= N plus K should be inactively recognized. It can be of type tf.Tensor, TypedArray, or Array.
  • axis: It is an optional parameter of type number. It is a 0-D integer type tensor that represents the axis in the stated tensor that is to be masked from. Here, the by default value is zero that masks from the first size, else (K + axis <= N).

Return Value: It returns Promise(tf.Tensor).

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input and mask
const tn = tf.tensor2d(
    [7, 8, 9, 10, 11, 12, 5, 62], [4, 2]);
const msk = tf.tensor1d([2, 1, 2, 3], 'bool');
  
// Calling tf.booleanMaskAsync() method and
// Printing output
const res = await tf.booleanMaskAsync(tn, msk);
res.print();


Output:

Tensor
    [[7 , 8 ],
     [9 , 10],
     [11, 12],
     [5 , 62]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.booleanMaskAsync() method and
// Printing output
const res = await tf.booleanMaskAsync(
    tf.tensor2d([34, 17, 7, 8], [2, 2]), 
    tf.tensor1d([1, 1], 'bool'), 1);
      
res.print();


Output:

Tensor
    [[34, 17],
     [7 , 8 ]]

Reference: https://js.tensorflow.org/api/latest/#booleanMaskAsync

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!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments