Tensorflow.js tf.data.Dataset.filter() 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 in Node.js.

The tf.data.Dataset.filter() function is used to filter the dataset according to predicate.

Syntax:

filter(predicate)

Parameters:

  • predicate: A function mapping a dataset element to a boolean or a Promise for one.

Return Value: It returns tf.data.Dataset

Example 1:

Javascript




const gfg = tf.data.array([8, 12, 25, 6, 34, 2, 67, 43])
   .filter(x => x >= 10);
await gfg.forEachAsync(Beginner => console.log(Beginner));


Output:

12
25
34
67
43

Example 2:

Javascript




const gfg = tf.data.array([10, 21, 36, 24, 54, 65, 72, 90, 95, 12])
   .filter(x => x%6 === 0);
await gfg.forEachAsync(Beginner => console.log(Beginner));


Output:

36
24
54
72
90
12

Reference: https://js.tensorflow.org/api/latest/#tf.data.Dataset.filter