# Create a dataloader using a list of targets and a list of tensors as data

**URL:** <https://discuss.pytorch.org/t/create-a-dataloader-using-a-list-of-targets-and-a-list-of-tensors-as-data/88242>\
**Category:** vision\
**Created:** [July 7, 2020, 1:54pm UTC](https://discuss.pytorch.org/t/create-a-dataloader-using-a-list-of-targets-and-a-list-of-tensors-as-data/88242 "2020-07-07T13:54:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SandPhoenix](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sandphoenix/32/29137_2.png) [@SandPhoenix](https://discuss.pytorch.org/u/SandPhoenix)\
**Post date:** [July 7, 2020, 1:54pm UTC](https://discuss.pytorch.org/t/create-a-dataloader-using-a-list-of-targets-and-a-list-of-tensors-as-data/88242/1 "2020-07-07T13:54:30Z")

</div>

Hello everyone,

So, I am working on a small project and I am kind of stuck for like 2 hours now on a thing that seems simple so I would be very thankful if anyone can help.  
So, I have a list of tensors that I called new\_images and a list of labels.  
I want to create a dataloader using these two.  
So, I tried several things like for example a dumb list of tuples but it did not work. It showed an error but I ignored it because I know that this kind of solution would not work.  
I tried creating a class dataset like this:

```auto
class DataSetCos(torch.utils.data.Dataset):
    def __init__ (self, nbr_samples, new_images, new_labels, transform=None):
        self.nbr_samples=nbr_samples
        self.labels=new_labels
        self.transform = transform
    def __len__ (self):
        return self.nbr_samples
    def __getitem__ (self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()
        sample = new_images[idx]
        if self.transform:
            sample = self.transform(sample)
        if (sample.float().size()[0] == 3):
            return [sample.float(), self.labels[idx]]

```

after that I loaded it like this:

```auto
dataset= DataSetCos(nbr_samples=100, new_images=new_images, new_labels=new_labels, transform =None)
dataloader= torch.utils.data.DataLoader(dataset,batch_size=batch_size,shuffle=False)

```

but when I try testing my model, I get this error IndexError: list index out of range.

---

<div class="post-metadata">

**Author:** ![Nikronic](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikronic/32/71873_2.png) [@Nikronic](https://discuss.pytorch.org/u/Nikronic)\
**Post date:** [July 8, 2020, 5:22am UTC](https://discuss.pytorch.org/t/create-a-dataloader-using-a-list-of-targets-and-a-list-of-tensors-as-data/88242/2 "2020-07-08T05:22:22Z")

</div>

Hi,

It is really hard to say what the exact problem is but it seems data loader is generating indices that is out of bound for your lists. I think changing ` __len__ ` can help. Try this:

```auto

    def __len__ (self):
        return len(self.new_images)

```

Also, can you let me know the shape of `new_image` and `new_labels`?

Bests

---

<div class="post-metadata">

**Author:** ![SandPhoenix](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sandphoenix/32/29137_2.png) [@SandPhoenix](https://discuss.pytorch.org/u/SandPhoenix)\
**Post date:** [July 8, 2020, 9:38pm UTC](https://discuss.pytorch.org/t/create-a-dataloader-using-a-list-of-targets-and-a-list-of-tensors-as-data/88242/3 "2020-07-08T21:38:46Z")

</div>

Hi thx for your answer!  
I will try doing that.  
For the shapes, the new\_images is a list of tensors of len 100 and same thing for the new\_labels
