Wednesday, July 3, 2024
HomeLanguagesJavascriptTensorflow.js tf.data.array() Method

Tensorflow.js tf.data.array() Method

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 .data.array() method is used to form a dataset based on an array made from elements.

Syntax :  

tf.data.array(items)

Parameters:  

  • items: It is the stated array made from elements that is to be parsed in a dataset like items, and it can be of type tf.void, number, string, TypedArray, tf.Tensor, tf.Tensor[], or {[key: string]:tf.Tensor, number, or string}[].

Return Value: It returns tf.data.Dataset.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining dataset formed of an array
// of objects and calling data.array()
const res = tf.data.array([
    {'element': 5}, 
    {'element': 6}, 
    {'element': 7}
]);
  
// Calling forEachAsync() method and
// Printing output
await res.forEachAsync(op => console.log(op));


Output:

{
  "element": 5
}
{
  "element": 6
}
{
  "element": 7
}

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining dataset formed of an array
// of numbers and calling data.array()
const res = tf.data.array([4.6, 7.9, 9.6, 2.6, 8.9]);
  
// Calling forEachAsync() method and
// Printing output
await res.forEachAsync(op => console.log(op));


Output:

4.6
7.9
9.6
2.6
8.9

Reference: https://js.tensorflow.org/api/latest/#data.array

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!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments