- Notifications
You must be signed in to change notification settings - Fork292
Bjarten/early-stopping-pytorch
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Early stopping is a form of regularization used to avoid overfitting on the training dataset. Early stopping keeps track of the validation loss, if the loss stops decreasing for several epochs in a row the training stops. TheEarlyStopping
class inearly_stopping_pytorch/early_stopping.py
is used to create an object to keep track of the validation loss while training aPyTorch model. It will save a checkpoint of the model each time the validation loss decrease. We set thepatience
argument in theEarlyStopping
class to how many epochs we want to wait after the last time the validation loss improved before breaking the training loop. There is a simple example of how to use theEarlyStopping
class in theMNIST_Early_Stopping_example notebook.
Underneath is a plot from the example notebook, which shows the last checkpoint made by the EarlyStopping object, right before the model started to overfit. It had patience set to 20.
pip install early-stopping-pytorch
For development or if you want the latest unreleased changes:
git clone https://github.com/your_username/early-stopping-pytorch.gitcd early-stopping-pytorch
Run the setup script to create a virtual environment and install all necessary dependencies.
./setup_dev_env.sh
Activate the virtual environment:
source dev-venv/bin/activate
Install the package locally in editable mode so you can use it immediately:
pip install -e.
fromearly_stopping_pytorchimportEarlyStopping# Initialize early stopping objectearly_stopping=EarlyStopping(patience=7,verbose=True)# In your training loop:forepochinrange(num_epochs):# ... training code ...val_loss= ...# calculate validation loss# Early stopping callearly_stopping(val_loss,model)ifearly_stopping.early_stop:print("Early stopping triggered")break
For a complete example, see theMNIST Early Stopping Example Notebook.
If you find this package useful in your research, please consider citing it as:
@misc{early_stopping_pytorch,author ={Bjarte Mehus Sunde},title ={early-stopping-pytorch: A PyTorch utility package for Early Stopping},year ={2024},url ={https://github.com/Bjarten/early-stopping-pytorch},}
TheEarlyStopping
class inearly_stopping_pytorch/early_stopping.py
is inspired by theignite EarlyStopping class.
About
Early stopping for PyTorch
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.