Fixing manual_seed for validation but not in training

I want to validate my model using only a fixed subset of the whole dataset without affecting the training loop. I want my code to look something like

for epoch in range(num_epochs):

for batch in train_dataloader:
     train_step()  #  loss, optimize, etc ..

torch.manual_seed(0) # This fixes the training data too!

for repetition in range(segments_per_speaker) # sampling multiple segments per speaker
    for batch in valid_dataloader:
         valid_step() # choose best model

Placing torch.manual_seed(0) after the training loop somehow fixes the training data in different epochs. What am I missing? Any recommendations to solve my issue?

Thanks in advance!

torch.manual_seed is used globally, so you would reseed the code each time you are calling this method.
I think the proper way would be to create the fixed subset for the validation once and reuse it in each iteration.

1 Like

Thanks a lot for your answer @ptrblck.

I was hoping to find a workaround not to fix a subset of the validation set. But it seems there aren’t. At least now I can proceed with it in peace.