Modular optimization library for PyTorch.
Project description
torchzero
torchzero implements a large number of chainable optimization modules that can be chained together to create custom optimizers:
import torchzero as tz
optimizer = tz.Modular(
model.parameters(),
tz.m.Adam(),
tz.m.Cautious(),
tz.m.LR(1e-3),
tz.m.WeightDecay(1e-4)
)
# standard training loop
for batch in dataset:
preds = model(batch)
loss = criterion(preds)
optimizer.zero_grad()
optimizer.step()
Each module takes the output of the previous module and applies a further transformation. This modular design avoids redundant code, such as reimplementing cautioning, orthogonalization, laplacian smoothing, etc for every optimizer. It is also easy to experiment with grafting, interpolation between different optimizers, and perhaps some weirder combinations like nested momentum.
Modules are not limited to gradient transformations. They can perform other operations like line searches, exponential moving average (EMA) and stochastic weight averaging (SWA), gradient accumulation, gradient approximation, and more.
There are over 100 modules, all accessible within the tz.m namespace. For example, the Adam update rule is available as tz.m.Adam. Complete list of modules is available in documentation.
Closure
Some modules and optimizers in torchzero, particularly line-search methods and gradient approximation modules, require a closure function. This is similar to how torch.optim.LBFGS works in PyTorch. In torchzero, closure needs to accept a boolean backward argument (though the argument can have any name). When backward=True, the closure should zero out old gradients using opt.zero_grad(), and compute new gradients using loss.backward().
def closure(backward = True):
preds = model(inputs)
loss = loss_fn(preds, targets)
if backward:
optimizer.zero_grad()
loss.backward()
return loss
optimizer.step(closure)
If you intend to use gradient-free methods, backward argument is still required in the closure. Simply leave it unused. Gradient-free and gradient approximation methods always call closure with backward=False.
All built-in pytorch optimizers, as well as most custom ones, support closure too. So the code above will work with all other optimizers out of the box, and you can switch between different optimizers without rewriting your training loop.
Documentation
For more information on how to create, use and extend torchzero modules, please refer to the documentation at torchzero.readthedocs.io.
Extra
Some other optimization related things in torchzero:
scipy.optimize.minimize wrapper
scipy.optimize.minimize wrapper with support for both gradient and hessian via batched autograd
from torchzero.optim.wrappers.scipy import ScipyMinimize
opt = ScipyMinimize(model.parameters(), method = 'trust-krylov')
Use as any other closure-based optimizer, but make sure closure accepts backward argument. Note that it performs full minimization on each step.
Nevergrad wrapper
Nevergrad is an optimization library by facebook with an insane number of gradient free methods.
from torchzero.optim.wrappers.nevergrad import NevergradOptimizer
opt = NevergradOptimizer(bench.parameters(), ng.optimizers.NGOptBase, budget = 1000)
Use as any other closure-based optimizer, but make sure closure accepts backward argument.
NLopt wrapper
NLopt is another optimization library similar to scipy.optimize.minimize, with a large number of both gradient based and gradient free methods.
from torchzero.optim.wrappers.nlopt import NLOptOptimizer
opt = NLOptOptimizer(bench.parameters(), 'LD_TNEWTON_PRECOND_RESTART', maxeval = 1000)
Use as any other closure-based optimizer, but make sure closure accepts backward argument. Note that it performs full minimization on each step.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchzero-0.1.8.tar.gz.
File metadata
- Download URL: torchzero-0.1.8.tar.gz
- Upload date:
- Size: 103.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa76525ca96b5466c0f8bf818fed2f7ae7c7f68772e94ff510f28595e8082098
|
|
| MD5 |
1a14c725055faf2941ddab22c6528034
|
|
| BLAKE2b-256 |
561defaf6c393797aee3ecb8ddb3da2e8933fae1cfc5bcdd3ff7d8f021f0cc10
|
Provenance
The following attestation bundles were made for torchzero-0.1.8.tar.gz:
Publisher:
python-publish.yml on inikishev/torchzero
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchzero-0.1.8.tar.gz -
Subject digest:
aa76525ca96b5466c0f8bf818fed2f7ae7c7f68772e94ff510f28595e8082098 - Sigstore transparency entry: 170044219
- Sigstore integration time:
-
Permalink:
inikishev/torchzero@de5cae48081347ce8f149235a81dc160ba41c37f -
Branch / Tag:
refs/tags/0.1.8 - Owner: https://github.com/inikishev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@de5cae48081347ce8f149235a81dc160ba41c37f -
Trigger Event:
push
-
Statement type:
File details
Details for the file torchzero-0.1.8-py3-none-any.whl.
File metadata
- Download URL: torchzero-0.1.8-py3-none-any.whl
- Upload date:
- Size: 139.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c47391d7b546eaf9099d19c6e46f65416b4f33c11c08ffd701513dbe599ec8bb
|
|
| MD5 |
616eb3088ef3580dc43d0d2c0d80c0d6
|
|
| BLAKE2b-256 |
6dfec9f5ec691a1a751a396046e5c16fd98f121b9f793b2a039f79d406b141ce
|
Provenance
The following attestation bundles were made for torchzero-0.1.8-py3-none-any.whl:
Publisher:
python-publish.yml on inikishev/torchzero
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchzero-0.1.8-py3-none-any.whl -
Subject digest:
c47391d7b546eaf9099d19c6e46f65416b4f33c11c08ffd701513dbe599ec8bb - Sigstore transparency entry: 170044220
- Sigstore integration time:
-
Permalink:
inikishev/torchzero@de5cae48081347ce8f149235a81dc160ba41c37f -
Branch / Tag:
refs/tags/0.1.8 - Owner: https://github.com/inikishev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@de5cae48081347ce8f149235a81dc160ba41c37f -
Trigger Event:
push
-
Statement type: