當前位置: 首頁>>代碼示例>>Python>>正文


Python Events.COMPLETED屬性代碼示例

本文整理匯總了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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:19,代碼來源:common.py

示例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) 
開發者ID:pytorch,項目名稱:ignite,代碼行數:21,代碼來源:test_engine.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:24,代碼來源:test_engine.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:23,代碼來源:test_engine_state_dict.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:27,代碼來源:test_event_handlers.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:27,代碼來源:test_event_handlers.py

示例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) 
開發者ID:pytorch,項目名稱:ignite,代碼行數:20,代碼來源:test_event_handlers.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:21,代碼來源:test_event_handlers.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:27,代碼來源:test_early_stopping.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:25,代碼來源:test_early_stopping.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:27,代碼來源:test_early_stopping.py

示例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 
開發者ID:pytorch,項目名稱:ignite,代碼行數:19,代碼來源:test_tqdm_logger.py

示例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)) 
開發者ID:pytorch,項目名稱:ignite,代碼行數:24,代碼來源:test_tqdm_logger.py

示例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 
開發者ID:hrhodin,項目名稱:UnsupervisedGeometryAwareRepresentationLearning,代碼行數:32,代碼來源:timing.py

示例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 
開發者ID:leokarlin,項目名稱:LaSO,代碼行數:32,代碼來源:timing.py


注:本文中的ignite.engine.Events.COMPLETED屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。