Skip to main content

A Custom PyTorch implementation of the OneCycleLR learning rate scheduler. (With some modifications)

Project description

OneCycle Learning Rate Scheduler (Custom Implementation)

A custom implementation of the OneCycle learning rate scheduler for PyTorch.

Features

  • Customized version of the OneCycleLR algorithm with four distinct phases: warmup, idling, annealing, and decay.
  • Flexibility in defining various hyperparameters such as:
    • Warmup iterations and type (linear or exponential)
    • Idling period duration
    • Annealing phase duration and minimum learning rate
    • Decay phase duration and minimum learning rate
  • Compatibility with any PyTorch optimizer

Installation

pip install custom-onecyclelr

Usage

Here's an example of how to integrate the scheduler into your training loop:

import torch
from custom_onecyclelr import scheduler

# Initialize model and optimizer
model = YourModel()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)

# Create the OneCycleLR scheduler with desired parameters
scheduler_instance = scheduler.OneCycleLr(
    optimizer,
    warmup_iters=6,  # Number of iterations for the warmup phase
    lr_idling_iters=8,  # Number of iterations where learning rate remains at max
    annealing_iters=56,  # Cosine annealing phase duration
    decay_iters=100,  # Linear decay phase duration
    max_lr=0.01,
    annealing_lr_min=0.001,
    decay_lr_min=0.0001,
    warmup_start_lr=0.0001,
    warmup_type="exp"  # "linear" or "exp"
)

# Training loop
for epoch in range(total_epochs):
    for inputs, targets in dataloader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()
        
        scheduler_instance.step()

Visualization

Visualization-img

You can visualize how the learning rate changes over iterations by running:

python examples/vis.py

This will generate a plot showing the different phases of the learning rate schedule.

License

This project is licensed under MIT License - see LICENSE for details.

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

custom_onecyclelr-0.1.3.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

custom_onecyclelr-0.1.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: custom_onecyclelr-0.1.3.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.1 Windows/11

File hashes

Hashes for custom_onecyclelr-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5ee1fe3bb70ac54ccc3a4f93ccdb56af2c2d42af6bfc6c8c70f81602120af430
MD5 91ee55ed0ea0021675bcfadc70446466
BLAKE2b-256 948a038ff95a87cc6849545305947d693c89fbd9b3fae3ca718bf0a58a9873e3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: custom_onecyclelr-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.1 Windows/11

File hashes

Hashes for custom_onecyclelr-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b0d4095e55689b75ef9dc361d295a1ba48559162da6267c52a7fb04146bb756a
MD5 cf6ee642b996b4e5a2dc60435e1b3159
BLAKE2b-256 8916b3c42d015fe46feae6fb340d70e2556404dbe85370ddf22b7dc490f9b2fb

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