Skip to content
Related Articles

Related Articles

Tensorflow.js tf.util.flatten() Function

Improve Article
Save Article
  • Last Updated : 18 May, 2021
Improve Article
Save Article

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.

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
Related Articles

Start Your Coding Journey Now!