MetricGroup#
- class ignite.metrics.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, ignite.metrics.metric.Metric]) – a dictionary of names to metric instances.
output_transform (Callable) – a callable that is used to transform the
Engine’sprocess_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_predandycontain multi-output 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_transformcan 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_unrollingargument is added.Methods
Computes the metric based on its accumulated state.
Helper method to update metric's computation.
Resets the metric to its initial state.
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
Mappingis returned, it will be (shallow) flattened into engine.state.metrics whencompleted()is called. - Return type:
Any
- Raises:
NotComputableError – raised when the metric cannot be computed.
- iteration_completed(engine)[source]#
Helper method to update metric’s computation. It is automatically attached to the engine with
attach().- Parameters:
engine (Engine) – the engine to which the metric must be attached
- Return type:
None
Note
engine.state.outputis used to compute metric values. The majority of implemented metrics accept the following formats forengine.state.output:(y_pred, y)or{'y_pred': y_pred, 'y': y}.y_predandycan be torch tensors or list of tensors/numbers if applicable.Changed in version 0.4.5:
y_predandycan be torch tensors or list of tensors/numbers