- 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_fnon zero rank process only and final result is broadcasted to all processes.
updatemust receive output of the form
compute_fn (Callable[[torch.Tensor, torch.Tensor], float]) – a callable which receives two tensors as the 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
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_fnis 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, torch.device]) – optional device specification for internal storage.
- Return type
For more information on how metric works with
Engine, visit Attach Engine API.
from collections import OrderedDict import torch from torch import nn, optim from ignite.engine import * from ignite.handlers import * from ignite.metrics import * from ignite.utils import * from ignite.contrib.metrics.regression import * from ignite.contrib.metrics import * # create default evaluator for doctests def eval_step(engine, batch): return batch default_evaluator = Engine(eval_step) # create default optimizer for doctests param_tensor = torch.zeros(, requires_grad=True) default_optimizer = torch.optim.SGD([param_tensor], lr=0.1) # create default trainer for doctests # as handlers could be attached to the trainer, # each test must define his own trainer using `.. testsetup:` def get_default_trainer(): def train_step(engine, batch): return batch return Engine(train_step) # create default model for doctests default_model = nn.Sequential(OrderedDict([ ('base', nn.Linear(4, 2)), ('fc', nn.Linear(2, 1)) ])) manual_seed(666)
def mse_fn(y_preds, y_targets): return torch.mean(((y_preds - y_targets.type_as(y_preds)) ** 2)).item() metric = EpochMetric(mse_fn) metric.attach(default_evaluator, "mse") y_true = torch.tensor([0, 1, 2, 3, 4, 5]) y_pred = y_true * 0.75 state = default_evaluator.run([[y_pred, y_true]]) print(state.metrics["mse"])
EpochMetricWarning: User is warned that there are issues with
compute_fnon a batch of data processed. To disable the warning, set
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
Mappingis returned, it will be (shallow) flattened into engine.state.metrics when
- 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[torch.Tensor, torch.Tensor]) – the is the output from the engine’s process function.
- Return type