本文整理汇总了Python中ignite.engine.Events.COMPLETED属性的典型用法代码示例。如果您正苦于以下问题:Python Events.COMPLETED属性的具体用法?Python Events.COMPLETED怎么用?Python Events.COMPLETED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类ignite.engine.Events
的用法示例。
在下文中一共展示了Events.COMPLETED属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_early_stopping_by_val_score
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def add_early_stopping_by_val_score(patience, evaluator, trainer, metric_name):
"""Method setups early stopping handler based on the score (named by `metric_name`) provided by `evaluator`.
Args:
patience (int): number of events to wait if no improvement and then stop the training.
evaluator (Engine): evaluation engine used to provide the score
trainer (Engine): trainer engine to stop the run if no improvement.
metric_name (str): metric name to use for score evaluation. This metric should be present in
`evaluator.state.metrics`.
Returns:
A :class:`~ignite.handlers.early_stopping.EarlyStopping` handler.
"""
es_handler = EarlyStopping(patience=patience, score_function=get_default_score_fn(metric_name), trainer=trainer)
evaluator.add_event_handler(Events.COMPLETED, es_handler)
return es_handler
示例2: test_time_stored_in_state
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_time_stored_in_state():
def _test(data, max_epochs, epoch_length):
sleep_time = 0.01
engine = Engine(lambda e, b: time.sleep(sleep_time))
def check_epoch_time(engine):
assert engine.state.times[Events.EPOCH_COMPLETED.name] >= sleep_time * epoch_length
def check_completed_time(engine):
assert engine.state.times[Events.COMPLETED.name] >= sleep_time * epoch_length * max_epochs
engine.add_event_handler(Events.EPOCH_COMPLETED, lambda e: check_epoch_time(e))
engine.add_event_handler(Events.COMPLETED, lambda e: check_completed_time(e))
engine.run(data, max_epochs=max_epochs, epoch_length=epoch_length)
_test(list(range(100)), max_epochs=2, epoch_length=100)
_test(list(range(200)), max_epochs=2, epoch_length=100)
_test(list(range(200)), max_epochs=5, epoch_length=100)
示例3: test_run_once_finite_iterator_no_epoch_length
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_run_once_finite_iterator_no_epoch_length():
# FR: https://github.com/pytorch/ignite/issues/871
unknown_size = 11
def finite_unk_size_data_iter():
for i in range(unknown_size):
yield i
bc = BatchChecker(data=list(range(unknown_size)))
engine = Engine(lambda e, b: bc.check(b))
completed_handler = MagicMock()
engine.add_event_handler(Events.COMPLETED, completed_handler)
data_iter = finite_unk_size_data_iter()
engine.run(data_iter)
assert engine.state.epoch == 1
assert engine.state.iteration == unknown_size
assert completed_handler.call_count == 1
示例4: test_state_dict_with_user_keys_integration
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_state_dict_with_user_keys_integration(dirname):
engine = Engine(lambda e, b: 1)
engine.state_dict_user_keys.append("alpha")
@engine.on(Events.STARTED)
def init_user_values(_):
engine.state.alpha = 0.1
fp = os.path.join(dirname, "engine.pt")
@engine.on(Events.COMPLETED)
def save_engine(_):
state_dict = engine.state_dict()
assert "alpha" in state_dict
torch.save(state_dict, fp)
engine.run([0, 1])
assert os.path.exists(fp)
state_dict = torch.load(fp)
assert "alpha" in state_dict and state_dict["alpha"] == 0.1
示例5: test_add_event_handler
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_add_event_handler():
engine = DummyEngine()
class Counter(object):
def __init__(self, count=0):
self.count = count
started_counter = Counter()
def handle_iteration_started(engine, counter):
counter.count += 1
engine.add_event_handler(Events.STARTED, handle_iteration_started, started_counter)
completed_counter = Counter()
def handle_iteration_completed(engine, counter):
counter.count += 1
engine.add_event_handler(Events.COMPLETED, handle_iteration_completed, completed_counter)
engine.run(15)
assert started_counter.count == 15
assert completed_counter.count == 15
示例6: test_add_event_handler_without_engine
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_add_event_handler_without_engine():
engine = DummyEngine()
class Counter(object):
def __init__(self, count=0):
self.count = count
started_counter = Counter()
def handle_iteration_started():
started_counter.count += 1
engine.add_event_handler(Events.STARTED, handle_iteration_started)
completed_counter = Counter()
def handle_iteration_completed(counter):
counter.count += 1
engine.add_event_handler(Events.COMPLETED, handle_iteration_completed, completed_counter)
engine.run(15)
assert started_counter.count == 15
assert completed_counter.count == 15
示例7: test_has_event_handler
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_has_event_handler():
engine = DummyEngine()
handlers = [MagicMock(spec_set=True), MagicMock(spec_set=True)]
m = MagicMock(spec_set=True)
for handler in handlers:
engine.add_event_handler(Events.STARTED, handler)
engine.add_event_handler(Events.COMPLETED, m)
for handler in handlers:
assert engine.has_event_handler(handler, Events.STARTED)
assert engine.has_event_handler(handler)
assert not engine.has_event_handler(handler, Events.COMPLETED)
assert not engine.has_event_handler(handler, Events.EPOCH_STARTED)
assert not engine.has_event_handler(m, Events.STARTED)
assert engine.has_event_handler(m, Events.COMPLETED)
assert engine.has_event_handler(m)
assert not engine.has_event_handler(m, Events.EPOCH_STARTED)
示例8: test_args_and_kwargs_are_passed_to_event
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_args_and_kwargs_are_passed_to_event():
engine = DummyEngine()
kwargs = {"a": "a", "b": "b"}
args = (1, 2, 3)
handlers = []
for event in [Events.STARTED, Events.COMPLETED]:
handler = create_autospec(spec=lambda e, x1, x2, x3, a, b: None)
engine.add_event_handler(event, handler, *args, **kwargs)
handlers.append(handler)
engine.run(1)
called_handlers = [handle for handle in handlers if handle.called]
assert len(called_handlers) == 2
for handler in called_handlers:
handler_args, handler_kwargs = handler.call_args
assert handler_args[0] == engine
assert handler_args[1::] == args
assert handler_kwargs == kwargs
示例9: test_with_engine_early_stopping
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_with_engine_early_stopping():
class Counter(object):
def __init__(self, count=0):
self.count = count
n_epochs_counter = Counter()
scores = iter([1.0, 0.8, 1.2, 1.5, 0.9, 1.0, 0.99, 1.1, 0.9])
def score_function(engine):
return next(scores)
trainer = Engine(do_nothing_update_fn)
evaluator = Engine(do_nothing_update_fn)
early_stopping = EarlyStopping(patience=3, score_function=score_function, trainer=trainer)
@trainer.on(Events.EPOCH_COMPLETED)
def evaluation(engine):
evaluator.run([0])
n_epochs_counter.count += 1
evaluator.add_event_handler(Events.COMPLETED, early_stopping)
trainer.run([0], max_epochs=10)
assert n_epochs_counter.count == 7
assert trainer.state.epoch == 7
示例10: test_with_engine_early_stopping_on_plateau
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_with_engine_early_stopping_on_plateau():
class Counter(object):
def __init__(self, count=0):
self.count = count
n_epochs_counter = Counter()
def score_function(engine):
return 0.047
trainer = Engine(do_nothing_update_fn)
evaluator = Engine(do_nothing_update_fn)
early_stopping = EarlyStopping(patience=4, score_function=score_function, trainer=trainer)
@trainer.on(Events.EPOCH_COMPLETED)
def evaluation(engine):
evaluator.run([0])
n_epochs_counter.count += 1
evaluator.add_event_handler(Events.COMPLETED, early_stopping)
trainer.run([0], max_epochs=10)
assert n_epochs_counter.count == 5
assert trainer.state.epoch == 5
示例11: test_with_engine_no_early_stopping
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_with_engine_no_early_stopping():
class Counter(object):
def __init__(self, count=0):
self.count = count
n_epochs_counter = Counter()
scores = iter([1.0, 0.8, 1.2, 1.23, 0.9, 1.0, 1.1, 1.253, 1.26, 1.2])
def score_function(engine):
return next(scores)
trainer = Engine(do_nothing_update_fn)
evaluator = Engine(do_nothing_update_fn)
early_stopping = EarlyStopping(patience=5, score_function=score_function, trainer=trainer)
@trainer.on(Events.EPOCH_COMPLETED)
def evaluation(engine):
evaluator.run([0])
n_epochs_counter.count += 1
evaluator.add_event_handler(Events.COMPLETED, early_stopping)
trainer.run([0], max_epochs=10)
assert n_epochs_counter.count == 10
assert trainer.state.epoch == 10
示例12: test_pbar_on_epochs
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_pbar_on_epochs(capsys):
n_epochs = 10
loader = [1, 2, 3, 4, 5]
engine = Engine(update_fn)
pbar = ProgressBar()
pbar.attach(engine, event_name=Events.EPOCH_STARTED, closing_event_name=Events.COMPLETED)
engine.run(loader, max_epochs=n_epochs)
captured = capsys.readouterr()
err = captured.err.split("\r")
err = list(map(lambda x: x.strip(), err))
err = list(filter(None, err))
actual = err[-1]
expected = "Epoch: [9/10] 90%|█████████ [00:00<00:00]"
assert actual == expected
示例13: test_pbar_wrong_events_order
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def test_pbar_wrong_events_order():
engine = Engine(update_fn)
pbar = ProgressBar()
with pytest.raises(ValueError, match="should be called before closing event"):
pbar.attach(engine, event_name=Events.COMPLETED, closing_event_name=Events.COMPLETED)
with pytest.raises(ValueError, match="should be called before closing event"):
pbar.attach(engine, event_name=Events.COMPLETED, closing_event_name=Events.EPOCH_COMPLETED)
with pytest.raises(ValueError, match="should be called before closing event"):
pbar.attach(engine, event_name=Events.COMPLETED, closing_event_name=Events.ITERATION_COMPLETED)
with pytest.raises(ValueError, match="should be called before closing event"):
pbar.attach(engine, event_name=Events.EPOCH_COMPLETED, closing_event_name=Events.EPOCH_COMPLETED)
with pytest.raises(ValueError, match="should be called before closing event"):
pbar.attach(engine, event_name=Events.ITERATION_COMPLETED, closing_event_name=Events.ITERATION_STARTED)
with pytest.raises(ValueError, match="should not be a filtered event"):
pbar.attach(engine, event_name=Events.ITERATION_STARTED, closing_event_name=Events.EPOCH_COMPLETED(every=10))
示例14: attach
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def attach(self, engine, start=Events.STARTED, pause=Events.COMPLETED, resume=None, step=None):
""" Register callbacks to control the timer.
Args:
engine (ignite.engine.Engine):
Engine that this timer will be attached to
start (ignite.engine.Events):
Event which should start (reset) the timer
pause (ignite.engine.Events):
Event which should pause the timer
resume (ignite.engine.Events, optional):
Event which should resume the timer
step (ignite.engine.Events, optional):
Event which should call the `step` method of the counter
Returns:
self (Timer)
"""
engine.add_event_handler(start, self.reset)
engine.add_event_handler(pause, self.pause)
if resume is not None:
engine.add_event_handler(resume, self.resume)
if step is not None:
engine.add_event_handler(step, self.step)
return self
示例15: attach
# 需要导入模块: from ignite.engine import Events [as 别名]
# 或者: from ignite.engine.Events import COMPLETED [as 别名]
def attach(self, engine, start=Events.STARTED, pause=Events.COMPLETED, resume=None, step=None):
""" Register callbacks to control the timer.
Args:
engine (Engine):
Engine that this timer will be attached to.
start (Events):
Event which should start (reset) the timer.
pause (Events):
Event which should pause the timer.
resume (Events, optional):
Event which should resume the timer.
step (Events, optional):
Event which should call the `step` method of the counter.
Returns:
self (Timer)
"""
engine.add_event_handler(start, self.reset)
engine.add_event_handler(pause, self.pause)
if resume is not None:
engine.add_event_handler(resume, self.resume)
if step is not None:
engine.add_event_handler(step, self.step)
return self