# Using variable sized input - Is padding required?

**URL:** <https://discuss.pytorch.org/t/using-variable-sized-input-is-padding-required/18131>\
**Category:** Uncategorized\
**Created:** [May 15, 2018, 10:05am UTC](https://discuss.pytorch.org/t/using-variable-sized-input-is-padding-required/18131 "2018-05-15T10:05:05Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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:11pm UTC](https://discuss.pytorch.org/t/using-variable-sized-input-is-padding-required/18131/7 "2019-07-25T18:11:57Z")

</div>

My take on how to solve this issue:

```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

```

* * *

There seems to be a large collection of posts all over pytorch that makes it difficult to solve this issue. I have collected a list of all of them hopefully making things easier for all of us. Here:

- [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)

* * *

Also, Stack-overflow has a version of this question too:

> <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/using-variable-sized-input-is-padding-required/18131)._
