# ConfusionMatrix#

class ignite.metrics.confusion_matrix.ConfusionMatrix(num_classes, average=None, output_transform=<function ConfusionMatrix.<lambda>>, device=device(type='cpu'))[source]#

Calculates confusion matrix for multi-class data.

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

• y_pred must contain logits and has the following shape (batch_size, num_classes, …). If you are doing binary classification, see Note for an example on how to get this.

• y should have the following shape (batch_size, …) and contains ground-truth class indices with or without the background class. During the computation, argmax of y_pred is taken to determine predicted classes.

Parameters
• num_classes (int) – Number of classes, should be > 1. See notes for more details.

• average (Optional[str]) – confusion matrix values averaging schema: None, “samples”, “recall”, “precision”. Default is None. If average=”samples” then confusion matrix values are normalized by the number of seen samples. If average=”recall” then confusion matrix values are normalized such that diagonal values represent class recalls. If average=”precision” then confusion matrix values are normalized such that diagonal values represent class precisions.

• 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.

• device (Union[str, torch.device]) – specifies which device updates are accumulated on. Setting the metric’s device to be the same as your update arguments ensures the update method is non-blocking. By default, CPU.

Note

The confusion matrix is formatted such that columns are predictions and rows are targets. For example, if you were to plot the matrix, you could correctly assign to the horizontal axis the label “predicted values” and to the vertical axis the label “actual values”.

Note

In case of the targets y in (batch_size, …) format, target indices between 0 and num_classes only contribute to the confusion matrix and others are neglected. For example, if num_classes=20 and target index equal 255 is encountered, then it is filtered out.

Examples

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

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)

metric = ConfusionMatrix(num_classes=3)
metric.attach(default_evaluator, 'cm')
y_true = torch.tensor([0, 1, 0, 1, 2])
y_pred = torch.tensor([
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
])
state = default_evaluator.run([[y_pred, y_true]])
print(state.metrics['cm'])

tensor([[1, 1, 0],
[0, 2, 0],
[0, 1, 0]])


If you are doing binary classification with a single output unit, you may have to transform your network output, so that you have one value for each class. E.g. you can transform your network output into a one-hot vector with:

def binary_one_hot_output_transform(output):
y_pred, y = output
y_pred = torch.sigmoid(y_pred).round().long()
y_pred = ignite.utils.to_onehot(y_pred, 2)
y = y.long()
return y_pred, y

metric = ConfusionMatrix(num_classes=2, output_transform=binary_one_hot_output_transform)
metric.attach(default_evaluator, 'cm')
y_true = torch.tensor([0, 1, 0, 1, 0])
y_pred = torch.tensor([0, 0, 1, 1, 0])
state = default_evaluator.run([[y_pred, y_true]])
print(state.metrics['cm'])

tensor([[2, 1],
[1, 1]])


Methods

 compute Computes the metric based on it's accumulated state. normalize Normalize given matrix with given average. reset Resets the metric to it's initial state. update Updates the metric's state using the passed batch output.
compute()[source]#

Computes the metric based on it’s 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.

static normalize(matrix, average)[source]#

Normalize given matrix with given average.

Parameters
Return type

torch.Tensor

reset()[source]#

Resets the metric to it’s 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[torch.Tensor]) – the is the output from the engine’s process function.

Return type

None