Metadata-Version: 2.3
Name: custom-onecyclelr
Version: 0.1.4
Summary: A Custom PyTorch implementation of the OneCycleLR learning rate scheduler. (With some modifications)
License: MIT License
         
         Copyright (c) 2025 Aidin Hamedi
         
         Permission is hereby granted, free of charge, to any person obtaining a copy
         of this software and associated documentation files (the "Software"), to deal
         in the Software without restriction, including without limitation the rights
         to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         copies of the Software, and to permit persons to whom the Software is
         furnished to do so, subject to the following conditions:
         
         The above copyright notice and this permission notice shall be included in all
         copies or substantial portions of the Software.
         
         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
         SOFTWARE.
Keywords: pytorch,deep-learning,optimizer,lr scheduler,OneCycleLR
Author: AidinHamedi
Author-email: aidin.hamediasl@gmail.com
Requires-Python: >=3.10
Classifier: Development Status :: 4 - Beta
Classifier: License :: OSI Approved :: MIT License
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: torch (>=2.0)
Project-URL: Homepage, https://github.com/AidinHamedi/Custom-OneCycleLr-Pytorch
Project-URL: Repository, https://github.com/AidinHamedi/Custom-OneCycleLr-Pytorch
Description-Content-Type: text/markdown


# OneCycle Learning Rate Scheduler

[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![PyPI - Version](https://img.shields.io/pypi/v/custom-onecyclelr)](https://pypi.org/project/custom-onecyclelr/)

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

```bash
pip install custom-onecyclelr
```

## Usage

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

```python
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](https://github.com/AidinHamedi/Custom-OneCycleLr-Pytorch/blob/master/doc/vis/onecycle_lr_schedule.png?raw=true)

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

```bash
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](LICENSE) for details.

