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.1.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.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gradinit-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 8d48509f01c0a253dda478fbc87a1325170809c237c7d2e32e611b4ecb4c7c61
MD5 26593bbe2c95ddc96cfe9b41c07cdad3
BLAKE2b-256 eb175ee76f751c84f46c64f11547870e653d0f2f183afa6ea301af198facbcb4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gradinit-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 edc9f40bbf5141bb332e319d884f896f8990746657aac67bdf087406917f65e4
MD5 5049425cd8ad62c32e6cbc70c11a8592
BLAKE2b-256 d8ef1446ebba0a420ebd489482d880bdc78cdeba4748c956b0eadf1e7e3b61de

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