avalanche.evaluation.metrics.ConfusionMatrix
- class avalanche.evaluation.metrics.ConfusionMatrix(num_classes: int | None = None, normalize: Literal['true', 'pred', 'all'] | None = None)[source]
The standalone confusion matrix metric.
Instances of this metric keep track of the confusion matrix by receiving a pair of “ground truth” and “prediction” Tensors describing the labels of a minibatch. Those two tensors can both contain plain labels or one-hot/logit vectors.
The result is the unnormalized running confusion matrix.
Beware that by default the confusion matrix size will depend on the value of the maximum label as detected by looking at both the ground truth and predictions Tensors. When passing one-hot/logit vectors, this metric will try to infer the number of classes from the vector sizes. Otherwise, the maximum label value encountered in the truth/prediction Tensors will be used.
If the user sets the num_classes, then the confusion matrix will always be of size num_classes, num_classes. Whenever a prediction or label tensor is provided as logits, only the first num_classes units will be considered in the confusion matrix computation. If they are provided as numerical labels, each of them has to be smaller than num_classes.
The reset method will bring the metric to its initial state. By default this metric in its initial state will return an empty Tensor.
- __init__(num_classes: int | None = None, normalize: Literal['true', 'pred', 'all'] | None = None)[source]
Creates an instance of the standalone confusion matrix metric.
By default this metric in its initial state will return an empty Tensor. The metric can be updated by using the update method while the running confusion matrix can be retrieved using the result method.
- Parameters:
num_classes – The number of classes. Defaults to None, which means that the number of classes will be inferred from ground truth and prediction Tensors (see class description for more details). If not None, the confusion matrix will always be of size num_classes, num_classes and only the first num_classes values of output logits or target logits will be considered in the update. If the output or targets are provided as numerical labels, there can be no label greater than num_classes.
normalize – how to normalize confusion matrix. None to not normalize
Methods
__init__
([num_classes, normalize])Creates an instance of the standalone confusion matrix metric.
nan_to_num
(matrix)reset
()Resets the metric.
result
()Retrieves the unnormalized confusion matrix.
update
(true_y, predicted_y)Update the running confusion matrix given the true and predicted labels.