Skip to main content

Cosine Annealing Linear Warmup

Project description

Cosine Annealing Scheduler with Linear Warmup

Implementation of a Cosine Annealing Scheduler with Linear Warmup and Restarts in PyTorch.
It has support for multiple parameters groups and minimum target learning rates.
Also works with the Lightning Modules!

Installation

pip install 'git+https://github.com/santurini/cosine-annealing-linear-warmup'

Usage

It is important to specify the parameters groups in the optimizer instantiation as the learning rates are directly inferred from the wrapped optimizer.

Example: Multiple groups

from cosine_warmup import CosineAnnealingLinearWarmup

optimizer = torch.optim.Adam([
    {"params": first_group_params, "lr": 1e-3},
    {"params": second_group_params, "lr": 1e-4},
    ]
)

scheduler = CosineAnnealingLinearWarmup(
    optimizer = optimizer,
    min_lrs = [ 1e-5, 1e-6 ],
    first_cycle_steps = 1000,
    warmup_steps = 500,
    gamma = 0.9
    )
    
# this is equivalent to

scheduler = CosineAnnealingLinearWarmup(
    optimizer = optimizer,
    min_lrs_pow = 2,
    first_cycle_steps = 1000,
    warmup_steps = 500,
    gamma = 0.9
    )

Example: Single groups

from cosine_linear_warmup import CosineAnnealingLinearWarmup

optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

scheduler = CosineAnnealingLinearWarmup(
    optimizer = optimizer,
    min_lrs = [ 1e-5 ],
    first_cycle_steps = 1000,
    warmup_steps = 500,
    gamma = 0.9
    )
    
# this is equivalent to

scheduler = CosineAnnealingLinearWarmup(
    optimizer = optimizer,
    min_lrs_pow = 2,
    first_cycle_steps = 1000,
    warmup_steps = 500,
    gamma = 0.9
    )

Visual Example

Unknown-2

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

cosine-warmup-0.0.0.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

cosine_warmup-0.0.0-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

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