How to load only the first 500 samples from dataloader

I have implemented this code below:

for idx, (data, target, meta) in list(enumerate(tqdm(dataloader_test)))[:500]:

but it still loaded the entire dataset instead of the first 500. How do we limit the number of samples to load?

Thank you in advance.

This is expected since you are slicing the list after creating it.
You could create the iterator via loader_iter = iter(dataloader_test) and iterate it 500 times:

for _ in range(500):
    data, target, meta = next(iter)

To be safe you could also wrap it into a try block and catch the StopIteration exception.

Hello @ptrblck , I tried creating an iterator and editing my for loop like this:

    loader_iter = iter(dataloader_test)
    
    
    for _ in range(500):
        idx, (data, target, meta) = next(iter)

        for d, t, m in zip(data, target, meta):
            d, t = map(lambda x: x.to(device), (d, t))

but I get this error:


    idx, (data, target, meta) = next(iter)

TypeError: 'builtin_function_or_method' object is not an iterator

my dataloader have batch_size by default so I can’t remove idx

Sorry, my second code snippet used bad variable names. You should use next(loader_iter), i.e. call it on the actual iterator object.