class ignite.metrics.EpochMetric(compute_fn, output_transform=<function EpochMetric.<lambda>>, check_compute_fn=True, device=device(type='cpu'))[source]#

Class for metrics that should be computed on the entire output history of a model. Model’s output and targets are restricted to be of shape (batch_size, n_targets). Output datatype should be float32. Target datatype should be long for classification and float for regression.


Current implementation stores all input data (output and target) in as tensors before computing a metric. This can potentially lead to a memory error if the input data is larger than available RAM.

In distributed configuration, all stored data (output and target) is mutually collected across all processes using all gather collective operation. This can potentially lead to a memory error. Compute method executes compute_fn on zero rank process only and final result is broadcasted to all processes.

  • update must receive output of the form (y_pred, y) or {'y_pred': y_pred, 'y': y}.

  • compute_fn (Callable) – a callable with the signature (torch.tensor, torch.tensor) takes as the input predictions and targets and returns a scalar. Input tensors will be on specified device (see arg below).

  • output_transform (Callable) – a callable that is used to transform the Engine’s process_function’s output into the form expected by the metric. This can be useful if, for example, you have a multi-output model and you want to compute the metric with respect to one of the outputs.

  • check_compute_fn (bool) – if True, compute_fn is run on the first batch of data to ensure there are no issues. If issues exist, user is warned that there might be an issue with the compute_fn. Default, True.

  • device (Union[str, device]) – optional device specification for internal storage.


EpochMetricWarning: User is warned that there are issues with compute_fn on a batch of data processed. To disable the warning, set check_compute_fn=False.



Computes the metric based on it's accumulated state.


Resets the metric to it's initial state.


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


Computes the metric based on it’s accumulated state.

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


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



NotComputableError – raised when the metric cannot be computed.


Resets the metric to it’s initial state.

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

Return type



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

By default, this is called once for each batch.


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

Return type