Why does Pytorch DataLoader output a list?

59 Views Asked by At

So as I was programming and skimming through the pytorch docs, I stumbled across DataLoader. I learnt a fair bit and continued researching, and then saw a comment on a youtube video covering it, stating that DataLoader actually outputs a list instead of a tensor.

I then used the type() on the iterable data and learnt that this was true, could anyone please help me understand why the DataLoader outputs a list instead of a tensor.

1

There are 1 best solutions below

0
Karl On

The pytorch DataLoader class has a collate_fn that processes dataset items into a batch. Using the example from the pytorch documentation, it works like this:

for indices in batch_sampler:
    yield collate_fn([dataset[i] for i in indices])

If you don't pass a collate_fn, pytorch automatically uses default_collate. The behavior of default_collate depends on the types from you dataset, defined here.