Shortcuts

LRScheduler#

class ignite.handlers.param_scheduler.LRScheduler(lr_scheduler, save_history=False, use_legacy=False)[source]#

A wrapper class to call torch.optim.lr_scheduler objects as ignite handlers.

Parameters
  • lr_scheduler (LRScheduler) – lr_scheduler object to wrap.

  • save_history (bool) – whether to log the parameter values to engine.state.param_history, (default=False).

  • use_legacy (bool) – if True, scheduler should be attached to Events.ITERATION_COMPLETED, (default=False).

Examples

from collections import OrderedDict

import torch
from torch import nn, optim

from ignite.engine import *
from ignite.handlers import *
from ignite.metrics import *
from ignite.metrics.clustering import *
from ignite.metrics.regression import *
from ignite.utils import *

# create default evaluator for doctests

def eval_step(engine, batch):
    return batch

default_evaluator = Engine(eval_step)

# create default optimizer for doctests

param_tensor = torch.zeros([1], requires_grad=True)
default_optimizer = torch.optim.SGD([param_tensor], lr=0.1)

# create default trainer for doctests
# as handlers could be attached to the trainer,
# each test must define his own trainer using `.. testsetup:`

def get_default_trainer():

    def train_step(engine, batch):
        return batch

    return Engine(train_step)

# create default model for doctests

default_model = nn.Sequential(OrderedDict([
    ('base', nn.Linear(4, 2)),
    ('fc', nn.Linear(2, 1))
]))

manual_seed(666)
default_trainer = get_default_trainer()

from torch.optim.lr_scheduler import StepLR

torch_lr_scheduler = StepLR(default_optimizer, step_size=3, gamma=0.1)
scheduler = LRScheduler(torch_lr_scheduler)

default_trainer.add_event_handler(Events.ITERATION_STARTED, scheduler)

@default_trainer.on(Events.ITERATION_COMPLETED)
def print_lr():
    print(default_optimizer.param_groups[0]["lr"])

default_trainer.run([0] * 8, max_epochs=1)
0.1
0.1
0.1
0.010...
0.010...
0.010...
0.001...
0.001...

New in version 0.4.5.

Changed in version 0.4.9: added use_legacy argument

Methods

get_param

Method to get current optimizer's parameter value

simulate_values

Method to simulate scheduled values during num_events events.

get_param()[source]#

Method to get current optimizer’s parameter value

Return type

Union[float, List[float]]

classmethod simulate_values(num_events, lr_scheduler, **kwargs)[source]#

Method to simulate scheduled values during num_events events.

Parameters
  • num_events (int) – number of events during the simulation.

  • lr_scheduler (LRScheduler) – lr_scheduler object to wrap.

  • kwargs (Any) –

Returns

event_index, value

Return type

List[List[int]]