Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

give an option to store EpochOutputStore data on engine.state #1974

Merged
merged 4 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions ignite/contrib/handlers/stores.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, List, Tuple, Union
from typing import Callable, List, Optional, Tuple, Union

from ignite.engine import Engine, Events

Expand All @@ -21,15 +21,17 @@ class EpochOutputStore:
eos = EpochOutputStore()
trainer = create_supervised_trainer(model, optimizer, loss)
train_evaluator = create_supervised_evaluator(model, metrics)
eos.attach(train_evaluator)
eos.attach(train_evaluator, 'output')

@trainer.on(Events.EPOCH_COMPLETED)
def log_training_results(engine):
train_evaluator.run(train_loader)
output = eos.data
output = train_evaluator.output
# do something with output, e.g., plotting

.. versionadded:: 0.4.2
.. versionchanged:: 0.5.0
`attach` now accepts an optional argument `name`
"""

def __init__(self, output_transform: Callable = lambda x: x):
Expand All @@ -45,8 +47,19 @@ def update(self, engine: Engine) -> None:
output = self.output_transform(engine.state.output)
self.data.append(output)

def attach(self, engine: Engine) -> None:
def store(self, engine: Engine) -> None:
vfdev-5 marked this conversation as resolved.
Show resolved Hide resolved
"""Store `self.data` on `engine.state.{self.name}`"""
setattr(engine.state, self.name, self.data)

def attach(self, engine: Engine, name: Optional[str] = None) -> None:
"""Attaching `reset` method at EPOCH_STARTED and
`update` method at ITERATION_COMPLETED."""
`update` method at ITERATION_COMPLETED.

If `name` is passed, will store `self.data` on `engine.state`
under `name`.
"""
engine.add_event_handler(Events.EPOCH_STARTED, self.reset)
engine.add_event_handler(Events.ITERATION_COMPLETED, self.update)
if name:
self.name = name
engine.add_event_handler(Events.EPOCH_COMPLETED, self.store)
8 changes: 7 additions & 1 deletion tests/ignite/contrib/handlers/test_stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_no_transform(dummy_evaluator, eos):
assert eos.data == [(1, 0)]


def test_tranform(dummy_evaluator):
def test_transform(dummy_evaluator):
eos = EpochOutputStore(output_transform=lambda x: x[0])
eos.attach(dummy_evaluator)

Expand Down Expand Up @@ -56,3 +56,9 @@ def test_attatch(dummy_evaluator, eos):
eos.attach(dummy_evaluator)
assert dummy_evaluator.has_event_handler(eos.reset, Events.EPOCH_STARTED)
assert dummy_evaluator.has_event_handler(eos.update, Events.ITERATION_COMPLETED)


def test_store_data(dummy_evaluator, eos):
eos.attach(dummy_evaluator, name="eval_data")
dummy_evaluator.run(range(1))
assert dummy_evaluator.state.eval_data == eos.data