Skip to main content

Pytorch implementation of the gradient-based initialization

Project description

PyTorch implementation of the GradInit

Pytorch-based library to initialize any neural network with the gradient descent and your dataset. This implementation is the simple way to use the method described in the paper Chen Zhu et al. GradInit: Learning to Initialize Neural Networks for Stable and Efficient Training

Gradinit may be used as the alternative to the warmup mechanism. It is more useful for deep or unusual architectures.

Installation

pip install gradinit

or

pip install --upgrade git+https://github.com/johngull/gradinit.git

Differences from the original paper

This implementation uses simplified loss in the case of the small gradients. in this case, gradinit uses the user's loss instead of the complex 2-stage procedure described in the paper.

Usage

To make gradinit you need to wrap your model with the GradInitWrapper and then you can use the usual training loop with some differences:

  • recalculate loss with the GradInitWrapper.grad_loss function
  • clip scales with the GradInitWrapper.clamp_scales function

Here is a simplified example of the full gradinit process:

model: torch.nn.Module = ...

with GradInitWrapper(model) as ginit:
    #it is important to create optimizer after wraping your model
    optimizer = torch.optim.Adam(model.parameters())  
    norm = 1    # use 2 for the SGD optimizer

    model.train()
    for x, y in data_loader:
        pred = model(x)
        loss = criterion(pred, y)
    
        # recalculate gradinit loss based on your loss
        loss = ginit.grad_loss(loss, norm)
    
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    
        # clip minimum values for the init scales
        ginit.clamp_scales()
    
# on exit of with statement model is recovered to its normal way
# here you should create your main optimizer and start training
optimizer = torch.optim.Adam(model.parameters())
...

Alternatively to the with-notation you may use detach or delete the wrapper object after finishing the initialization process.

ginit = GradInitWrapper(model)
# full gradinit loop
...
ginit.detach() # or del ginit

# here you should create your main optimizer and start training
optimizer = torch.optim.Adam(model.parameters())
...

From our experience gradinit works worse in the mixed-precision mode. And we recommend running gradinit in the full-precision mode and then starting the main training loop in mixed-precision.

But if you really want to try, gradinit supports torch mixed-precision. In such a case gradinit need to use your scaler object. Here is an example of how to use gradinit with the torch amp.

import torch.cuda.amp

model: torch.nn.Module = ...
scaler: torch.cuda.amp.GradScaler = ...

with GradInitWrapper(model) as ginit:
    # it is important to create optimizer after wraping your model
    optimizer = torch.optim.Adam(model.parameters())
    norm = 1  # use 2 for the SGD optimizer

    model.train()
    for x, y in data_loader:
        with autocast():
            pred = model(x)
            loss = criterion(pred, y)

        # recalculate gradinit loss based on your loss
        loss = ginit.grad_loss(loss, norm, scaler=scaler)

        optimizer.zero_grad()
        scaler.scale(loss).backward()
        scaler.step(optimizer)
        scaler.update()

        # clip minimum values for the init scales
        ginit.clamp_scales()

# on exit of with statement model is recovered to its normal way
# here you should create your main optimizer and start training
optimizer = torch.optim.Adam(model.parameters())
...

Experiments

For the experiment results see this page

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

gradinit-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

gradinit-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file gradinit-0.1.0.tar.gz.

File metadata

  • Download URL: gradinit-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for gradinit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dacd17d081f27fafbdf4c31e664e27605633ee182edd65cf979fdacbb7d278b9
MD5 fe7105c85a71e9b06fd7770df2ee6e74
BLAKE2b-256 e2f5c3f59e02e601abcac07545139f54ede63b7360b0a8565da031d309ecb1bc

See more details on using hashes here.

File details

Details for the file gradinit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: gradinit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for gradinit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8dc59713447fca1758e7e2bb700c9fd0797d68b1e896af617a518b0620b37ab3
MD5 165d6417c891844e8e9a823b6e2d1320
BLAKE2b-256 bfc7a25a8d73b25aa84cb9e9209d5f6bc95a5bd8fd38869070978edcb2b197cd

See more details on using hashes here.

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