diff --git a/README.md b/README.md index 2e1621e4309b..1c4ed6479dd3 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,9 @@ Examples Custom events related to backward and optimizer step calls: ```python +from ignite.engine import EventEnum + + class BackpropEvents(EventEnum): BACKWARD_STARTED = 'backward_started' BACKWARD_COMPLETED = 'backward_completed' diff --git a/docs/source/concepts.rst b/docs/source/concepts.rst index f6b4a074f6e3..add2345be1ca 100644 --- a/docs/source/concepts.rst +++ b/docs/source/concepts.rst @@ -234,6 +234,8 @@ and be registered with :meth:`~ignite.engine.engine.Engine.register_events` in a .. code-block:: python + from ignite.engine import EventEnum + class CustomEvents(EventEnum): """ Custom events defined by user diff --git a/docs/source/engine.rst b/docs/source/engine.rst index a5fa15d322a7..807f62b21487 100644 --- a/docs/source/engine.rst +++ b/docs/source/engine.rst @@ -92,6 +92,8 @@ ignite.engine.events .. autoclass:: Events :members: +.. autoclass:: EventEnum + .. autoclass:: State .. autoclass:: RemovableEventHandle diff --git a/docs/source/faq.rst b/docs/source/faq.rst index c930d2d7e71e..9ef9563ccc53 100644 --- a/docs/source/faq.rst +++ b/docs/source/faq.rst @@ -47,6 +47,8 @@ flexibility to the user to allow for this: .. code-block:: python + from ignite.engine import EventEnum + class BackpropEvents(EventEnum): """ Events based on back propagation diff --git a/ignite/engine/events.py b/ignite/engine/events.py index 296629c9614c..7cf2b4627a59 100644 --- a/ignite/engine/events.py +++ b/ignite/engine/events.py @@ -142,6 +142,36 @@ def __init__(self, *args, **kwargs): class EventEnum(CallableEventWithFilter, Enum): + """Base class for all :class:`~ignite.engine.events.Events`. User defined custom events should also inherit + this class. For example, Custom events based on the loss calculation and backward pass can be created as follows: + + .. code-block:: python + + from ignite.engine import EventEnum + + class BackpropEvents(EventEnum): + BACKWARD_STARTED = 'backward_started' + BACKWARD_COMPLETED = 'backward_completed' + OPTIM_STEP_COMPLETED = 'optim_step_completed' + + def update(engine, batch): + # ... + loss = criterion(y_pred, y) + engine.fire_event(BackpropEvents.BACKWARD_STARTED) + loss.backward() + engine.fire_event(BackpropEvents.BACKWARD_COMPLETED) + optimizer.step() + engine.fire_event(BackpropEvents.OPTIM_STEP_COMPLETED) + # ... + + trainer = Engine(update) + trainer.register_events(*BackpropEvents) + + @trainer.on(BackpropEvents.BACKWARD_STARTED) + def function_before_backprop(engine): + # ... + """ + pass