glue

class AccuracyAndF1(topk=(1), pos_label=1, name='acc_and_f1', *args, **kwargs)[源代码]

基类:paddle.metric.metrics.Metric

This class encapsulates Accuracy, Precision, Recall and F1 metric logic, and accumulate function returns accuracy, precision, recall and f1. The overview of all metrics could be seen at the document of paddle.metric for details.

参数
  • topk (int or tuple(int), optional) -- Number of top elements to look at for computing accuracy. Defaults to (1,).

  • pos_label (int, optional) -- The positive label for calculating precision and recall. Defaults to 1.

  • name (str, optional) -- String name of the metric instance. Defaults to 'acc_and_f1'.

示例

import paddle
from paddlenlp.metrics import AccuracyAndF1

x = paddle.to_tensor([[0.1, 0.9], [0.5, 0.5], [0.6, 0.4], [0.7, 0.3]])
y = paddle.to_tensor([[1], [0], [1], [1]])

m = AccuracyAndF1()
correct = m.compute(x, y)
m.update(correct)
res = m.accumulate()
print(res) # (0.5, 0.5, 0.3333333333333333, 0.4, 0.45)
compute(pred, label, *args)[源代码]

Accepts network's output and the labels, and calculates the top-k (maximum value in topk) indices for accuracy.

参数
  • pred (Tensor) -- Predicted tensor, and its dtype is float32 or float64, and has a shape of [batch_size, num_classes].

  • label (Tensor) -- The ground truth tensor, and its dtype is is int64, and has a shape of [batch_size, 1] or [batch_size, num_classes] in one hot representation.

返回

Correct mask, each element indicates whether the prediction equals to the label. Its' a tensor with a data type of float32 and has a shape of [batch_size, topk].

返回类型

Tensor

update(correct, *args)[源代码]

Updates the metrics states (accuracy, precision and recall), in order to calculate accumulated accuracy, precision and recall of all instances.

参数

correct (Tensor) -- Correct mask for calculating accuracy, and it's a tensor with shape [batch_size, topk] and has a dtype of float32.

accumulate()[源代码]

Calculates and returns the accumulated metric.

返回

The accumulated metric. A tuple of shape (acc, precision, recall, f1, average_of_acc_and_f1)

With the fields:

  • acc (numpy.float64):

    The accumulated accuracy.

  • precision (numpy.float64):

    The accumulated precision.

  • recall (numpy.float64):

    The accumulated recall.

  • f1 (numpy.float64):

    The accumulated f1.

  • average_of_acc_and_f1 (numpy.float64):

    The average of accumulated accuracy and f1.

返回类型

tuple

reset()[源代码]

Resets all metric states.

name()[源代码]

Returns name of the metric instance.

返回

The name of the metric instance.

返回类型

str

class Mcc(name='mcc', *args, **kwargs)[源代码]

基类:paddle.metric.metrics.Metric

This class calculates Matthews correlation coefficient .

参数

name (str, optional) -- String name of the metric instance. Defaults to 'mcc'.

示例

import paddle
from paddlenlp.metrics import Mcc

x = paddle.to_tensor([[-0.1, 0.12], [-0.23, 0.23], [-0.32, 0.21], [-0.13, 0.23]])
y = paddle.to_tensor([[1], [0], [1], [1]])

m = Mcc()
(preds, label) = m.compute(x, y)
m.update((preds, label))
res = m.accumulate()
print(res) # (0.0,)
compute(pred, label, *args)[源代码]

Processes the pred tensor, and returns the indices of the maximum of each sample.

参数
  • pred (Tensor) -- The predicted value is a Tensor with dtype float32 or float64. Shape is [batch_size, 1].

  • label (Tensor) -- The ground truth value is Tensor with dtype int64, and its shape is [batch_size, 1].

返回

A tuple of preds and label. Each shape is [batch_size, 1], with dtype float32 or float64.

返回类型

tuple

update(preds_and_labels)[源代码]

Calculates states, i.e. the number of true positive, false positive, true negative and false negative samples.

参数

preds_and_labels (tuple[Tensor]) -- Tuple of predicted value and the ground truth label, with dtype float32 or float64. Each shape is [batch_size, 1].

accumulate()[源代码]

Calculates and returns the accumulated metric.

返回

The accumulated metric. A tuple of shape (mcc,)

With the fields:

  • mcc (numpy.float64):

    The accumulated mcc.

返回类型

tuple

reset()[源代码]

Resets all metric states.

name()[源代码]

Returns name of the metric instance.

返回

The name of the metric instance.

返回类型

str

class PearsonAndSpearman(name='pearson_and_spearman', *args, **kwargs)[源代码]

基类:paddle.metric.metrics.Metric

The class calculates Pearson correlation coefficient and Spearman's rank correlation coefficient .

参数

name (str, optional) -- String name of the metric instance. Defaults to 'pearson_and_spearman'.

示例

import paddle
from paddlenlp.metrics import PearsonAndSpearman

x = paddle.to_tensor([[0.1], [1.0], [2.4], [0.9]])
y = paddle.to_tensor([[0.0], [1.0], [2.9], [1.0]])

m = PearsonAndSpearman()
m.update((x, y))
res = m.accumulate()
print(res) # (0.9985229081857804, 1.0, 0.9992614540928901)
update(preds_and_labels)[源代码]

Ensures the type of preds and labels is numpy.ndarray and reshapes them into [-1, 1].

参数

preds_and_labels (tuple[Tensor] or list[Tensor]) -- Tuple of predicted value and the ground truth label, with dtype float32 or float64. Each shape is [batch_size, d0, ..., dN].

accumulate()[源代码]

Calculates and returns the accumulated metric.

返回

The accumulated metric. A tuple of (pearson, spearman, the_average_of_pearson_and_spearman)

With the fields:

  • pearson (numpy.float64):

    The accumulated pearson.

  • spearman (numpy.float64):

    The accumulated spearman.

  • the_average_of_pearson_and_spearman (numpy.float64):

    The average of accumulated pearson and spearman correlation coefficient.

返回类型

tuple

reset()[源代码]

Resets all metric states.

name()[源代码]

Returns name of the metric instance.

返回

The name of the metric instance.

返回类型

str