Name | gradientnorm-autoclipper JSON |
Version |
1.0.0
JSON |
| download |
home_page | None |
Summary | Clip gradient norm automatically |
upload_time | 2024-05-28 21:58:32 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | MIT License Copyright (c) 2024 michaldyczko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
pytorch
gradient clipping
automl
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# AutoClipper
AutoClippedOptimizer is a Python factory function that returns an optimizer class with automatic gradient clipping. This feature can help to stabilize training in certain situations by limiting the magnitude of gradient updates.
The implementation is inspired by the paper "AutoClip: Adaptive Gradient Clipping for Source Separation Networks" (<https://arxiv.org/abs/2007.14469>) with two key differences:
1. Instead of keeping track of the whole grad norm history, it limits its size to a specified window.
2. It enables setting a max_norm to clamp the max grad norm value.
## Usage
```python
from autoclipper import AutoClippedOptimizer
# Create a new optimizer class with automatic gradient clipping
optimizer_cls = AutoClippedOptimizer(optimizer_cls, q=0.1, window=200, max_norm=None)
# Use the new optimizer class in your training loop
optimizer = optimizer_cls(model.parameters(), lr=0.01)
```
## Parameters
- `optimizer_cls` (Type[Optimizer]): The base optimizer class to extend with automatic gradient clipping.
- `q` (float, optional): The quantile at which to clip gradients. Gradients with norms larger than the q-th quantile of recent gradient norms are clipped. Default is 0.1.
- `window` (int, optional): The number of recent gradient norms to consider when computing the q-th quantile for clipping. Default is 200.
- `max_norm` (float, optional): An optional maximum gradient norm. If provided, gradients with norms larger than this value are always clipped to this value. Default is None, which means no absolute maximum gradient norm is enforced.
## Methods
- `__init__(self, *args, **kwargs)`: Initializes the optimizer.
- `_get_grad_norm(self)`: Calculates the norm of the gradient for the current step.
- `_autoclip_gradients(self)`: Automatically clips the gradients based on the recent gradient norms.
- `_main_params(self)`: Yields the main parameters of the optimizer.
- `step(self, closure=None, **kwargs)`: Performs a single optimization step.
- `reset(self)`: Resets the state of the optimizer.
Raw data
{
"_id": null,
"home_page": null,
"name": "gradientnorm-autoclipper",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "pytorch, gradient clipping, automl",
"author": null,
"author_email": "Micha\u0142 Dyczko <michal@dyczko.dev>",
"download_url": "https://files.pythonhosted.org/packages/03/a5/5be07daf059384c64eaf40c1eb06a19360981139ce9561a59cb78ea13aca/gradientnorm_autoclipper-1.0.0.tar.gz",
"platform": null,
"description": "# AutoClipper\n\nAutoClippedOptimizer is a Python factory function that returns an optimizer class with automatic gradient clipping. This feature can help to stabilize training in certain situations by limiting the magnitude of gradient updates.\n\nThe implementation is inspired by the paper \"AutoClip: Adaptive Gradient Clipping for Source Separation Networks\" (<https://arxiv.org/abs/2007.14469>) with two key differences:\n\n1. Instead of keeping track of the whole grad norm history, it limits its size to a specified window.\n2. It enables setting a max_norm to clamp the max grad norm value.\n\n## Usage\n\n```python\nfrom autoclipper import AutoClippedOptimizer\n\n# Create a new optimizer class with automatic gradient clipping\noptimizer_cls = AutoClippedOptimizer(optimizer_cls, q=0.1, window=200, max_norm=None)\n\n# Use the new optimizer class in your training loop\noptimizer = optimizer_cls(model.parameters(), lr=0.01)\n```\n\n## Parameters\n\n- `optimizer_cls` (Type[Optimizer]): The base optimizer class to extend with automatic gradient clipping.\n- `q` (float, optional): The quantile at which to clip gradients. Gradients with norms larger than the q-th quantile of recent gradient norms are clipped. Default is 0.1.\n- `window` (int, optional): The number of recent gradient norms to consider when computing the q-th quantile for clipping. Default is 200.\n- `max_norm` (float, optional): An optional maximum gradient norm. If provided, gradients with norms larger than this value are always clipped to this value. Default is None, which means no absolute maximum gradient norm is enforced.\n\n## Methods\n\n- `__init__(self, *args, **kwargs)`: Initializes the optimizer.\n- `_get_grad_norm(self)`: Calculates the norm of the gradient for the current step.\n- `_autoclip_gradients(self)`: Automatically clips the gradients based on the recent gradient norms.\n- `_main_params(self)`: Yields the main parameters of the optimizer.\n- `step(self, closure=None, **kwargs)`: Performs a single optimization step.\n- `reset(self)`: Resets the state of the optimizer.\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2024 michaldyczko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "Clip gradient norm automatically",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/michaldyczko/gradientnorm-autoclipper"
},
"split_keywords": [
"pytorch",
" gradient clipping",
" automl"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e2cf10a3c0731b7740b3dd513a341bd5c0db8deb5a81ecb7cd3b5364781a75a5",
"md5": "b4b47dcd65e30f4905a4ba4636839a4e",
"sha256": "0e9e3f28a4122870fc5b0e7d7d05fa0049840719f7358574ac0bfa76bf359d28"
},
"downloads": -1,
"filename": "gradientnorm_autoclipper-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b4b47dcd65e30f4905a4ba4636839a4e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 5662,
"upload_time": "2024-05-28T21:58:30",
"upload_time_iso_8601": "2024-05-28T21:58:30.840474Z",
"url": "https://files.pythonhosted.org/packages/e2/cf/10a3c0731b7740b3dd513a341bd5c0db8deb5a81ecb7cd3b5364781a75a5/gradientnorm_autoclipper-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "03a55be07daf059384c64eaf40c1eb06a19360981139ce9561a59cb78ea13aca",
"md5": "25f4ee6937e6a77c1cb1abed9cdbca91",
"sha256": "f30747120808dec1035de8dd551f2ce52cffa6745016aad5f7a3250d97573063"
},
"downloads": -1,
"filename": "gradientnorm_autoclipper-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "25f4ee6937e6a77c1cb1abed9cdbca91",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 4217,
"upload_time": "2024-05-28T21:58:32",
"upload_time_iso_8601": "2024-05-28T21:58:32.693092Z",
"url": "https://files.pythonhosted.org/packages/03/a5/5be07daf059384c64eaf40c1eb06a19360981139ce9561a59cb78ea13aca/gradientnorm_autoclipper-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-28 21:58:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "michaldyczko",
"github_project": "gradientnorm-autoclipper",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "gradientnorm-autoclipper"
}