# How to do padding based on lengths?

**URL:** <https://discuss.pytorch.org/t/how-to-do-padding-based-on-lengths/24442>\
**Category:** Uncategorized\
**Created:** [September 4, 2018, 1:14am UTC](https://discuss.pytorch.org/t/how-to-do-padding-based-on-lengths/24442 "2018-09-04T01:14:06Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![pinocchio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pinocchio/32/22093_2.png) [@pinocchio](https://discuss.pytorch.org/u/pinocchio)\
**Post date:** [July 25, 2019, 6:16pm UTC](https://discuss.pytorch.org/t/how-to-do-padding-based-on-lengths/24442/10 "2019-07-25T18:16:54Z")

</div>

I used `torch.nn.utils.rnn.pad_sequence` for my dataloader class:

```auto
def collate_fn_padd(batch):
    '''
    Padds batch of variable length

    note: it converts things ToTensor manually here since the ToTensor transform
    assume it takes in images rather than arbitrary tensors.
    '''
    ## get sequence lengths
    lengths = torch.tensor([t.shape[0] for t in batch ]).to(device)
    ## padd
    batch = [torch.Tensor(t).to(device) for t in batch]
    batch = torch.nn.utils.rnn.pad_sequence(batch)
    ## compute mask
    mask = (batch != 0).to(device)
    return batch, lengths, mask

```

* * *

Many Many related posts:

- [How to create batches of a list of varying dimension tensors?](https://discuss.pytorch.org/t/how-to-create-batches-of-a-list-of-varying-dimension-tensors/50773)
- [How to create a dataloader with variable-size input](https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278)
- [Using variable sized input - Is padding required?](https://discuss.pytorch.org/t/using-variable-sized-input-is-padding-required/18131)
- [DataLoader for various length of data](https://discuss.pytorch.org/t/dataloader-for-various-length-of-data/6418)
- [How to do padding based on lengths?](https://discuss.pytorch.org/t/how-to-do-padding-based-on-lengths/24442)

bucketing:

- [Tensorflow-esque bucket by sequence length](https://discuss.pytorch.org/t/tensorflow-esque-bucket-by-sequence-length/41284)

* * *

Even in Stack overflows there is a question about this:

> <https://stackoverflow.com/questions/55041080/how-does-pytorch-dataloader-handle-variable-size-data>

* * *

crossposted: [https://www.quora.com/unanswered/How-does-Pytorch-Dataloader-handle-variable-size-data](https://www.quora.com/unanswered/How-does-Pytorch-Dataloader-handle-variable-size-data)

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-do-padding-based-on-lengths/24442)._
