CollaborativeCoding.metrics.accuracy ==================================== .. py:module:: CollaborativeCoding.metrics.accuracy Classes ------- .. autoapisummary:: CollaborativeCoding.metrics.accuracy.Accuracy Module Contents --------------- .. py:class:: Accuracy(num_classes, macro_averaging=False) Bases: :py:obj:`torch.nn.Module` Computes the accuracy of a model's predictions. Args ---------- num_classes : int The number of classes in the classification task. macro_averaging : bool, 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 .. py:attribute:: num_classes .. py:attribute:: macro_averaging :value: False .. py:attribute:: y_true :value: [] .. py:attribute:: y_pred :value: [] .. py:method:: forward(y_true, y_pred) Store the true and predicted labels. Parameters ---------- y_true : torch.Tensor True labels. y_pred : torch.Tensor Predicted labels. Either a 1D tensor of shape (batch_size,) or a 2D tensor of shape (batch_size, num_classes). .. py:method:: _macro_acc() Compute the macro-average accuracy on the stored predictions. Returns ------- float Macro-average accuracy score. .. py:method:: _micro_acc() Compute the micro-average accuracy on the stored predictions. Returns ------- float Micro-average accuracy score. .. py:method:: __returnmetric__() Return the computed accuracy based on the averaging method for all stored predictions. Returns ------- float Computed accuracy score. .. py:method:: __reset__() Reset the stored true and predicted labels.