paddle03-paddle.metric


🌟 paddle.metric

评估器相关API

  • 1.paddle.Metric() 评估器基类

  • 2.paddle.metric.Accuracy() 准确率评估器类

    参数: 
        topk: int/tuple[int], 计算准确率的 top个数,默认1
    
    • 独立使用实例:
      import numpy as np
      import paddle
      
      x = paddle.to_tensor(np.array([
          [0.1, 0.2, 0.3, 0.4],
          [0.1, 0.4, 0.3, 0.2],
          [0.1, 0.2, 0.4, 0.3],
          [0.1, 0.2, 0.3, 0.4]]))
      y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))
      
      m = paddle.metric.Accuracy()
      correct = m.compute(x, y)       # [[0],[1],[1],[1]]
      m.update(correct)
      res = m.accumulate()
      print(res) # 0.75
      
    • 在Model API中的示例:
      from paddle.static import InputSpec
      import paddle.vision.transforms as T
      from paddle.vision.datasets import MNIST
      
      input = InputSpec([None, 1, 28, 28], 'float32', 'image')
      label = InputSpec([None, 1], 'int64', 'label')
      transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
      train_dataset = MNIST(mode='train', transform=transform)
      
      model = paddle.Model(paddle.vision.LeNet(), input, label)
      optim = paddle.optimizer.Adam(
          learning_rate=0.001, parameters=model.parameters())
      model.prepare(
          optim,
          loss=paddle.nn.CrossEntropyLoss(),
          metrics=paddle.metric.Accuracy())           # *********
      
      model.fit(train_dataset, batch_size=64)         # *********
      
    • 方法: compute(pred, label, *args) 计算top-k(topk中的最大值)的索引
      参数: 
          1.pred (Tensor) - 预测结果为是float64或float32类型的Tensor。shape为[batch_size, d0, ..., dN]
          2.label (Tensor) - 真实的标签值是一个int64类型的Tensor,shape为[batch_size, d0, ..., 1] 或one hot表示的形状[batch_size, d0, ..., num_classes].  
      返回:一个Tensor,shape是[batch_size, d0, ..., topk], 值为0或1,1表示预测正确  
      
    • 方法:update(pred, label, *args) 更新metric的状态(正确预测的个数和总个数),以便计算累积的准确率。返回:当前step的准确率。
      参数: compute() 结果, correct:tensor, np,一个值为0或1的Tensor,shape是[batch_size, d0, ..., topk]  
      返回: 当前 step 的准确率。 
      
    • 方法: reset() 清空状态和计算结果,返回: 无
    • 方法: accumulate() 累积的统计指标,计算和返回准确率。
      返回: 准确率,一般是个标量 或 多个标量,和topk的个数一致
      
    • 方法: name() 返回:评估的名字,string类型
  • 3.paddle.metric.Auc() auc 评估器类

    * 计算Auc,在二分类(binary classification)中广泛使用
    * 该接口创建四个局部变量true_positives, true_negatives, false_positives和false_negatives,用于计算Auc。为了离散化AUC曲线,使用临界值的线性间隔来计算召回率和准确率的值。用false positive的召回值高度计算ROC曲线面积,用recall的准确值高度计算PR曲线面积。  
    
    参数: 
        1.curve (str) - 将要计算的曲线名的模式,包括'ROC'(默认)或者'PR'(Precision-Recall-curve)
        2.num_thresholds (int) - 离散化AUC曲线的整数阈值数,默认是4095。
    
    • 独立使用示例:
      import numpy as np
      import paddle
      
      m = paddle.metric.Auc()
      
      n = 8
      class0_preds = np.random.random(size = (n, 1))
      class1_preds = 1 - class0_preds
      preds = np.concatenate((class0_preds, class1_preds), axis=1)
      labels = np.random.randint(2, size = (n, 1))
      
      m.update(preds=preds, labels=labels)
      res = m.accumulate()
      
    • 在Model API中的示例
      model.prepare(optim, loss=loss, metrics=paddle.metric.Auc())
      model.fit(data, batch_size=16)   
      
    • 方法: update(pred, label, *args) 更新AUC计算的状态
    • 方法: reset() 清空状态和计算结果
    • 方法: accumulate() 累积的统计指标,计算和返回AUC值。 返回:AUC值,一个标量。
  • 4.paddle.metric.Precision() 精确率评估器类 仅针对二分类 预测正确数量/预测数量

  • 5.paddle.metric.Recall() 召回率评估器类 仅针对二分类 预测正确数量/正确数量
    #机器学习 Micro-F1和Macro-F1详解

评估器函数相关API

  • paddle.metric.accuracy(input, label, k=1, correct=None, total=None) 准确率评估函数
    使用输入和标签计算准确率。 如果正确的标签在topk个预测值里,则计算结果加1。注意:输出正确率的类型由input类型决定,input和lable的类型可以不一样
    
    参数: 
        1.input (Tensor)-数据类型为float32,float64。输入为网络的预测值。shape为 [sample_number, class_dim]
        2.label (Tensor)-数据类型为int64,int32。输入为数据集的标签。shape为 [sample_number, 1]
        3.k (int64|int32,可选) - 取每个类别中k个预测值用于计算,默认值为1
        4.correct (int64|int32, 可选)-正确预测值的个数,默认值为None
        5.total (int64|int32,可选)-总共的预测值,默认值为None。
    
    predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]],        dtype='float32')
    label = paddle.to_tensor([[2], [0]], dtype="int64")
    result = paddle.metric.accuracy(input=predictions, label=label, k=1)
    # [0.5]
    


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM