Why data is discarded in shuffle operation when training a deep network?

3 views (last 30 days)
When training a deep learning network, if the batch size does not evenly divide the number of training samples, then the training data that does not fit into the final batch of each epoch is discarded. Why this limitation? why part of the training data is discarded?
Setting the shuffle training option to "every-epoch" does not prevent discarding data, it just avoid discarding the same data every epoch.

Answers (1)

Aravind
Aravind on 30 Jan 2025
When training deep learning networks in MATLAB, if the batch size does not evenly divide the number of training samples, the leftover data that cannot fill a complete batch at the end of each epoch will be discarded. This is explained in the documentation here: https://www.mathworks.com/help/releases/R2022a/deeplearning/ref/trainingoptions.html#d123e146068. Under the “Shuffle” option, it is recommended to set the value to “every-epoch” to avoid discarding the same data each time.
Here are some reasons for this behavior:
  • Batch Processing Consistency: MATLAB's deep learning framework, similar to others, is optimized for processing batches of a consistent size, which enhances computational efficiency and fully utilizes parallel processing capabilities, particularly on GPUs.
  • Gradient Estimation Stability: Inconsistent, smaller batch sizes can lead to higher variance in gradient estimates, which can destabilize the convergence process during training and potentially result in less reliable learning outcomes.
This approach balances computational efficiency with the use of all available data during training.
To ensure no data is discarded, you can use a custom training loop. You can define a “minibatchqueue” object with your input data to create mini-batches. By setting the “PartialMiniBatch” option to “return”, you ensure that even if the number of observations is not divisible by the mini-batch size, no data is lost, as the final mini-batch will contain fewer observations. You can find more information about this here. You can also refer to this example on how to train a network using a custom training loop: https://www.mathworks.com/help/releases/R2022a/deeplearning/ug/train-network-using-custom-training-loop.html.
I hope this answers your question.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!