Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.util.flatten() Function

Tensorflow.js tf.util.flatten() Function

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments