Class AbstractAccuracy

java.lang.Object
ai.djl.training.evaluator.Evaluator
ai.djl.training.evaluator.AbstractAccuracy
Direct Known Subclasses:
Accuracy, BinaryAccuracy, Coverage, SingleShotDetectionAccuracy, TopKAccuracy

public abstract class AbstractAccuracy extends Evaluator
Accuracy is an Evaluator that computes the accuracy score.

The accuracy score is defined as \(accuracy(y, \hat{y}) = \frac{1}{n}\sum_{i=0}^{n-1}1(\hat{y_i} == y_i)\)

  • Field Details

    • correctInstances

      protected Map<String,Long> correctInstances
    • axis

      protected int axis
  • Constructor Details

    • AbstractAccuracy

      public AbstractAccuracy(String name)
      Creates an accuracy evaluator that computes accuracy across axis 1.
      Parameters:
      name - the name of the evaluator, default is "Accuracy"
    • AbstractAccuracy

      public AbstractAccuracy(String name, int axis)
      Creates an accuracy evaluator.
      Parameters:
      name - the name of the evaluator, default is "Accuracy"
      axis - the axis that represent classes in prediction, default 1
  • Method Details

    • accuracyHelper

      protected abstract ai.djl.util.Pair<Long,NDArray> accuracyHelper(NDList labels, NDList predictions)
      A helper for classes extending AbstractAccuracy.
      Parameters:
      labels - the labels to get accuracy for
      predictions - the predictions to get accuracy for
      Returns:
      a pair(number of total values, ndarray int of correct values)
    • evaluate

      public NDArray evaluate(NDList labels, NDList predictions)
      Calculates the evaluation between the labels and the predictions.
      Specified by:
      evaluate in class Evaluator
      Parameters:
      labels - the correct values
      predictions - the predicted values
      Returns:
      the evaluation result
    • addAccumulator

      public void addAccumulator(String key)
      Adds an accumulator for the results of the evaluation with the given key.
      Specified by:
      addAccumulator in class Evaluator
      Parameters:
      key - the key for the new accumulator
    • updateAccumulator

      public void updateAccumulator(String key, NDList labels, NDList predictions)
      Updates the evaluator with the given key based on a NDList of labels and predictions.

      This is a synchronized operation. You should only call it at the end of a batch or epoch.

      Specified by:
      updateAccumulator in class Evaluator
      Parameters:
      key - the key of the accumulator to update
      labels - a NDList of labels
      predictions - a NDList of predictions
    • updateAccumulators

      public void updateAccumulators(String[] keys, NDList labels, NDList predictions)
      Updates the evaluator with the given keys based on a NDList of labels and predictions.

      This is a synchronized operation. You should only call it at the end of a batch or epoch.

      This is an alternative to @{link Evaluator.updateAccumulator(String, NDList, NDList)} that may be more efficient when updating multiple accumulators at once.

      Overrides:
      updateAccumulators in class Evaluator
      Parameters:
      keys - the keys of all the accumulators to update
      labels - a NDList of labels
      predictions - a NDList of predictions
    • resetAccumulator

      public void resetAccumulator(String key)
      Resets the evaluator value with the given key.
      Specified by:
      resetAccumulator in class Evaluator
      Parameters:
      key - the key of the accumulator to reset
    • getAccumulator

      public float getAccumulator(String key)
      Returns the accumulated evaluator value.
      Specified by:
      getAccumulator in class Evaluator
      Parameters:
      key - the key of the accumulator to get
      Returns:
      the accumulated value