I’m noticing that initializing any random model, and not using that random model anywhere further in the code, is causing variations in the loss and AUC as compared to not initializing that random model.
For instance, in the below code snippet, just initializing the unused auxiliary model model_aux is causing variations in the loss and AUC of model.
This seems strange.
Did you set the seed correctly to ensure a fair comparison?
I would also think that the data loading order might also create such differences. That to say, in case you used a random sampler for your dataloader with shuffle=True rather than a custom sampler class, the samples included in the batches for each run shall be different and might be a potential reason for this difference.
Did you checked this difference multiple times and if yes, is this difference seen every time?
I tried setting shuffle=False, yet a significant difference persists.
Yes, I checked this multiple times and the difference is seen every time.
Also, the difference varies with altering the architecture of the auxiliary model.
Almost certainly you are using some sort of randomization in your training
and initializing model_aux is consuming some random numbers, putting
your training at a different point in the (pseudo)random-number stream.
I know you said that you’ve set shuffle = False, but there could be other
sources of randomization such as Dropout layers or random transformations
such as RandomCrop if you are using augmentation.
Try this experiment:
# to make runs repeatable
model = Model(n_heads, num_layers, device)
# either do or don't instantiate Model_aux
model_aux = Model_aux(n_heads_aux, num_layers_aux, device)
# set random-number stream to the same well-defined state
# regardless of whether Model_aux was instantiated
# run some training ...
First verify that your runs are repeatable when you perform multiple runs withoutModel_aux.
Assuming this test passes, try running withModel_aux (making sure to
include the second call to torch.manual_seed (98765)) and see if you
continue to get the same results.
@KFrank, Thank you very much for the help!
Setting the random seed again after instantiating Model_aux has resolved the issue.
The loss and AUC score now remain the same regardless of Model_aux 's initialization.