{"id":15027447,"url":"https://github.com/bjarten/early-stopping-pytorch","last_synced_at":"2025-10-08T17:13:10.573Z","repository":{"id":41156314,"uuid":"163538431","full_name":"Bjarten/early-stopping-pytorch","owner":"Bjarten","description":"Early stopping for PyTorch ","archived":false,"fork":false,"pushed_at":"2024-11-11T10:34:07.000Z","size":735,"stargazers_count":1252,"open_issues_count":2,"forks_count":292,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-29T09:23:15.082Z","etag":null,"topics":["early","early-stopping","mnist","python","pytorch","pytorch-tutorial","regularization","stopping","tutorial"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bjarten.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-29T20:15:51.000Z","updated_at":"2025-04-15T08:13:56.000Z","dependencies_parsed_at":"2024-12-05T11:06:11.608Z","dependency_job_id":"468a001b-5321-4227-aedd-3ae32f3d4422","html_url":"https://github.com/Bjarten/early-stopping-pytorch","commit_stats":{"total_commits":38,"total_committers":8,"mean_commits":4.75,"dds":0.4473684210526315,"last_synced_commit":"cba4e599d93e2f0693ea066cb331f7689ae0a40d"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bjarten%2Fearly-stopping-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bjarten%2Fearly-stopping-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bjarten%2Fearly-stopping-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bjarten%2Fearly-stopping-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bjarten","download_url":"https://codeload.github.com/Bjarten/early-stopping-pytorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254052722,"owners_count":22006716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["early","early-stopping","mnist","python","pytorch","pytorch-tutorial","regularization","stopping","tutorial"],"created_at":"2024-09-24T20:06:26.933Z","updated_at":"2025-10-08T17:13:05.554Z","avatar_url":"https://github.com/Bjarten.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Early Stopping for PyTorch\nEarly 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. The ```EarlyStopping``` class in ```early_stopping_pytorch/early_stopping.py``` is used to create an object to keep track of the validation loss while training a [PyTorch](https://pytorch.org/) model. It will save a checkpoint of the model each time the validation loss decrease.  We set the ```patience``` argument in the ```EarlyStopping``` 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 the ```EarlyStopping``` class in the [MNIST_Early_Stopping_example](MNIST_Early_Stopping_example.ipynb) notebook.\n\nUnderneath 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.\n\n![Loss Plot](https://raw.githubusercontent.com/Bjarten/early-stopping-pytorch/main/loss_plot.png)\n\n## Installation\n\n### Option 1: Install from PyPI (Recommended)\n```bash\npip install early-stopping-pytorch\n```\n\n### Option 2: Install from Source\nFor development or if you want the latest unreleased changes:\n\n### 1. Clone the Repository\n```bash\ngit clone https://github.com/your_username/early-stopping-pytorch.git\ncd early-stopping-pytorch\n```\n\n### 2. Set Up the Virtual Environment\nRun the setup script to create a virtual environment and install all necessary dependencies.\n```bash\n./setup_dev_env.sh\n```\n\n### 3. Activate the Virtual Environment\nActivate the virtual environment:\n```bash\nsource dev-venv/bin/activate\n```\n\n### 4. Install the Package in Editable Mode\nInstall the package locally in editable mode so you can use it immediately:\n```bash\npip install -e .\n```\n\n## Usage\n\n```python\nfrom early_stopping_pytorch import EarlyStopping\n\n# Initialize early stopping object\nearly_stopping = EarlyStopping(patience=7, verbose=True)\n\n# In your training loop:\nfor epoch in range(num_epochs):\n    # ... training code ...\n    val_loss = ... # calculate validation loss\n\n    # Early stopping call\n    early_stopping(val_loss, model)\n    if early_stopping.early_stop:\n        print(\"Early stopping triggered\")\n        break\n```\n\nFor a complete example, see the [MNIST Early Stopping Example Notebook](MNIST_Early_Stopping_example.ipynb).\n\n## Citation\n\nIf you find this package useful in your research, please consider citing it as:\n\n```bibtex\n@misc{early_stopping_pytorch,\n  author = {Bjarte Mehus Sunde},\n  title = {early-stopping-pytorch: A PyTorch utility package for Early Stopping},\n  year = {2024},\n  url = {https://github.com/Bjarten/early-stopping-pytorch},\n}\n```\n\n## References\nThe ```EarlyStopping``` class in ```early_stopping_pytorch/early_stopping.py``` is inspired by the [ignite EarlyStopping class](https://github.com/pytorch/ignite/blob/master/ignite/handlers/early_stopping.py).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjarten%2Fearly-stopping-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbjarten%2Fearly-stopping-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjarten%2Fearly-stopping-pytorch/lists"}