Package ai.djl.metric

Class Metrics

java.lang.Object
ai.djl.metric.Metrics

public class Metrics extends Object
A collection of Metric objects organized by metric name.

Metric is a utility class that is used in the Trainer and Predictor to capture performance and other metrics during runtime.

It is built as a collection of individual Metric classes. As a container for individual metrics classes, Metrics stores them as time series data so that metric-vs-timeline analysis can be performed. It also provides convenient statistical methods for getting aggregated information, such as mean and percentile. The metrics is used to store key performance indicators (KPIs) during inference and training runs. These KPIs include various latencies, CPU and GPU memory consumption, losses, etc.

For more details about using the metrics, see the metrics tutorial.

  • Constructor Details

    • Metrics

      public Metrics()
      Constructs an empty Metrics instance.
  • Method Details

    • setLimit

      public void setLimit(int limit)
      Sets the max size for each metric.
      Parameters:
      limit - the max size for each metric
    • setOnLimit

      public void setOnLimit(BiConsumer<Metrics,String> onLimit)
      Sets the callback function when hit the limit.
      Parameters:
      onLimit - the callback function
    • addMetric

      public void addMetric(Metric metric)
      Adds a Metric to the collection.
      Parameters:
      metric - the Metric to be added
    • addMetric

      public void addMetric(String name, Number value)
      Adds a Metric given the metric's name and value.
      Parameters:
      name - the metric name
      value - the metric value
    • addMetric

      public void addMetric(String name, Number value, Unit unit)
      Adds a Metric given the metric's name, value, and unit.
      Parameters:
      name - the metric name
      value - the metric value
      unit - the metric unit
    • hasMetric

      public boolean hasMetric(String name)
      Returns true if the metrics object has a metric with the given name.
      Parameters:
      name - the name to check for
      Returns:
      true if the metrics object has a metric with the given name
    • getMetric

      public List<Metric> getMetric(String name)
      Returns all Metrics with the specified metric name.
      Parameters:
      name - the name of the metric
      Returns:
      a list of Metric with the specified metric name
    • getMetricNames

      public Set<String> getMetricNames()
      Returns a set of String metric names.
      Returns:
      a set of String metric names
    • latestMetric

      public Metric latestMetric(String name)
      Returns the latest Metric with the specified metric name.
      Parameters:
      name - the name of the metric
      Returns:
      the Metric with the specified metric name
      Throws:
      IllegalArgumentException - if the given name is not found
    • percentile

      public Metric percentile(String metricName, int percentile)
      Returns a percentile Metric object for the specified metric name.
      Parameters:
      metricName - the name of the metric
      percentile - the percentile
      Returns:
      the Metric object at specified percentile
    • mean

      public double mean(String metricName)
      Returns the average value of the specified metric.
      Parameters:
      metricName - the name of the metric
      Returns:
      the average value of the specified metric