Shortcuts

MetricGroup

class ignite.metrics.metric_group.MetricGroup(metrics, output_transform=<function MetricGroup.<lambda>>, skip_unrolling=False)[source]

A class for grouping metrics so that user could manage them easier.

Parameters
  • metrics (Dict[str, Metric]) – a dictionary of names to metric instances.

  • output_transform (Callable) – a callable that is used to transform the Engine’s process_function’s output into the form expected by the metric. output_transform of each metric in the group is also called upon its update.

  • skip_unrolling (bool) – specifies whether output should be unrolled before being fed to update method. Should be true for multi-output model, for example, if y_pred and y contain multi-ouput as (y_pred_a, y_pred_b) and (y_a, y_b), in which case the update method is called for (y_pred_a, y_a) and (y_pred_b, y_b).Alternatively, output_transform can be used to handle this.

Examples

We construct a group of metrics, attach them to the engine at once and retrieve their result.

import torch

metric_group = MetricGroup({'acc': Accuracy(), 'precision': Precision(), 'loss': Loss(nn.NLLLoss())})
metric_group.attach(default_evaluator, "eval_metrics")
y_true = torch.tensor([1, 0, 1, 1, 0, 1])
y_pred = torch.tensor([1, 0, 1, 0, 1, 1])
state = default_evaluator.run([[y_pred, y_true]])

# Metrics individually available in `state.metrics`
state.metrics["acc"], state.metrics["precision"], state.metrics["loss"]

# And also altogether
state.metrics["eval_metrics"]

Changed in version 0.5.2: skip_unrolling argument is added.

Methods

compute

Computes the metric based on its accumulated state.

reset

Resets the metric to its initial state.

update

Updates the metric's state using the passed batch output.

compute()[source]

Computes the metric based on its accumulated state.

By default, this is called at the end of each epoch.

Returns

the actual quantity of interest. However, if a Mapping is returned, it will be (shallow) flattened into engine.state.metrics when completed() is called.

Return type

Any

Raises

NotComputableError – raised when the metric cannot be computed.

reset()[source]

Resets the metric to its initial state.

By default, this is called at the start of each epoch.

Return type

None

update(output)[source]

Updates the metric’s state using the passed batch output.

By default, this is called once for each batch.

Parameters

output (Sequence[Tensor]) – the is the output from the engine’s process function.

Return type

None