Test accuracy is increasing when calculated at intermediate epochs vs at the last epoch

Thanks for the code.
It seems that the additional call of the test method inside the training loop calls into the pseudo-random number generator and thus changes the order of the training data in the next step.
This will result in a bit noise during the training, which thus yields a different end result.

You can check it by printing the sum of the data samples in your training loop (which can be seen as a “unique value” for the sample).
If I add a seeding inside the training loop to make sure the train_loader uses the same order of samples, I get the same results for the additional test call and just the final one:

 for epoch in range(1, epochs + 1):
        torch.manual_seed(seed+epoch+1)
        train(model, device, train_loader, optimizer, epoch,log_interval)
        #test(model, device, test_loader) # uncommenting this line produces the same result now

    test(model, device, test_loader)

I’m unsure, where the test_loader is using random numbers, but based on the current workaround, this is my best guess.

3 Likes