Change the param by monitoring the change of a scalar statistics. The param will be changed when the scalar does not decrease/increase enough. Once triggered, this callback observes the latest **one** value of ``stat_name``, from the monitor backend. This callback will then change
| 360 | |
| 361 | |
| 362 | class StatMonitorParamSetter(HyperParamSetter): |
| 363 | """ |
| 364 | Change the param by monitoring the change of a scalar statistics. |
| 365 | The param will be changed when the scalar does not decrease/increase enough. |
| 366 | |
| 367 | Once triggered, this callback observes the latest **one** value of ``stat_name``, from the monitor backend. |
| 368 | |
| 369 | This callback will then change a hyperparameter ``param`` by ``new_value = value_func(old_value)``, if: |
| 370 | ``min(history) >= history[0] - threshold``, where |
| 371 | ``history = [the most recent k observations of stat_name]`` |
| 372 | |
| 373 | Note: |
| 374 | The statistics of interest must be created at a frequency higher than or equal to this callback. |
| 375 | For example, using ``PeriodicTrigger(StatMonitorParamSetter(...), every_k_steps=100)`` |
| 376 | is meaningless if the statistics to be monitored is only updated every 500 steps. |
| 377 | |
| 378 | Callbacks are executed in order. Therefore, if the statistics to be monitored |
| 379 | is created after this callback, the behavior of this callback may get delayed. |
| 380 | |
| 381 | Example: |
| 382 | |
| 383 | If validation error wasn't decreasing for 5 epochs, decay the learning rate by 0.2: |
| 384 | |
| 385 | .. code-block:: python |
| 386 | |
| 387 | StatMonitorParamSetter('learning_rate', 'val-error', |
| 388 | lambda x: x * 0.2, threshold=0, last_k=5) |
| 389 | """ |
| 390 | |
| 391 | _enable_before_train = False |
| 392 | |
| 393 | def __init__(self, param, stat_name, value_func, threshold, |
| 394 | last_k, reverse=False): |
| 395 | """ |
| 396 | Args: |
| 397 | param: same as in :class:`HyperParamSetter`. |
| 398 | stat_name (str): name of the statistics. |
| 399 | value_func (float -> float): a function which returns a new value |
| 400 | taking the old value. |
| 401 | threshold (float): change threshold. |
| 402 | last_k (int): use last k observations of statistics. |
| 403 | reverse (bool): monitor increasing instead of decreasing. |
| 404 | If True, ``param`` will be changed when ``max(history) <= history[0] + threshold``. |
| 405 | """ |
| 406 | super(StatMonitorParamSetter, self).__init__(param) |
| 407 | self.stat_name = stat_name |
| 408 | self.value_func = value_func |
| 409 | self.history = deque(maxlen=last_k) |
| 410 | self.threshold = threshold |
| 411 | self.reverse = reverse |
| 412 | |
| 413 | def _get_value_to_set(self): |
| 414 | try: |
| 415 | last = self.trainer.monitors.get_history(self.stat_name)[-1] |
| 416 | except (KeyError, IndexError): |
| 417 | logger.warn( |
| 418 | "[StatMonitorParamSetter] No history data available for key '{}'.".format(self.stat_name)) |
| 419 | return None |
no outgoing calls
no test coverage detected
searching dependent graphs…