Main Content

shuffle

Shuffle data in minibatchqueue

Since R2020b

    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 object so that you can obtain data from it in a random order. By contrast, the reset function resets the minibatchqueue object to the start of the underlying datastore.

    Create a minibatchqueue object 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'
        PreprocessingEnvironment: 'serial'
    
       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 object. Use hasdata to check if data is still available.

    while hasdata(mbq)
        next(mbq);
    end

    Shuffle the minibatchqueue object 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 object 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 object are the same as the first mini-batch after the minibatchqueue object is created.

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

    The reset function returns the minibatchqueue object 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

    Mini-batch queue, specified as a minibatchqueue object.

    Version History

    Introduced in R2020b