Open In App

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:  



Return Value: It returns tf.data.Dataset.

Example 1:  




// 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:




// 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

Article Tags :