Calling enumerate on dataloader gives list instead of tensor

Hi, I have created a dataloader object from a subsetted dataset as:

target_index = np.random.choice(len(target_dataset), k_samp, replace= True)
target_dataset =, target_index)
target_loader =, batch_size=batch_size,
                                         shuffle=True, num_workers=workers)

When I enumerate over this dataloader as follows, the type for data is actually a list and not tensor

for i, data in enumerate(target_loader, 0):

I created my target_dataset as

target_dataset = dset.ImageFolder(root=target_dataroot,
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),

I double checked and the contents of my target_dataroot are in fact images.

Not sure why I am getting a list when iterating over the dataloader instead of tensors?

Tried it with a few other subsetted datasets and iterating over those give me tensors.


ImageFolder will return a data and target tensor. You are currently assigning both return values to data in the DataLoader loop. If you want to use both objects as tensors, you could either use two return variables via:

for i, (data, target) in enumerate(target_loader, 0):

or unwrap them inside the loop:

for i, data in enumerate(target_loader, 0):
    x, y = data

Thanks, that fixed it.

Although, I find it strange that I have another dataset set up the same way using ImageFolder and I am not having this issue with that dataset.

What causes this issue to occur?

This shouldn’t be the case, as it would indicate that the other ImageFolder approach would return a single tensor (instead of the data and target tensors).
Could you post a code snippet to reproduce this behavior?

I think I fixed it! I had to do

images = data[0].to(device)

after enumerating over the dataloader! That fixed the problem if it being a list and not a tensor