Saturday, December 13, 2025
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!
RELATED ARTICLES

Most Popular

Dominic
32446 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12030 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6896 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS