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:
- Instead of keeping track of the whole grad norm history, it limits its size to a specified window.
- It enables setting a max_norm to clamp the max grad norm value.
Usage
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.
Metadata
Release files for gradientnorm-autoclipper 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gradientnorm_autoclipper-1.0.0.tar.gz | 4.2 kB | Without provenance | Details for gradientnorm_autoclipper-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gradientnorm_autoclipper-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for gradientnorm_autoclipper-1.0.0-py3-none-any.whl |
Total release size: 9.9 kB
Release files / gradientnorm_autoclipper-1.0.0.tar.gz
| Download URL | gradientnorm_autoclipper-1.0.0.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f30747120808dec1035de8dd551f2ce52cffa6745016aad5f7a3250d97573063
|
|
BLAKE2b-256 checksum How to use checksums |
03a55be07daf059384c64eaf40c1eb06a19360981139ce9561a59cb78ea13aca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.12
|
Release files / gradientnorm_autoclipper-1.0.0-py3-none-any.whl
| Download URL | gradientnorm_autoclipper-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e9e3f28a4122870fc5b0e7d7d05fa0049840719f7358574ac0bfa76bf359d28
|
|
BLAKE2b-256 checksum How to use checksums |
e2cf10a3c0731b7740b3dd513a341bd5c0db8deb5a81ecb7cd3b5364781a75a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.12
|
Security for gradientnorm-autoclipper 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.