Potential Leakage of Information Across Folds in Kfold.py
See original GitHub issue🐛 Bug
I believe that there can potentially be some leakage of information across folds when changing some parameters in the Kfold.py script. Say that the user chooses to save every single checkpoint. After the first fold training finishes, the second fold uses the same checkpoint directory as the first fold. So if the second fold finishes training and the user decides to load the best checkpoint, the second fold may potentially load a checkpoint from the training of the first fold.
To Reproduce
Run the script Kfold.py
Expected behavior
We expect that the training process of multiple folds is independent of one another.
Environment
- PyTorch Lightning Version: 1.6.0dev
- PyTorch Version: 1.10.0+cu102
- Python version: 3.7.11
- OS: Linux
- CUDA/cuDNN version: Using CPU
- GPU models and configuration:
- How you installed PyTorch (
conda,pip, source): - If compiling from source, the output of
torch.__config__.show(): - Any other relevant information:
Additional context
I think the solution may be to clear out previous checkpoints when starting out the new fold. We would also need to reset the checkpoint states (like reset minimum validation loss when advancing to the next fold).
cc @awaelchli @ananthsub @ninginthecloud @rohitgr7 @otaj @carmocca @justusschock
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)

Top Related StackOverflow Question
To use 1 model checkpoint per fold, here is how I did it:
val_lossshould be f"fold_{fold}-val_loss"ModelCheckpointwill throw an error because the model checkpoint for fold 0 can only monitorfold_0-val_lossso, during other folds, the metricfold_0-val_lossis not found. We can simply extendModelCheckpointto ignore folds that are not relevantNow, model checkpoint for k-fold will work properly 😉
This issue has been automatically marked as stale because it hasn’t had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, Pytorch Lightning Team!