Open In App

Tensorflow.js tf.booleanMaskAsync() Function

Last Updated : 01 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads