Main Content

shuffle

Shuffle data in minibatchqueue

    Description

    shuffle(mbq) resets the data held in mbq and shuffles it into a random order. After shuffling, the next function returns different mini-batches. Use this syntax to reset and shuffle your data after each training epoch in a custom training loop.

    Examples

    collapse all

    The shuffle function resets and shuffles the minibatchqueue so that you can obtain data from it in a random order. By contrast, the reset function resets the minibatchqueue to the start of the underlying datastore.

    Create a minibatchqueue from a datastore.

    ds = digitDatastore;
    mbq = minibatchqueue(ds,'MinibatchSize',256)
    mbq = 
    minibatchqueue with 1 output and properties:
    
       Mini-batch creation:
               MiniBatchSize: 256
            PartialMiniBatch: 'return'
                MiniBatchFcn: 'collate'
        DispatchInBackground: 0
    
       Outputs:
                  OutputCast: {'single'}
             OutputAsDlarray: 1
             MiniBatchFormat: {''}
           OutputEnvironment: {'auto'}
    

    Obtain the first mini-batch of data.

    X1 = next(mbq);

    Iterate over the rest of the data in the minibatchqueue. Use hasdata to check if data is still available.

    while hasdata(mbq)
        next(mbq);
    end

    Shuffle the minibatchqueue and obtain the first mini-batch after the queue is shuffled.

    shuffle(mbq);
    X2 = next(mbq);

    Iterate over the remaining data again.

    while hasdata(mbq)
        next(mbq);
    end

    Reset the minibatchqueue and obtain the first mini-batch after the queue is reset.

    reset(mbq);
    X3 = next(mbq);

    Check whether the mini-batches obtained after resetting or shuffling the minibatchqueue are the same as the first mini-batch after the minibatchqueue is created.

    isequal(X1,X2)
    isequal(X1,X3)
    ans = 
       0
    ans = 
       1
    

    The reset function returns the minibatchqueue to the start of the underlying data, so that the next function returns mini-batches in the same order each time. By contrast, the shuffle function shuffles the underlying data and produces randomized mini-batches.

    Input Arguments

    collapse all

    Queue of mini-batches, specified as a minibatchqueue object.

    Introduced in R2020b