How do you get data of next iteration when shuffle=True?

Hi
I’m trying to get data for the next iteration in the dataloader loop.
The point is I need both data for the current iteration and the one for the next iteration or even for n times the next iteration.
So I did following.

for num_iter, (img, label) in enumerate(train_loader):
        train_loader_list = list(train_loader)
        tuple = train_loader_list[num_iter]

I was going to get data for the next iteration by doing train_loader_list[num_iter+1]
but tuple[0] and img were different!
I think it’s because of shuffle=True.
In this case, what’s the most efficient way to get both data for the current iteration and the one for the n times next iteration?

If you think the data for the current iteration and next iteration should be the same, why not just use the same data.

data_current_iteration = next(train_loader)
data_next_iteration = data_current_iteration

Or do I misunderstand your question?

I am not sure if I understand your question correctly or not but I’d like to mention few things that I think might be helpful.

  1. Basically, when you set shuffle equal to True your dataset indices will change every epoch and you won’t get the same instances in two consecutive epochs since you reshuffled at every epoch. Also, your dataloader should give you the correct (image, label) pairs and if it doesn’t seem right, please check the output of your dataset one more time.
  2. If you need more than one instance for your calculation at each iteration, I would suggest changing the __getitem__ function of your dataset. (It is not a good idea if you are just trying to update the weights less frequently or training a GAN , …)
  3. Here is another possible way but I cannot say if it’s the most efficient way for your case or not.
for curr_iter in range(num_iter): 
        i = 0 
        while i < number_of_iterations_you_need:
              data_iter = next(iter(train_loader))
              #do anything you want
              i +=1