Shortcuts

PiecewiseLinear#

class ignite.handlers.param_scheduler.PiecewiseLinear(optimizer, param_name, milestones_values, save_history=False, param_group_index=None)[source]#

Piecewise linear parameter scheduler

Parameters
  • optimizer (torch.optim.optimizer.Optimizer) – torch optimizer or any object with attribute param_groups as a sequence.

  • param_name (str) – name of optimizer’s parameter to update.

  • milestones_values (List[Tuple[int, float]]) – list of tuples (event index, parameter value) represents milestones and parameter. Milestones should be increasing integers.

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

  • param_group_index (Optional[int]) – optimizer’s parameters group to use.

scheduler = PiecewiseLinear(optimizer, "lr",
                            milestones_values=[(10, 0.5), (20, 0.45), (21, 0.3), (30, 0.1), (40, 0.1)])
# Attach to the trainer
trainer.add_event_handler(Events.ITERATION_STARTED, scheduler)
#
# Sets the learning rate to 0.5 over the first 10 iterations, then decreases linearly from 0.5 to 0.45 between
# 10th and 20th iterations. Next there is a jump to 0.3 at the 21st iteration and LR decreases linearly
# from 0.3 to 0.1 between 21st and 30th iterations and remains 0.1 until the end of the iterations.
#

New in version 0.5.1.

Methods

get_param

Method to get current optimizer’s parameter values

load_state_dict

Copies parameters from state_dict into this ParamScheduler.

plot_values

Method to plot simulated scheduled values during num_events events.

simulate_values

Method to simulate scheduled values during num_events events.

state_dict

Returns a dictionary containing a whole state of ParamScheduler.

get_param()[source]#

Method to get current optimizer’s parameter values

Returns

list of params, or scalar param

Return type

float

load_state_dict(state_dict)#

Copies parameters from state_dict into this ParamScheduler.

Parameters

state_dict (Mapping) – a dict containing parameters.

Return type

None

classmethod plot_values(num_events, **scheduler_kwargs)#

Method to plot simulated scheduled values during num_events events.

This class requires matplotlib package to be installed:

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

  • scheduler_kwargs (Mapping) – parameter scheduler configuration kwargs.

Returns

matplotlib.lines.Line2D

Return type

Any

Examples

import matplotlib.pylab as plt

plt.figure(figsize=(10, 7))
LinearCyclicalScheduler.plot_values(num_events=50, param_name='lr',
                                    start_value=1e-1, end_value=1e-3, cycle_size=10))
classmethod simulate_values(num_events, **scheduler_kwargs)#

Method to simulate scheduled values during num_events events.

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

  • scheduler_kwargs (Any) – parameter scheduler configuration kwargs.

Returns

event_index, value

Return type

List[List[int]]

Examples:

lr_values = np.array(LinearCyclicalScheduler.simulate_values(num_events=50, param_name='lr',
                                                             start_value=1e-1, end_value=1e-3,
                                                             cycle_size=10))

plt.plot(lr_values[:, 0], lr_values[:, 1], label="learning rate")
plt.xlabel("events")
plt.ylabel("values")
plt.legend()
state_dict()#

Returns a dictionary containing a whole state of ParamScheduler.

Returns

a dictionary containing a whole state of ParamScheduler

Return type

dict