Saving split dataset

Kamil_Adamczewski: <p>We can divide a dataset by means of torch.utils.data.random_split. However, for reproduction of the results, is it possible to save the split datasets to load them later?</p>

ptrblck: <p>You could use a seed for the random number generator (<code>torch.manual_seed</code>) and make sure the split is the same every time.<br>
Alternatively, you could split the sample indices, store each index tensor locally via <code>torch.save</code>, and use it in <code>Subset</code>.</p>

Kamil_Adamczewski: <p>Thank you ptrblck for great answer, as always. It does work but I stumbled upon a strange issue.</p>
<p>I split my training set into training and validation set using a deterministic seed as mentioned:</p>
<pre><code class="lang-auto">torch.manual_seed(0)
train_dataset, val_dataset = torch.utils.data.random_split(trainval_dataset, [train_size, val_size])
</code></pre>
<p>I wanted to test the CNN then on a validation set (using torchvision CIFAR10). When I test it on a testset, the accuracy is always the same as expected. However, when I test it on validation set, the accuracy changes. When delving into the code, I realized the problem is not with the validation set (whose targets are the same every time I run an instance of a script) but actually the network spits different outputs.</p>
<p>How is it possible? Especially, that it appears when feeding the validation set but not a test set?</p>

ptrblck: <p>Are you calling <code>model.eval()</code> when checking the accuracy on the validation set?</p>

Kamil_Adamczewski: <p>Yes, I am. I also just tried <code>model.train()</code> for the sake of completeness and the same erratic behavior happens.</p>

ptrblck: <p>To understand the issue completely: you are calling <code>model.eval()</code> and check the accuracy on the validation set. The validation set indices (passed to <code>Subset</code>) are definitely the same, but the accuracy changes for sequential runs?</p>

Kamil_Adamczewski: <p>Precisely. Here are examples of two different runs. I show the output of the same batch and print respectively:</p>
<pre><code>   print(predicted.eq(targets).sum().item())
   print(predicted.eq(targets))
   print(predicted)
   print(targets)
</code></pre>
<p>Targets are the same, but predicted values slightly vary.</p>
<p>Run 1:</p>
<hr>
<p>118<br>
tensor([1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1,<br>
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,<br>
1, 1, 1,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2, 4, 1, 1, 4, 8, 2, 9, 7, 6, 9, 1, 2, 9, 1, 1, 5, 1, 7, 7, 9, 4, 3, 3,<br>
4, 6, 0, 5, 5, 5, 7, 7, 0, 7, 0, 4, 7, 3, 6, 1, 5, 0, 4, 0, 3, 1, 4, 8,<br>
7, 6, 3, 7, 2, 5, 3, 0, 8, 5, 0, 2, 9, 7, 2, 3, 2, 8, 9, 6, 1, 1, 9, 1,<br>
4, 9, 4, 8, 7, 5, 4, 7, 7, 8, 0, 6, 7, 4, 7, 5, 8, 6, 1, 3, 1, 8, 5, 8,<br>
4, 2, 3, 7, 7, 2, 5, 1], device=‘cuda:0’)</p>

ptrblck: <p>Were you able to exactly reproduce the same model parameters for your runs?<br>
I.e. did you compare the <code>state_dict</code>s before running the validation loop?<br>
Even if you are seeding and getting the same data samples for your runs, the result might still differ, e.g. due to cudnn as described in the <a href="https://pytorch.org/docs/stable/notes/randomness.html" rel="nofollow noopener">Reproducibility docs</a>.</p>

Kamil_Adamczewski: <p>I load the same pre-trained model in both cases. In the case of running this model on the test set, the output is always the same. Do you think there still can be any room for such nondeterministic behavior?</p>

ptrblck: <p>Did you follow the advice from the reproducibility docs?<br>
Are you using and random transformations in your <code>Dataset</code> for the validation set?</p>