Open In App

Tensorflow.js tf.Tensor class .array() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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 .array() method is used to return the tensor input like a nested array. Moreover, the relocation of data is made asynchronously.

Syntax:

array()

Parameters: This method does not hold any parameters.

Return Value: It returns the promise of number[].

Example 1:

Javascript




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Creating a tensor
const tn = tf.tensor([5, 6, 3, 4]);
  
// Calling array() method
const res = await tn.array();
  
// Printing output
console.log(res)


Output:

5,6,3,4

Example 2:

Javascript




// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Calling array() method and
// Printing output
console.log(await tf.tensor2d([1.7, 3.7, 9.7, null], [4, 1]).array());


Output:

1.7000000476837158,3.700000047683716,9.699999809265137,0

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


Last Updated : 22 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads