Saturday, February 7, 2026
HomeLanguagesJavascriptTensorflow.js tf.data.Dataset class .take() Method

Tensorflow.js tf.data.Dataset class .take() Method

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

The .take() method is used to form a dataset with maximum count foremost items out of the stated dataset.

Syntax:

take(count)

Parameters:  

  • count: It is the number of elements present in the stated dataset that is to be utilized to create the different dataset. Moreover, in case the count is not defined or else negative or is higher than the dimension of the stated dataset then the newly created dataset will hold each item of the given dataset.

Return Value: It returns tf.data.Dataset.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining dataset formed of an array of
// numbers and calling take() method
const res = tf.data.array([11, 12, 31, 43, 15, 64]).take(4);
  
// Calling forEachAsync() method and
// Printing output
await res.forEachAsync(op => console.log(op));


Output:

11
12
31
43 

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling forEachAsync(), take() method 
// and printing output
await tf.data.array([31.1, 81.2, 5.1, 0, NaN, 'a']).
take(10.7).forEachAsync(op => console.log(op));


Output:

31.1
81.2
5.1
0
NaN
a 

Reference: https://js.tensorflow.org/api/latest/#tf.data.Dataset.take

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

Dominic
32492 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6863 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12079 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7239 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6934 POSTS0 COMMENTS