ray.tune.stopper.Stopper#

class ray.tune.stopper.Stopper[源代码]#

基类:ABC

用于实现 Tune 实验停止器的基类。

允许用户通过 stop_all 实现实验级别的停止。默认情况下,此类不会停止任何试验。子类需要实现 __call__stop_all

示例

>>> import time
>>> from ray import train, tune
>>> from ray.tune import Stopper
>>>
>>> class TimeStopper(Stopper):
...     def __init__(self):
...         self._start = time.time()
...         self._deadline = 2  # Stop all trials after 2 seconds
...
...     def __call__(self, trial_id, result):
...         return False
...
...     def stop_all(self):
...         return time.time() - self._start > self._deadline
...
>>> def train_fn(config):
...     for i in range(100):
...         time.sleep(1)
...         train.report({"iter": i})
...
>>> tuner = tune.Tuner(
...     train_fn,
...     tune_config=tune.TuneConfig(num_samples=2),
...     run_config=train.RunConfig(stop=TimeStopper()),
... )
>>> print("[ignore]"); result_grid = tuner.fit()  
[ignore]...

方法

__init__

stop_all

如果实验应终止,则返回 true。