accuracy

paddle.static.accuracy ( input, label, k=1, correct=None, total=None ) [源代码]

accuracy layer。 参考 https://en.wikipedia.org/wiki/Precision_and_recall

使用输入和标签计算准确率。 如果正确的标签在topk个预测值里,则计算结果加1。注意:输出正确率的类型由input类型决定,input和lable的类型可以不一样。

参数:

  • input (Tensor|LoDTensor)-数据类型为float32,float64。输入为网络的预测值。shape为 [sample_number, class_dim]

  • label (Tensor|LoDTensor)-数据类型为int64,int32。输入为数据集的标签。shape为 [sample_number, 1]

  • k (int64|int32) - 取每个类别中k个预测值用于计算。

  • correct (int64|int32)-正确预测值的个数。

  • total (int64|int32)-总共的预测值。

返回: Tensor,计算出来的正确率,数据类型为float32。

代码示例

  1. import numpy as np
  2. import paddle
  3. import paddle.static as static
  4. import paddle.nn.functional as F
  5. paddle.enable_static()
  6. data = static.data(name="input", shape=[-1, 32, 32], dtype="float32")
  7. label = static.data(name="label", shape=[-1,1], dtype="int")
  8. fc_out = static.nn.fc(x=data, size=10)
  9. predict = F.softmax(x=fc_out)
  10. result = static.accuracy(input=predict, label=label, k=5)
  11. place = paddle.CPUPlace()
  12. exe = static.Executor(place)
  13. exe.run(static.default_startup_program())
  14. x = np.random.rand(3, 32, 32).astype("float32")
  15. y = np.array([[1],[0],[1]])
  16. output= exe.run(feed={"input": x,"label": y},
  17. fetch_list=[result[0]])
  18. print(output)
  19. #[array([0.], dtype=float32)]