Metric

This module includes a set of metric classes for evaluating the model’s performance. The specific metric classes could be converted from C++ implmentation or implemented directly using Python.

Example usage:

from singa import tensor
from singa import metric

x = tensor.Tensor((3, 5))
x.uniform(0, 1)  # randomly genearte the prediction activation
x = tensor.SoftMax(x)  # normalize the prediction into probabilities
y = tensor.from_numpy(np.array([0, 1, 3], dtype=np.int))  # set the truth

f = metric.Accuracy()
acc = f.evaluate(x, y)  # averaged accuracy over all 3 samples in x
class singa.metric.Metric

Bases: object

Base metric class.

Subclasses that wrap the C++ loss classes can use the inherited foward, and evaluate functions of this base class. Other subclasses need to override these functions. Users need to feed in the predictions and ground truth to get the metric values.

forward(x, y)

Compute the metric for each sample.

Parameters
  • x (Tensor) – predictions, one row per sample

  • y (Tensor) – ground truth values, one row per sample

Returns

a tensor of floats, one per sample

evaluate(x, y)

Compute the averaged metric over all samples.

Parameters
  • x (Tensor) – predictions, one row per sample

  • y (Tensor) – ground truth values, one row per sample

Returns

a float value for the averaged metric

class singa.metric.Accuracy

Bases: singa.metric.Metric

Compute the top one accuracy for single label prediction tasks.

It calls the C++ functions to do the calculation.

class singa.metric.Precision(top_k)

Bases: singa.metric.Metric

Make the top-k labels of max probability as the prediction

Compute the precision against the groundtruth labels

forward(x, y)

Compute the precision for each sample.

Convert tensor to numpy for computation

Parameters
  • x (Tensor) – predictions, one row per sample

  • y (Tensor) – ground truth labels, one row per sample

Returns

a tensor of floats, one per sample

evaluate(x, y)

Compute the averaged precision over all samples.

Parameters
  • x (Tensor) – predictions, one row per sample

  • y (Tensor) – ground truth values, one row per sample

Returns

a float value for the averaged metric

class singa.metric.Recall(top_k)

Bases: singa.metric.Metric

Make the top-k labels of max probability as the prediction

Compute the recall against the groundtruth labels

forward(x, y)

Compute the recall for each sample.

Convert tensor to numpy for computation

Parameters
  • x (Tensor) – predictions, one row per sample

  • y (Tensor) – ground truth labels, one row per sample

Returns

a tensor of floats, one per sample

evaluate(x, y)

Compute the averaged precision over all samples.

Parameters
  • x (Tensor) – predictions, one row per sample

  • y (Tensor) – ground truth values, one row per sample

Returns

a float value for the averaged metric