# Invalid argument tensors for dataloader pytoch

**URL:** <https://discuss.pytorch.org/t/invalid-argument-tensors-for-dataloader-pytoch/55449>\
**Category:** vision\
**Created:** [September 8, 2019, 7:05am UTC](https://discuss.pytorch.org/t/invalid-argument-tensors-for-dataloader-pytoch/55449 "2019-09-08T07:05:46Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![spanev](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/spanev/32/14892_2.png) [@spanev](https://discuss.pytorch.org/u/spanev)\
**Post date:** [September 8, 2019, 3:07pm UTC](https://discuss.pytorch.org/t/invalid-argument-tensors-for-dataloader-pytoch/55449/2 "2019-09-08T15:07:18Z")

</div>

Hi @Arta_A,

You can’t directly create a batch of samples with different sizes because the Dataloader try to collate/glue them with `torch.stack` (which requires [“All tensors need to be of the same size.”](https://pytorch.org/docs/stable/torch.html#torch.stack)).

> [@Arta\_A](#):
>
> —\> 43 return torch.stack(batch, 0, out=out)

The workaround consists in providing a custom collation function (through the Dataloader `collate_fn` arg) that will (e.g.) create the batch as a Python list (or anything you want) rather than a Tensor.

Please find a detailed example in this thread [How to create a dataloader with variable-size input - #3 by jdhao](https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278/3).

---

_[View the full topic](https://discuss.pytorch.org/t/invalid-argument-tensors-for-dataloader-pytoch/55449)._
