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
import * as tf from "@tensorflow/tfjs"
const arr = [[11, 12], [13, 14], [15, [16, [17]]]];
const res = tf.util.flatten(arr);
console.log(res);
|
Output:
11,12,13,14,15,16,17
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
const arr = [[11, 12], [13, 14], [15, [16, [17]]]];
const des_arr = [9, 10]
const res = tf.util.flatten(arr, des_arr, true );
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,
GeeksforGeeks 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!
Last Updated :
18 May, 2021
Like Article
Save Article