Don’t know if you still need this. I currently encounter the same issue, and find the torchtext library helpful. You can refer to the document here 3 for BucketIterator. It can pad the sequence with 0 at the end so sequence in the same minibatch will have same length. Besides that, you can set the Field 1 object with " include_lengths=True", then the iterator will give you original length (before padding) of each sequence. These length information can then be utilized in Pytorch’s “torch.nn.utils.rnn.pack_padded_sequence()”, and “torch.nn.utils.rnn.pad_packed_sequence()”.