1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
import numbers
import warnings
from bisect import bisect_right
from typing import Any, List, Sequence, Tuple, Union
from ignite.engine import CallableEventWithFilter, Engine, Events, EventsList
from ignite.handlers.param_scheduler import BaseParamScheduler
class StateParamScheduler(BaseParamScheduler):
"""An abstract class for updating an engine state parameter values during training.
Args:
param_name: name of parameter to update.
save_history: whether to log the parameter values to ``engine.state.param_history``, (default=False).
create_new: whether to create ``param_name`` on ``engine.state`` taking into account whether ``param_name``
attribute already exists or not. Overrides existing attribute by default, (default=False).
Note:
Parameter scheduler works independently of the internal state of the attached engine.
More precisely, whatever the state of the engine (newly created or used by another scheduler) the scheduler
sets defined absolute values.
.. versionadded:: 0.4.7
"""
def __init__(self, param_name: str, save_history: bool = False, create_new: bool = False):
super(StateParamScheduler, self).__init__(param_name, save_history)
self.create_new = create_new
def attach(
self,
engine: Engine,
event: Union[str, Events, CallableEventWithFilter, EventsList] = Events.ITERATION_COMPLETED,
) -> None:
"""Attach the handler to the engine. Once the handler is attached, the ``Engine.state`` will have a new
attribute with the name ``param_name``. Then the current value of the parameter can be retrieved from
``Engine.state`` when the engine is running.
Args:
engine: trainer to which the handler will be attached.
event: trigger ``param_name`` value update.
"""
if hasattr(engine.state, self.param_name):
if self.create_new:
raise ValueError(
f"Attribute '{self.param_name}' already exists in the engine.state. "
f"This may be a conflict between multiple handlers. "
f"Please choose another name."
)
else:
if not self.create_new:
warnings.warn(
f"Attribute '{self.param_name}' is not defined in the engine.state. "
f"{type(self).__name__} will create it. Remove this warning by setting create_new=True."
)
setattr(engine.state, self.param_name, None)
if self.save_history:
if not hasattr(engine.state, "param_history") or engine.state.param_history is None:
setattr(engine.state, "param_history", {})
engine.state.param_history.setdefault(self.param_name, []) # type: ignore[attr-defined]
engine.add_event_handler(event, self)
def __call__(self, engine: Engine) -> None:
self.event_index += 1
value = self.get_param()
setattr(engine.state, self.param_name, value)
if self.save_history:
engine.state.param_history[self.param_name].append(value) # type: ignore[attr-defined]
@classmethod
def simulate_values(cls, num_events: int, **scheduler_kwargs: Any) -> List[List[int]]:
"""Method to simulate scheduled engine state parameter values during `num_events` events.
Args:
num_events: number of events during the simulation.
scheduler_kwargs: parameter scheduler configuration kwargs.
Returns:
event_index, value
Examples:
.. code-block:: python
import matplotlib.pyplot as plt
import numpy as np
step_state_param_values = np.array(
StepStateScheduler.simulate_values(
num_events=20, param_name="step_scheduled_param", initial_value=10, gamma=0.99, step_size=5
)
)
plt.plot(step_state_param_values[:, 0], step_state_param_values[:, 1], label="learning rate")
plt.xlabel("events")
plt.ylabel("values")
plt.legend()
"""
for key in ["save_history"]:
if key in scheduler_kwargs:
del scheduler_kwargs[key]
values = []
scheduler = cls(save_history=False, **scheduler_kwargs)
engine = Engine(lambda e, b: None)
for i in range(num_events):
scheduler(engine=engine)
values.append([i, getattr(engine.state, scheduler_kwargs["param_name"])])
return values
class LambdaStateScheduler(StateParamScheduler):
"""Update a parameter during training by using a user defined callable object.
User defined callable object is taking an event index as input and returns parameter value.
Args:
lambda_obj: user defined callable object.
param_name: name of parameter to update.
save_history: whether to log the parameter values to
`engine.state.param_history`, (default=False).
create_new: whether to create ``param_name`` on
``engine.state`` taking into account whether
``param_name`` attribute already exists or not.
Overrides existing attribute by default, (default=False).
Examples:
.. include:: defaults.rst
:start-after: :orphan:
.. testcode::
default_trainer = get_default_trainer()
class LambdaState:
def __init__(self, initial_value, gamma):
self.initial_value = initial_value
self.gamma = gamma
def __call__(self, event_index):
return self.initial_value * self.gamma ** (event_index % 9)
param_scheduler = LambdaStateScheduler(
param_name="param", lambda_obj=LambdaState(1, 0.9), create_new=True
)
# parameter is param, initial_value sets param to 1 and in this example gamma = 1
# using class 'LambdaState' user defined callable object can be created
# update a parameter during training by using a user defined callable object
# user defined callable object is taking an event index as input and returns parameter value
# in this example, we update as initial_value * gamma ** (event_endex % 9)
# in every Epoch the parameter is updated as 1 * 0.9 ** (Epoch % 9)
# In Epoch 3, parameter param = 1 * 0.9 ** (3 % 9) = 0.729
# In Epoch 10, parameter param = 1 * 0.9 ** (10 % 9) = 0.9
param_scheduler.attach(default_trainer, Events.EPOCH_COMPLETED)
@default_trainer.on(Events.EPOCH_COMPLETED)
def print_param():
print(default_trainer.state.param)
default_trainer.run([0], max_epochs=10)
.. testoutput::
0.9
0.81
0.7290...
0.6561
0.5904...
0.5314...
0.4782...
0.4304...
1.0
0.9
.. versionadded:: 0.4.7
"""
def __init__(self, lambda_obj: Any, param_name: str, save_history: bool = False, create_new: bool = False):
super(LambdaStateScheduler, self).__init__(param_name, save_history, create_new)
if not callable(lambda_obj):
raise ValueError("Expected lambda_obj to be callable.")
self.lambda_obj = lambda_obj
self._state_attrs += ["lambda_obj"]
def get_param(self) -> Union[List[float], float]:
return self.lambda_obj(self.event_index)
class PiecewiseLinearStateScheduler(StateParamScheduler):
"""Piecewise linear state parameter scheduler.
Args:
milestones_values: list of tuples (event index, parameter value)
represents milestones and parameter values. Milestones should be increasing integers.
param_name: name of parameter to update.
save_history: whether to log the parameter values to
`engine.state.param_history`, (default=False).
create_new: whether to create ``param_name`` on
``engine.state`` taking into account whether
``param_name`` attribute already exists or not.
Overrides existing attribute by default, (default=False).
Examples:
.. include:: defaults.rst
:start-after: :orphan:
.. testcode::
default_trainer = get_default_trainer()
param_scheduler = PiecewiseLinearStateScheduler(
param_name="param", milestones_values=[(5, 1.0), (10, 0.8), (15, 0.6)], create_new=True
)
# parameter is param, milestone (5, 1.0) sets param to 1.0
# milestone is (5, 1.0), param=1 for Epoch 1 to 5,
# next milestone is (10, 0.8), param linearly reduces from 1.0 to 0.8
# Epoch 10, param = 0.8
# next milestone is (15,0.6), param linearly reduces from 0.8 to 0.6
# Epoch 15, param = 0.6
param_scheduler.attach(default_trainer, Events.EPOCH_COMPLETED)
@default_trainer.on(Events.EPOCH_COMPLETED)
def print_param():
print(default_trainer.state.param)
default_trainer.run([0], max_epochs=15)
.. testoutput::
1.0
1.0
1.0
1.0
1.0
0.96
0.92
0.88
0.8400...
0.8
0.76
0.72
0.68
0.64
0.6
.. versionadded:: 0.4.7
"""
def __init__(
self,
milestones_values: List[Tuple[int, float]],
param_name: str,
save_history: bool = False,
create_new: bool = False,
):
super(PiecewiseLinearStateScheduler, self).__init__(param_name, save_history, create_new)
if not isinstance(milestones_values, Sequence):
raise TypeError(
f"Argument milestones_values should be a list or tuple, but given {type(milestones_values)}"
)
if len(milestones_values) < 1:
raise ValueError(
f"Argument milestones_values should be with at least one value, but given {milestones_values}"
)
values: List[float] = []
milestones: List[int] = []
for pair in milestones_values:
if not isinstance(pair, tuple) or len(pair) != 2:
raise ValueError("Argument milestones_values should be a list of pairs (milestone, param_value)")
if not isinstance(pair[0], numbers.Integral):
raise TypeError(f"Value of a milestone should be integer, but given {type(pair[0])}")
if len(milestones) > 0 and pair[0] < milestones[-1]:
raise ValueError(
f"Milestones should be increasing integers, but given {pair[0]} is smaller "
f"than the previous milestone {milestones[-1]}"
)
milestones.append(pair[0])
values.append(pair[1])
self.values = values
self.milestones = milestones
self._index = 0
self._state_attrs += ["values", "milestones", "_index"]
def _get_start_end(self) -> Tuple[int, int, float, float]:
if self.milestones[0] > self.event_index:
return self.event_index - 1, self.event_index, self.values[0], self.values[0]
elif self.milestones[-1] <= self.event_index:
return (self.event_index, self.event_index + 1, self.values[-1], self.values[-1])
elif self.milestones[self._index] <= self.event_index < self.milestones[self._index + 1]:
return (
self.milestones[self._index],
self.milestones[self._index + 1],
self.values[self._index],
self.values[self._index + 1],
)
else:
self._index += 1
return self._get_start_end()
def get_param(self) -> Union[List[float], float]:
start_index, end_index, start_value, end_value = self._get_start_end()
return start_value + (end_value - start_value) * (self.event_index - start_index) / (end_index - start_index)
class ExpStateScheduler(StateParamScheduler):
"""Update a parameter during training by using exponential function.
The function decays the parameter value by gamma every step.
Based on the closed form of ExponentialLR from PyTorch
https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.ExponentialLR.html
Args:
initial_value: Starting value of the parameter.
gamma: Multiplicative factor of parameter value decay.
param_name: name of parameter to update.
save_history: whether to log the parameter values to
`engine.state.param_history`, (default=False).
create_new: whether to create ``param_name`` on
``engine.state`` taking into account whether
``param_name`` attribute already exists or not.
Overrides existing attribute by default, (default=False).
Examples:
.. include:: defaults.rst
:start-after: :orphan:
.. testcode::
default_trainer = get_default_trainer()
param_scheduler = ExpStateScheduler(
param_name="param", initial_value=1, gamma=0.9, create_new=True
)
# parameter is param, initial_value sets param to 1, gamma is set as 0.9
# Epoch 1, param changes from 1 to 1*0.9, param = 0.9
# Epoch 2, param changes from 0.9 to 0.9*0.9, param = 0.81
# Epoch 3, param changes from 0.81 to 0.81*0.9, param = 0.729
# Epoch 4, param changes from 0.81 to 0.729*0.9, param = 0.6561
param_scheduler.attach(default_trainer, Events.EPOCH_COMPLETED)
@default_trainer.on(Events.EPOCH_COMPLETED)
def print_param():
print(default_trainer.state.param)
default_trainer.run([0], max_epochs=4)
.. testoutput::
0.9
0.81
0.7290...
0.6561
.. versionadded:: 0.4.7
"""
def __init__(
self, initial_value: float, gamma: float, param_name: str, save_history: bool = False, create_new: bool = False
):
super(ExpStateScheduler, self).__init__(param_name, save_history, create_new)
self.initial_value = initial_value
self.gamma = gamma
self._state_attrs += ["initial_value", "gamma"]
def get_param(self) -> Union[List[float], float]:
return self.initial_value * self.gamma**self.event_index
class StepStateScheduler(StateParamScheduler):
"""Update a parameter during training by using a step function.
This function decays the parameter value by gamma every step_size.
Based on StepLR from PyTorch.
https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.StepLR.html
Args:
initial_value: Starting value of the parameter.
gamma: Multiplicative factor of parameter value decay.
step_size: Period of parameter value decay.
param_name: name of parameter to update.
save_history: whether to log the parameter values to
`engine.state.param_history`, (default=False).
create_new: whether to create ``param_name`` on
``engine.state`` taking into account whether
``param_name`` attribute already exists or not.
Overrides existing attribute by default, (default=False).
Examples:
.. include:: defaults.rst
:start-after: :orphan:
.. testcode::
default_trainer = get_default_trainer()
param_scheduler = StepStateScheduler(
param_name="param", initial_value=1, gamma=0.9, step_size=5, create_new=True
)
# parameter is param, initial_value sets param to 1, gamma is set as 0.9
# Epoch 1 to 4, param does not change as step size is 5,
# Epoch 5, param changes from 1 to 1*0.9, param = 0.9
# Epoch 5 to 9, param = 0.9 as step size is 5,
# Epoch 10, param changes from 0.9 to 0.9*0.9, param = 0.81
# Epoch 10 to 14, param = 0.81, as step size is 5
# Epoch 15, param changes from 0.81 to 0.81*0.9, param = 0.729
# and so on ... the param change at Epoch = 5, 10, 15, 20, . . .
param_scheduler.attach(default_trainer, Events.EPOCH_COMPLETED)
@default_trainer.on(Events.EPOCH_COMPLETED(every=5))
def print_param():
print(default_trainer.state.param)
default_trainer.run([0], max_epochs=25)
.. testoutput::
0.9
0.81
0.7290...
0.6561
0.5904...
.. versionadded:: 0.4.7
"""
def __init__(
self,
initial_value: float,
gamma: float,
step_size: int,
param_name: str,
save_history: bool = False,
create_new: bool = False,
):
super(StepStateScheduler, self).__init__(param_name, save_history, create_new)
self.initial_value = initial_value
self.gamma = gamma
self.step_size = step_size
self._state_attrs += ["initial_value", "gamma", "step_size"]
def get_param(self) -> Union[List[float], float]:
return self.initial_value * self.gamma ** (self.event_index // self.step_size)
class MultiStepStateScheduler(StateParamScheduler):
"""Update a parameter during training by using a multi step function.
The function decays the parameter value by gamma once the number of steps reaches one of the milestones.
Based on MultiStepLR from PyTorch.
https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.MultiStepLR.html
Args:
initial_value: Starting value of the parameter.
gamma: Multiplicative factor of parameter value decay.
milestones: List of step indices. Must be increasing.
param_name: name of parameter to update.
save_history: whether to log the parameter values to
`engine.state.param_history`, (default=False).
create_new: whether to create ``param_name`` on
``engine.state`` taking into account whether
``param_name`` attribute already exists or not.
Overrides existing attribute by default, (default=False).
Examples:
.. include:: defaults.rst
:start-after: :orphan:
.. testcode::
default_trainer = get_default_trainer()
param_scheduler = MultiStepStateScheduler(
param_name="param", initial_value=1, gamma=0.9, milestones=[3, 6, 9, 12], create_new=True
)
# parameter is param, initial_value sets param to 1, gamma is set as 0.9
# Epoch 1 to 2, param does not change as milestone is 3
# Epoch 3, param changes from 1 to 1*0.9, param = 0.9
# Epoch 3 to 5, param does not change as milestone is 6
# Epoch 6, param changes from 0.9 to 0.9*0.9, param = 0.81
# Epoch 6 to 8, param does not change as milestone is 9
# Epoch 9, param changes from 0.81 to 0.81*0.9, param = 0.729
# Epoch 9 to 11, param does not change as milestone is 12
# Epoch 12, param changes from 0.729 to 0.729*0.9, param = 0.6561
param_scheduler.attach(default_trainer, Events.EPOCH_COMPLETED)
@default_trainer.on(Events.EPOCH_COMPLETED)
def print_param():
print(default_trainer.state.param)
default_trainer.run([0], max_epochs=12)
.. testoutput::
1.0
1.0
0.9
0.9
0.9
0.81
0.81
0.81
0.7290...
0.7290...
0.7290...
0.6561
.. versionadded:: 0.4.7
"""
def __init__(
self,
initial_value: float,
gamma: float,
milestones: List[int],
param_name: str,
save_history: bool = False,
create_new: bool = False,
):
super(MultiStepStateScheduler, self).__init__(param_name, save_history, create_new)
self.initial_value = initial_value
self.gamma = gamma
self.milestones = milestones
self._state_attrs += ["initial_value", "gamma", "milestones"]
def get_param(self) -> Union[List[float], float]:
return self.initial_value * self.gamma ** bisect_right(self.milestones, self.event_index)
|