Skip to main content

Modular optimization library for PyTorch.

Project description

example workflow

torchzero

This is a work-in-progress optimizers library for pytorch with composable zeroth, first, second order and quasi newton methods, gradient approximation, line searches and a whole lot of other stuff.

Most optimizers are modular, meaning you can chain them like this:

optimizer = torchzero.optim.Modular(model.parameters(), [*list of modules*])`

For example you might use [ClipNorm(4), LR(1e-3), NesterovMomentum(0.9)] for standard SGD with gradient clipping and nesterov momentum. Move ClipNorm to the end to clip the update instead of the gradients. If you don't have access to gradients, add a RandomizedFDM() at the beginning to approximate them via randomized finite differences. Add Cautious() to make the optimizer cautious.

Each new module takes previous module update and works on it. That way there is no need to reimplement stuff like laplacian smoothing for all optimizers, and it is easy to experiment with grafting, interpolation between different optimizers, and perhaps some weirder combinations like nested momentum.

How to use

All modules are defined in torchzero.modules. You can generally mix and match them however you want. Some pre-made optimizers are available in torchzero.optim.

Some optimizers require closure, which should look like this:

def closure(backward = True):
  preds = model(inputs)
  loss = loss_fn(preds, targets)

  # if you can't call loss.backward(), and instead use gradient-free methods,
  # they always call closure with backward=False.
  # so you can remove the part below, but keep the unused backward argument.
  if backward:
    optimizer.zero_grad()
    loss.backward()
  return loss

optimizer.step(closure)

This closure will also work with all built in pytorch optimizers, including LBFGS, all optimizers in this library, as well as most custom ones.

Contents

Docs are available at torchzero.readthedocs.io. A preliminary list of all modules is available here https://torchzero.readthedocs.io/en/latest/autoapi/torchzero/modules/index.html#classes. Some of the implemented algorithms:

All modules should be quite fast, especially on models with many different parameters, due to _foreach operations.

I am getting to the point where I can start focusing on good docs and tests. As of now, the code should be considered experimental, untested and subject to change, so feel free but be careful if using this for actual project.

Wrappers

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 optimizer (make sure closure accepts backward argument like one from How to use). Note that it performs full minimization on each step.

Nevergrad wrapper

opt = NevergradOptimizer(bench.parameters(), ng.optimizers.NGOptBase, budget = 1000)

Use as any other optimizer (make sure closure accepts backward argument like one from How to use).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchzero-0.1.3.tar.gz (103.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchzero-0.1.3-py3-none-any.whl (138.6 kB view details)

Uploaded Python 3

File details

Details for the file torchzero-0.1.3.tar.gz.

File metadata

  • Download URL: torchzero-0.1.3.tar.gz
  • Upload date:
  • Size: 103.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for torchzero-0.1.3.tar.gz
Algorithm Hash digest
SHA256 50fd42b5554cfe01c40cb64e9ea225bee11005fac01ae79f949e3312d65d8da1
MD5 7ee40b7ea9604094eceb115bb9a0e051
BLAKE2b-256 e0c8d1be0a2aa4d9c5c690705f0c262923be23594bb274fb32ac4cdc64433f29

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchzero-0.1.3.tar.gz:

Publisher: python-publish.yml on inikishev/torchzero

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchzero-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: torchzero-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 138.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for torchzero-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5987d9baa828d90b032a4b4d6fd0fd25b87982e62f1334e5a5ba3544b7ca6d0a
MD5 1d11014ec1f879218042be5ffde5a905
BLAKE2b-256 db9cd1a9e26772209c9220c80c9038183d8aa00c8164d05dc480c0f512537f51

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchzero-0.1.3-py3-none-any.whl:

Publisher: python-publish.yml on inikishev/torchzero

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page