Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Tensorflow.js tf.util.flatten() Function

  • Last Updated : 18 May, 2021

Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .util.flatten() function is used to flatten an inconsistent array that is nested.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax :  

tf.util.flatten(arr, result?, skipTypedArray?)

Parameters:  



  • arr: It is the stated nested array which is to be flattened. It can be of type number, Boolean, string, Promise<number>, TypedArray, RecursiveArray, or TypedArray>.
  • result: It is the stated destination array that carries the elements. It is optional parameter and can be of type number, Boolean, string, Promise<number>, TypedArray[].
  • skipTypedArray: It is the optional parameter which prevent the flattening of typed arrays. And the by default value of it is false.

Return Value: It can return number, Boolean, string, Promise<number>, or TypedArray[].

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining nested array
const arr = [[11, 12], [13, 14], [15, [16, [17]]]];
  
// Calling tf.util.flatten() method
const res = tf.util.flatten(arr);
  
// printing output
console.log(res);

Output:

11,12,13,14,15,16,17

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining nested array
const arr = [[11, 12], [13, 14], [15, [16, [17]]]];
  
// Defining destination array
const des_arr = [9, 10]
  
// Calling tf.util.flatten() method with
// all its parameters
const res = tf.util.flatten(arr, des_arr, true);
  
// printing output
console.log(res);

Output:

9,10,11,12,13,14,15,16,17

Reference:https://js.tensorflow.org/api/1.0.0/#flatten




My Personal Notes arrow_drop_up
Recommended Articles
Page :