Accuracy

class paddle.fluid.metrics.Accuracy(name=None)[源代码]

该接口用来计算多个mini-batch的平均准确率。Accuracy对象有两个状态value和weight。Accuracy的定义参照 https://en.wikipedia.org/wiki/Accuracy_and_precision

参数

  • name (str,可选) – 具体用法请参见 Name ,一般无需设置,默认值为None。

返回

初始化后的 Accuracy 对象

返回类型

Accuracy

代码示例

  1. import paddle.fluid as fluid
  2. # 假设有batch_size = 128
  3. batch_size=128
  4. accuracy_manager = fluid.metrics.Accuracy()
  5. # 假设第一个batch的准确率为0.9
  6. batch1_acc = 0.9
  7. accuracy_manager.update(value = batch1_acc, weight = batch_size)
  8. print("expect accuracy: %.2f, get accuracy: %.2f" % (batch1_acc, accuracy_manager.eval()))
  9. # 假设第二个batch的准确率为0.8
  10. batch2_acc = 0.8
  11. accuracy_manager.update(value = batch2_acc, weight = batch_size)
  12. #batch1和batch2的联合准确率为(batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2
  13. print("expect accuracy: %.2f, get accuracy: %.2f" % ((batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2, accuracy_manager.eval()))
  14. #重置accuracy_manager
  15. accuracy_manager.reset()
  16. #假设第三个batch的准确率为0.8
  17. batch3_acc = 0.8
  18. accuracy_manager.update(value = batch3_acc, weight = batch_size)
  19. print("expect accuracy: %.2f, get accuracy: %.2f" % (batch3_acc, accuracy_manager.eval()))

方法

update(value, weight)

该函数使用输入的(value, weight)来累计更新Accuracy对象的对应状态,更新方式如下:

Accuracy - 图1

参数

  • value (float|numpy.array) – mini-batch的正确率
  • weight (int|float) – mini-batch的大小

返回

eval()

该函数计算并返回累计的mini-batches的平均准确率。

返回 累计的mini-batches的平均准确率

返回类型 float或numpy.array