# Shuffle batch retrieval but not values within batch

**URL:** <https://discuss.pytorch.org/t/shuffle-batch-retrieval-but-not-values-within-batch/186449>\
**Category:** data\
**Created:** [August 16, 2023, 3:50am UTC](https://discuss.pytorch.org/t/shuffle-batch-retrieval-but-not-values-within-batch/186449 "2023-08-16T03:50:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gursi261](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gursi261/32/63205_2.png) [@gursi261](https://discuss.pytorch.org/u/gursi261)\
**Post date:** [August 16, 2023, 3:50am UTC](https://discuss.pytorch.org/t/shuffle-batch-retrieval-but-not-values-within-batch/186449/1 "2023-08-16T03:50:45Z")

</div>

I am working on a machine translation dataset and the input entries are sorted from the shortest sequence to the longest sequence. I pad them to the max length in each batch using collate\_fn.

Is there a way to make batch creation unshuffled so that similar length entries end up in the same batch (since the data is sorted by length), but still shuffle the retrieval of batches themselves?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [August 16, 2023, 2:47pm UTC](https://discuss.pytorch.org/t/shuffle-batch-retrieval-but-not-values-within-batch/186449/2 "2023-08-16T14:47:47Z")

</div>

Take a look at e.g. [this post](https://discuss.pytorch.org/t/gru-and-padded-sequences-tipps-and-tricks/90729/3) from @vdw where he shared a similar approach.

---

<div class="post-metadata">

**Author:** ![gursi261](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gursi261/32/63205_2.png) [@gursi261](https://discuss.pytorch.org/u/gursi261)\
**Post date:** [August 16, 2023, 11:46pm UTC](https://discuss.pytorch.org/t/shuffle-batch-retrieval-but-not-values-within-batch/186449/3 "2023-08-16T23:46:45Z")

</div>

I see, thanks for your reply!

---

<div class="post-metadata">

**Author:** ![vdw](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vdw/32/10074_2.png) [@vdw](https://discuss.pytorch.org/u/vdw)\
**Post date:** [August 17, 2023, 12:52am UTC](https://discuss.pytorch.org/t/shuffle-batch-retrieval-but-not-values-within-batch/186449/4 "2023-08-17T00:52:58Z")

</div>

This [post](https://discuss.pytorch.org/t/using-batches-for-seq2seq-models/55205) is more specifically for Seq2Seq models like machine translation.

---

<div class="post-metadata">

**Author:** ![gursi261](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gursi261/32/63205_2.png) [@gursi261](https://discuss.pytorch.org/u/gursi261)\
**Post date:** [August 17, 2023, 9:53am UTC](https://discuss.pytorch.org/t/shuffle-batch-retrieval-but-not-values-within-batch/186449/5 "2023-08-17T09:53:08Z")

</div>

Got it, thanks for the answer!
