Could you describe the “memory leak” a bit more?
Are you seeing an increase in memory usage in the Windows task manager after executing the training and validation loop, which is approx. constant afterwards or is the memory increasing in each iteration until you run out of memory?
The former could be expected, if you are keeping objects alive from the training loop and if the validation loop needs to allocate additional memory.
PyTorch uses a custom caching allocator, which will keep the already allocated memory in the cache and will not release it for performance reasons.
Calling empty_cache won’t avoid any out of memory issues, will slow down your code, and is useful if you want to release the cache so that other processes could use it.