CollaborativeCoding.metrics.accuracy

Classes

Accuracy

Computes the accuracy of a model's predictions.

Module Contents

class CollaborativeCoding.metrics.accuracy.Accuracy(num_classes, macro_averaging=False)

Bases: torch.nn.Module

Computes the accuracy of a model’s predictions.

Args

num_classesint

The number of classes in the classification task.

macro_averagingbool, optional

If True, computes macro-average accuracy. Otherwise, computes micro-average accuracy. Default is False.

Methods

forward(y_true, y_pred)

Stores the true and predicted labels. Typically called for each batch during the forward pass of a model.

_macro_acc()

Computes the macro-average accuracy.

_micro_acc()

Computes the micro-average accuracy.

__returnmetric__()

Returns the computed accuracy based on the averaging method for all stored predictions.

__reset__()

Resets the stored true and predicted labels.

Examples

>>> y_true = torch.tensor([0, 1, 2, 3, 3])
>>> y_pred = torch.tensor([0, 1, 2, 3, 0])
>>> accuracy = Accuracy(num_classes=4)
>>> accuracy(y_true, y_pred)
>>> accuracy.__returnmetric__()
0.8
>>> accuracy.__reset__()
>>> accuracy.macro_averaging = True
>>> accuracy(y_true, y_pred)
>>> accuracy.__returnmetric__()
0.875
num_classes
macro_averaging = False
y_true = []
y_pred = []
forward(y_true, y_pred)

Store the true and predicted labels.

Parameters

y_truetorch.Tensor

True labels.

y_predtorch.Tensor

Predicted labels. Either a 1D tensor of shape (batch_size,) or a 2D tensor of shape (batch_size, num_classes).

_macro_acc()

Compute the macro-average accuracy on the stored predictions.

Returns

float

Macro-average accuracy score.

_micro_acc()

Compute the micro-average accuracy on the stored predictions.

Returns

float

Micro-average accuracy score.

__returnmetric__()

Return the computed accuracy based on the averaging method for all stored predictions.

Returns

float

Computed accuracy score.

__reset__()

Reset the stored true and predicted labels.