API - Metrics

The tensorlayerx.metrics directory contians Accuracy, Auc, Precision and Recall. For more complex metrics, you can encapsulates metric logic and APIs by base class.

Metric list

Metric()

Base class for metric

Accuracy([topk])

Accuracy metric

Auc([curve, num_thresholds])

The auc metric is for binary classification.

Precision()

Precision score for binary classification task.

Recall()

Recall score for binary classification task.

acc(predicts, labels[, topk])

Accuracy function.

Metric

class tensorlayerx.metrics.Metric[source]

Base class for metric

__init__()[source]

Initializing the Metric.

update()[source]

Update states for metric.

result()[source]

Computes and returns the metric value.

reset()[source]

Reset states and result.

Accuracy

class tensorlayerx.metrics.Accuracy(topk=1)[source]

Accuracy metric

Parameters:

topk (int) – Specifies the top-k categorical accuracy to compute. Default is (1,).

Examples

>>> import tensorlayerx as tlx
>>> y_pred = tlx.ops.convert_to_tensor(np.array([[0.3, 0.2, 0.1, 0.4], [0.2, 0.2, 0.5, 0.1]]))
>>> y_true = tlx.ops.convert_to_tensor(np.array([[1], [3]]))
>>> metric = tlx.metrics.Accuracy()
>>> metric.update(y_pred, y_true)
>>> res = metric.result()
>>> metric.reset()
reset()[source]

Resets all of the metric state.

result()[source]

Computes the top-k categorical accuracy.

Returns:

Return type:

computed result.

update(y_pred, y_true)[source]

Updates the internal evaluation result y_pred and y_true.

Parameters:
  • y_pred (Tensor) – The predicted value.

  • y_true (Tensor) – The ground truth.

Auc

class tensorlayerx.metrics.Auc(curve='ROC', num_thresholds=4095)[source]

The auc metric is for binary classification.

Parameters:
  • curve (str) – Specifies the mode of the curve to be computed. Only support ‘ROC’ now.

  • num_thresholds (int) – The number of thresholds to use when discretizing the roc curve.

reset()[source]

Reset states and result

result()[source]

Return the area (a float score) under auc curve

Returns:

Return type:

computed result.

update(y_pred, y_true)[source]

Updates the auc curve with y_pred and y_true.

Parameters:
  • y_pred (Tensor) – The predicted value.

  • y_true (Tensor) – The ground truth.

Precision

class tensorlayerx.metrics.Precision[source]

Precision score for binary classification task.

Examples

>>> import tensorlayerx as tlx
>>> y_pred = tlx.ops.convert_to_tensor(np.array([0.3, 0.2, 0.1, 0.7]))
>>> y_true = tlx.ops.convert_to_tensor(np.array([1, 0, 0, 1]))
>>> metric = tlx.metrics.Precision()
>>> metric.update(y_pred, y_true)
>>> res = metric.result()
>>> metric.reset()
reset()[source]

Resets all of the metric state.

result()[source]

Return the precision

Returns:

Return type:

computed result.

update(y_pred, y_true)[source]

Update the states based on the current mini-batch prediction results.

Parameters:
  • y_pred (Tensor) – The predicted value.

  • y_true (Tensor) – The ground truth.

Recall

class tensorlayerx.metrics.Recall[source]

Recall score for binary classification task.

Examples

>>> import tensorlayerx as tlx
>>> y_pred = tlx.ops.convert_to_tensor(np.array([0.3, 0.2, 0.1, 0.7]))
>>> y_true = tlx.ops.convert_to_tensor(np.array([1, 0, 0, 1]))
>>> metric = tlx.metrics.Recall()
>>> metric.update(y_pred, y_true)
>>> res = metric.result()
>>> metric.reset()
reset()[source]

Resets all of the metric state.

result()[source]

Return the recall

Returns:

Return type:

computed result.

update(y_pred, y_true)[source]

Update the states based on the current mini-batch prediction results.

Parameters:
  • y_pred (Tensor) – The predicted value.

  • y_true (Tensor) – The ground truth.

acc

tensorlayerx.metrics.acc(predicts, labels, topk=1)[source]

Accuracy function.

Parameters:
  • predicts (Tensor) – The predicted value.

  • labels (Tensor) – The ground truth.

  • topk (int) – The top k predictions for each class will be checked.

Returns:

Return type:

The accuracy result.

Examples

>>> import tensorlayerx as tlx
>>> y_pred = tlx.ops.convert_to_tensor(np.array([[0.3, 0.2, 0.1, 0.4], [0.2, 0.2, 0.5, 0.1]]))
>>> y_true = tlx.ops.convert_to_tensor(np.array([[1], [3]]))
>>> acc = tlx.metrics.acc(y_pred, y_true, topk=1)