|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import collections |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +from ray.tune.trial_scheduler import FIFOScheduler, TrialScheduler |
| 9 | + |
| 10 | + |
| 11 | +class MedianStoppingRule(FIFOScheduler): |
| 12 | + """Implements the median stopping rule as described in the Vizier paper: |
| 13 | +
|
| 14 | + https://research.google.com/pubs/pub46180.html |
| 15 | +
|
| 16 | + Args: |
| 17 | + time_attr (str): The TrainingResult attr to use for comparing time. |
| 18 | + Note that you can pass in something non-temporal such as |
| 19 | + `training_iteration` as a measure of progress, the only requirement |
| 20 | + is that the attribute should increase monotonically. |
| 21 | + reward_attr (str): The TrainingResult objective value attribute. As |
| 22 | + with `time_attr`, this may refer to any objective value that |
| 23 | + is supposed to increase with time. |
| 24 | + grace_period (float): Only stop trials at least this old in time. |
| 25 | + The units are the same as the attribute named by `time_attr`. |
| 26 | + min_samples_required (int): Min samples to compute median over. |
| 27 | + hard_stop (bool): If false, pauses trials instead of stopping |
| 28 | + them. When all other trials are complete, paused trials will be |
| 29 | + resumed and allowed to run FIFO. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, time_attr='time_total_s', reward_attr='episode_reward_mean', |
| 34 | + grace_period=60.0, min_samples_required=3, hard_stop=True): |
| 35 | + FIFOScheduler.__init__(self) |
| 36 | + self._stopped_trials = set() |
| 37 | + self._completed_trials = set() |
| 38 | + self._results = collections.defaultdict(list) |
| 39 | + self._grace_period = grace_period |
| 40 | + self._min_samples_required = min_samples_required |
| 41 | + self._reward_attr = reward_attr |
| 42 | + self._time_attr = time_attr |
| 43 | + self._hard_stop = hard_stop |
| 44 | + |
| 45 | + def on_trial_result(self, trial_runner, trial, result): |
| 46 | + """Callback for early stopping. |
| 47 | +
|
| 48 | + This stopping rule stops a running trial if the trial's best objective |
| 49 | + value by step `t` is strictly worse than the median of the running |
| 50 | + averages of all completed trials' objectives reported up to step `t`. |
| 51 | + """ |
| 52 | + |
| 53 | + if trial in self._stopped_trials: |
| 54 | + assert not self._hard_stop |
| 55 | + return TrialScheduler.CONTINUE # fall back to FIFO |
| 56 | + |
| 57 | + time = getattr(result, self._time_attr) |
| 58 | + self._results[trial].append(result) |
| 59 | + median_result = self._get_median_result(time) |
| 60 | + best_result = self._best_result(trial) |
| 61 | + print("Trial {} best res={} vs median res={} at t={}".format( |
| 62 | + trial, best_result, median_result, time)) |
| 63 | + if best_result < median_result and time > self._grace_period: |
| 64 | + print("MedianStoppingRule: early stopping {}".format(trial)) |
| 65 | + self._stopped_trials.add(trial) |
| 66 | + if self._hard_stop: |
| 67 | + return TrialScheduler.STOP |
| 68 | + else: |
| 69 | + return TrialScheduler.PAUSE |
| 70 | + else: |
| 71 | + return TrialScheduler.CONTINUE |
| 72 | + |
| 73 | + def on_trial_complete(self, trial_runner, trial, result): |
| 74 | + self._results[trial].append(result) |
| 75 | + self._completed_trials.add(trial) |
| 76 | + |
| 77 | + def debug_string(self): |
| 78 | + return "Using MedianStoppingRule: num_stopped={}.".format( |
| 79 | + len(self._stopped_trials)) |
| 80 | + |
| 81 | + def _get_median_result(self, time): |
| 82 | + scores = [] |
| 83 | + for trial in self._completed_trials: |
| 84 | + scores.append(self._running_result(trial, time)) |
| 85 | + if len(scores) >= self._min_samples_required: |
| 86 | + return np.median(scores) |
| 87 | + else: |
| 88 | + return float('-inf') |
| 89 | + |
| 90 | + def _running_result(self, trial, t_max=float('inf')): |
| 91 | + results = self._results[trial] |
| 92 | + # TODO(ekl) we could do interpolation to be more precise, but for now |
| 93 | + # assume len(results) is large and the time diffs are roughly equal |
| 94 | + return np.mean( |
| 95 | + [getattr(r, self._reward_attr) |
| 96 | + for r in results if getattr(r, self._time_attr) <= t_max]) |
| 97 | + |
| 98 | + def _best_result(self, trial): |
| 99 | + results = self._results[trial] |
| 100 | + return max([getattr(r, self._reward_attr) for r in results]) |
0 commit comments