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

# 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.4.tar.gz (4.9 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.4-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: custom_onecyclelr-0.1.4.tar.gz
  • Upload date:
  • Size: 4.9 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.4.tar.gz
Algorithm Hash digest
SHA256 c874451ecc7613df10c76087c302844a3a527e0602ff2de3cefcc78601d47d42
MD5 45b6a3d0a6ef5fa4e37245a9fc38601a
BLAKE2b-256 f3200901b753f851b5a8ffaaa75b38a201f345bb4dacfabd7c047985e03198a4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: custom_onecyclelr-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.9 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f3c38a0625974dd7c41562f38a57d915d21f5d238ea5ae1f931d0f7f6ad41daa
MD5 e69f8ca8f112b5303b050278b6caabf9
BLAKE2b-256 8f1e5380b2f4ff0fc542d4d2c34c6fa7b56fe4bc593d7f1d4ddfc36fdcb76cfe

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