Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js class .shuffle() Method

  • Last Updated : 01 Jul, 2021

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

The method randomly shuffles a tensor along its first dimension.

    buffer_size, seed=None, 


  • buffer_size: This is the number of elements from which the new dataset will be sampled.
  • seed[optional]: It is an optional parameter used to create a random seed for the distribution, to see the same results use same seed.
  • reshuffle_each_iteration: A Boolean, which is true indicates that the dataset should be pseudo randomly reshuffled each time it is iterated over. Default value is true. It is Optional parameter.

Return Value: A tensor with same shape and data type as value, but shuffled along its first dimension.

Example 1: In this example first we will create a tensor and then shuffle it, In this example reshuffle_each_iteration is True


async function shuffle() {
    // Creating a Tensor
    const a =[1, 2, 3, 4, 5, 6]).shuffle(3);
    await a.forEachAsync(e => console.log(e)); //print 1
    await a.forEachAsync(e => console.log(e)); //print 2


3 4 1 2 5 6
3 4 2 5 6 1

Example 2: In this example, seed is set to an Integer, whenever a specific integer is used it will generate that specific output


async function shuffleseed() {
    const a =[1, 2, 3]).shuffle(3, seed = 42);
    await a.forEachAsync(e => console.log(e));
    const b =[1, 2, 3]).shuffle(3, seed = 42);
    await b.forEachAsync(e => console.log(e));


2 1 3
2 1 3


My Personal Notes arrow_drop_up
Recommended Articles
Page :