Skip to content

Commit 0f0920d

Browse files
authored
Added state attributes for Polyaxon Logger (#2161)
* added state attributes for Polyaxon Logger * autopep8 fix Co-authored-by: Ishan-Kumar2 <[email protected]>
1 parent 327cec5 commit 0f0920d

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

ignite/contrib/handlers/polyaxon_logger.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ def global_step_transform(*args, **kwargs):
190190
global_step_transform=global_step_transform
191191
)
192192
193+
Another example where the State Attributes ``trainer.state.alpha`` and ``trainer.state.beta``
194+
are also logged along with the NLL and Accuracy after each iteration:
195+
196+
.. code-block:: python
197+
198+
plx_logger.attach_output_handler(
199+
trainer,
200+
event_name=Events.ITERATION_COMPLETED,
201+
tag="training",
202+
metrics=["nll", "accuracy"],
203+
state_attributes=["alpha", "beta"],
204+
)
205+
193206
Args:
194207
tag: common title for all produced plots. For example, "training"
195208
metric_names: list of metric names to plot or a string "all" to plot all available
@@ -203,6 +216,7 @@ def global_step_transform(*args, **kwargs):
203216
Default is None, global_step based on attached engine. If provided,
204217
uses function output as global_step. To setup global step from another engine, please use
205218
:meth:`~ignite.contrib.handlers.polyaxon_logger.global_step_from_engine`.
219+
state_attributes: list of attributes of the ``trainer.state`` to plot.
206220
207221
Note:
208222
@@ -213,6 +227,8 @@ def global_step_transform(*args, **kwargs):
213227
def global_step_transform(engine, event_name):
214228
return engine.state.get_event_attrib_value(event_name)
215229
230+
.. versionchanged:: 0.5.0
231+
accepts an optional list of `state_attributes`
216232
"""
217233

218234
def __init__(
@@ -221,8 +237,11 @@ def __init__(
221237
metric_names: Optional[List[str]] = None,
222238
output_transform: Optional[Callable] = None,
223239
global_step_transform: Optional[Callable] = None,
240+
state_attributes: Optional[List[str]] = None,
224241
):
225-
super(OutputHandler, self).__init__(tag, metric_names, output_transform, global_step_transform)
242+
super(OutputHandler, self).__init__(
243+
tag, metric_names, output_transform, global_step_transform, state_attributes
244+
)
226245

227246
def __call__(self, engine: Engine, logger: PolyaxonLogger, event_name: Union[str, Events]) -> None:
228247

tests/ignite/contrib/handlers/test_polyaxon_logger.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ def test_output_handler_with_global_step_from_engine():
197197
)
198198

199199

200+
def test_output_handler_state_attrs():
201+
wrapper = OutputHandler("tag", state_attributes=["alpha", "beta", "gamma"])
202+
mock_logger = MagicMock(spec=PolyaxonLogger)
203+
mock_logger.log_metrics = MagicMock()
204+
205+
mock_engine = MagicMock()
206+
mock_engine.state = State()
207+
mock_engine.state.iteration = 5
208+
mock_engine.state.alpha = 3.899
209+
mock_engine.state.beta = torch.tensor(12.21)
210+
mock_engine.state.gamma = torch.tensor([21.0, 6.0])
211+
212+
wrapper(mock_engine, mock_logger, Events.ITERATION_STARTED)
213+
214+
mock_logger.log_metrics.assert_called_once_with(
215+
**{"tag/alpha": 3.899, "tag/beta": torch.tensor(12.21).item(), "tag/gamma/0": 21.0, "tag/gamma/1": 6.0,},
216+
step=5,
217+
)
218+
219+
200220
def test_optimizer_params_handler_wrong_setup():
201221

202222
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)