Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.data.Dataset class .forEachAsync() Method

Tensorflow.js tf.data.Dataset class .forEachAsync() Method

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.forEachAsync() function is used after applying a function to each and every element of the dataset. We can use this method to print an array, modifying the values of the array etc.

Syntax:

forEachAsync(f)

Parameters:

  • f: It accepts a function which is applied to each and every element of the dataset

Return Value: It returns a promise

Example 1:

Javascript




// Creating an array
const arr = tf.data.array([10, 20, 30, 40, 50]);
  
// Applying the function on the array
await arr.forEachAsync(element => console.log(element));


Output:

10
20
30
40
50

Example 2: In this example we will pass a function that calculates the square of an element.

Javascript




// Creating an array
const arr = tf.data.array([1, 2, 3, 4, 5]);
  
// Applying the function on the array
await arr.forEachAsync(element => console.log(element*element));


Output:

1
4
9
16
25

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

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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments