Shuffle=True or Shuffle=False for val and test dataloaders

OBouldjedri: <p>I was confused if I should set Shuffle= True for test data loadet and val data loader as it is the case in train data loader ?</p>
<p>train_loader = torch.utils.data.DataLoader(train_dataset, batch_size = BATCH_SIZE, shuffle = True)</p>
<p>valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size = BATCH_SIZE)</p>
<p>test_loader = torch.utils.data.DataLoader(test_dataset, batch_size = BATCH_SIZE)</p>
<p>or</p>
<p>train_loader = torch.utils.data.DataLoader(train_dataset, batch_size = BATCH_SIZE, shuffle = True)</p>
<p>valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size = BATCH_SIZE, shuffle = True)</p>
<p>test_loader = torch.utils.data.DataLoader(test_dataset, batch_size = BATCH_SIZE, shuffle = True)</p>
<p>and how this would have an impact on the performences ?</p>

ptrblck: <p>You don’t need to shuffle the validation and test datasets, since no training is done, the model is used in <code>model.eval()</code> and thus the order of samples won’t change the results.</p>

OBouldjedri: <p>I forgot to mention that my data sets structure in train/val/test datasets is set this way:<br>
all samples from class 1, followed by all samples from class2 … ,all samples from class n<br>
does your answear holds even in this case ?</p>

ptrblck: <p>Yes, shuffling would still not be needed in the val/test datasets, since you’ve already split the original dataset into training, validation, test.<br>
Since your samples are ordered, make sure to use a stratified split to create the train/val/test datasets.</p>

OBouldjedri: <p>so shuffle = True or shuffle= false in the val/test loaders would yeild the same results ?<br>
for the second point yes I am sure I have samples from all classes in train/val/test sets.</p>
<p>basicly my problem is my test loss function is not decreasing I am getting some thing like this (which I interpret as ovverfitting) :<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/e/2/e26c0609e787ccbac5e4c59064da160c42e065bc.png" alt="train_accuracy" data-base62-sha1="wj1lyUKl7WjSmJS6Lfb3GFuMaXa" width="432" height="288"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/8/0/8056fbead1ec3de84de444376260bad326a935c5.png" alt="train_loss" data-base62-sha1="ijltG4ZBbnyAYD7ackJCX50PV9H" width="432" height="288"><br>
but on the test loss :<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/5/253a7c87d12c6e4887fb4f668d000dd6f9c43817.png" alt="test_accuracy" data-base62-sha1="5jkYkndwXEtEEm7N4PZDSZsiS2P" width="432" height="288"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/b/5/b5f6f83cb33758bad66291a2260e113a647bbad8.png" alt="test_loss" data-base62-sha1="pXJyeOMPcAFHqaVSfaJ5Xi5fdMI" width="432" height="288"></p>
<p>do you have an idea about what it could be (my model is so basic 4 conv layers and 3 linear layers)?</p>

srishti-git1110: <p>Yes, shuffling the validation/test data will not have any impact on the accuracy, loss etc.<br>
Shuffling is done during the training to make sure we aren’t exposing our model to the same cycle (order) of data in every epoch. It is basically done to ensure the model isn’t adapting its learning to any kind of spurious pattern.<br>
Make sure you aren’t making other errors like <a href="https://stackoverflow.com/questions/65402802/pytorch-shuffle-dataloader?rq=1" rel="noopener nofollow ugc">this</a>.</p>
<p>Hope this helps,<br>
S</p>

Anuj_Chopra: <p>But I have observed that shuffling the TRAIN dataset increases the time taken to load considerably. Is it expected or I am doing something wrong?</p>

ptrblck: <p>Depending on your storage random reads might be more expensive than sequential reads (this should be especially visible using HDDs), which might explain the observed slowdown.<br>
You could profile your storage using e.g. <code>fio</code> as described <a href="https://askubuntu.com/a/991311">here</a>.</p>

Anuj_Chopra: <p>I am using SSD. I have a dataframe which contains filepath of images I have to read. It is not necessary that image of row 1 is just before the image of row 2 on disk.<br>
I believe there is something else which is taking time.</p>

Jessi_Chen: <p>Hi, I am curious that have you solved your probelm? I think the problem may because of batchnormalization layers, did you use them in your model structure? did you shuffle your training dataset during training?</p>

Jessi_Chen: <p>Talking about this case, will it be different when you val/test to no shuffle, with batchnormalization to track_running_stats=True*=False?</p>

ptrblck: <aside class="quote no-group" data-username="Jessi_Chen" data-post="11" data-topic="143720">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jessi_chen/48/63090_2.png" class="avatar"> Jessi_Chen:</div>
<blockquote>
<p>with batchnormalization to track_running_stats=True*=False?</p>
</blockquote>
</aside>
<p>I’m unsure what <code>=True*=False</code> means, but assume you are asking about both use cases?<br>
If you are tracking running stats, calling <code>model.eval()</code> will use these to normalize the data and shuffling during validation is irrelevant for these layers. However, if you do not track running stats, the inputs will be normalized with their own stats during training and validation, so shuffling matters in both cases (as well as the batch size).</p>