Open In App

Tensorflow.js tf.data.array() Method

Last Updated : 04 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 .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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads