Skip to main content

AGC Optimizers

A small lib for using adaptive gradient clipping in your optimizer. Currently PyTorch only.

News

Sep 15, 2021

  • Add AGC use independent from optimizer choice in PyTorch

Sep 14, 2021

  • Add AdamW, Adam, SGD and RMSprop with AGC
  • Add first comparsion between optimizers with and without AGC based on CIFAR10

Introduction

Brock et al. introduced 2021 a new clipping technique in order to increase stability of large batch training and high learning rates in their Normalizer-Free Networks (NFNet), the adaptive gradient clipping. This clipping method is not implemented in leading frameworks, thus I provide optimizers which are capable of AGC.

Installation

pip install agc_optims

Usage

To be consistent with PyTorch all arguments of the optimizer remain the same as in the standard. Only two parameters are added for the AGC:

  • clipping : Hyperparameter for the clipping of the parameter. Default value 1e-2, smaller batch sizes demand a higher clipping parameter
  • agc_eps : Term used in AGC to prevent grads clipped to zero, default value 1e-3

Optimizer independent

from torch.optim import Adam
from agc_optims.clipper import AGC

net = Net() # your model

optimizer = Adam(net.parameters(), lr=0.001)
optimizer = AGC(optimizer=optimizer, clipping=0.16)

SGD

from agc_optims.optim import SGD_AGC

net = Net() # your model

optimizer = SGD_AGC(net.parameters(), lr=0.01, momentum=0.9, clipping=0.16)

Adam

from agc_optims.optim import Adam_AGC

net = Net() # your model

optimizer = Adam_AGC(net.parameters(), lr=0.001, weight_decay=1e-4, clipping=0.16)

AdamW

from agc_optims.optim import AdamW_AGC

net = Net() # your model

optimizer = AdamW_AGC(net.parameters(), lr=0.001, weight_decay=1e-4, clipping=0.16)

RMSprop

from agc_optims.optim import RMSprop_AGC

net = Net() # your model

optimizer = RMSprop_AGC(net.parameters(), lr=0.001, clipping=0.16)

Now you can use the optimizer just like their non-AGC counterparts.

Comparison

The following comparison shows that for batch sizes 64 and 128 Adam with AGC performs better than the normal Adam. SGD is unfortunately worse with AGC, but the batch size is also very small compared to the NFNet paper. This requires more comparisons with higher batch sizes and also on other data sets. RMSprop is also better at both batch sizes with AGC than without. The learning rate was left at the default value for all optimizers and the scripts in the performance_tests folder were used as the test environment.

Batch Size 64 - SGD Accuracy on Cifar10 Batch Size 64 - SGD Loss on Cifar10
Batch Size 128 - SGD Accuracy on Cifar10 Batch Size 128 - SGD Loss on Cifar10
Batch Size 64 - Adam Accuracy on Cifar10 Batch Size 64 - Adam Loss on Cifar10
Batch Size 128 - Adam Accuracy on Cifar10 Batch Size 128 - Adam Loss on Cifar10
Batch Size 64 - RMSProp Accuracy on Cifar10 Batch Size 64 - RMSProp Loss on Cifar10
Batch Size 128 - RMSProp Accuracy on Cifar10 Batch Size 128 - RMSProp Loss on Cifar10

As a little treat, I have also compared the speed of the optimizer with and without AGC to see whether this greatly increases training times.

Batch Size 128 - RMSProp Accuracy on Cifar10 Batch Size 128 - RMSProp Loss on Cifar10

To Do

  • Add first comparsion based on CIFAR10 with a small CNN
  • Add AGC independent from optimizer
  • Add comparsion with higher batch sizes (256,512,1024)
  • Add tests for each optimizer
  • Clipping == 0 no AGC
  • Add comparsion based on CIFAR100 with a small CNN
  • Add comparsion based on CIFAR10/100 with ResNet
  • Add comparsion with ImageNet (I do not have enough GPU-Power currently if someone provides some tests I would be grateful)
  • Add all optimizer included in PyTorch
  • Support of other frameworks than PyTorch
  • Add first comparsion based on CIFAR with a small CNN

Metadata

Release files for agc-optims 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agc-optims 0.0.2
File Size Uploaded Provenance
agc_optims-0.0.2.tar.gz 11.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for agc-optims 0.0.2
File Interpreter ABI Platform
agc_optims-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 26.7 kB

Release files / agc_optims-0.0.2.tar.gz

Download URL agc_optims-0.0.2.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d7ce307ecb8440c44c67e2476bb58648c75f66dc62e009ec6da389f89659665f
BLAKE2b-256 checksum
How to use checksums
8edded06fe52097a1715291fe85d4ae72bd24d85d716c3ed9a497a5d22923d52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / agc_optims-0.0.2-py3-none-any.whl

Download URL agc_optims-0.0.2-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67a625704f56979746b04ecc811d6cf0eabf487f6a740c17458600cc45bb20ba
BLAKE2b-256 checksum
How to use checksums
767f2667051f696d640ec96cfbce05d4d6f7342b932944bc0da841fb18e6d987
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Security for agc-optims 0.0.2

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.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page