# GRU and padded sequences | Tipps and Tricks

**URL:** <https://discuss.pytorch.org/t/gru-and-padded-sequences-tipps-and-tricks/90729>\
**Category:** nlp\
**Created:** [July 27, 2020, 9:26pm UTC](https://discuss.pytorch.org/t/gru-and-padded-sequences-tipps-and-tricks/90729 "2020-07-27T21:26:20Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [July 28, 2020, 1:05am UTC](https://discuss.pytorch.org/t/gru-and-padded-sequences-tipps-and-tricks/90729/3 "2020-07-28T01:05:40Z")

</div>

> [@Unity05](#):
>
> My first idea was to sort the sequences by their length, so that the total length difference in a padded batch would be as small as possible. Now I’m wondering whether there is an even better way to prevent the sequence padding to falsifie the networks output (by too much).

The [`BucketIterator`](https://torchtext.readthedocs.io/en/latest/data.html#bucketiterator) creates batches with sequences of the same or similar lengths. Or you can create your own Sampler to create batches with sequences of the same length; see [this post as an example](https://discuss.pytorch.org/t/tensorflow-esque-bucket-by-sequence-length/41284/13).

The comments from @googlebot are of course valid, but it’s generally a good idea to avoid batches where the lengths of the sequences vary a great deal.

---

_[View the full topic](https://discuss.pytorch.org/t/gru-and-padded-sequences-tipps-and-tricks/90729)._
