Skip to main content

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

Project description

OneCycle Learning Rate Scheduler

Ruff License: MIT PyPI - Version

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"
)

# You can also use the config_helper function to simplify the configuration (works best for higher total iterations)
# from custom_onecyclelr import config
# scheduler_instance = scheduler.OneCycleLr(
#     optimizer,
#     config.config_helper(200, 0.008)
# )

# 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.2.0.tar.gz (5.1 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.2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: custom_onecyclelr-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/6.16.4-arch1-1

File hashes

Hashes for custom_onecyclelr-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d965d1aaf10856f6eb48325303676e45b987dd7d731f022fc22c02effc35a4d5
MD5 8c0e8f9fa655a06c321e07d931b2b2eb
BLAKE2b-256 9ba978eb9b78a4438b84cde60298a6b5db746d7e5f56adbe00e5d628ea86aed8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: custom_onecyclelr-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/6.16.4-arch1-1

File hashes

Hashes for custom_onecyclelr-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 464e26bbb7f6839d6cea5cdfd4dc49fcd0b18453b6545632f32e7cdc08d1bca8
MD5 45299f60cbcb354148a691712b86353b
BLAKE2b-256 af66f209b5a79ad027a574e339e2f80752b8961292725221fbc6711b5fc97792

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