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。

代码示例

import numpy as np

import paddle
import paddle.static as static
import paddle.nn.functional as F

paddle.enable_static()
data = static.data(name="input", shape=[-1, 32, 32], dtype="float32")
label = static.data(name="label", shape=[-1,1], dtype="int")
fc_out = static.nn.fc(x=data, size=10)
predict = F.softmax(x=fc_out)
result = static.accuracy(input=predict, label=label, k=5)

place = paddle.CPUPlace()
exe = static.Executor(place)

exe.run(static.default_startup_program())
x = np.random.rand(3, 32, 32).astype("float32")
y = np.array([[1],[0],[1]])
output= exe.run(feed={"input": x,"label": y},
            fetch_list=[result[0]])
print(output)

#[array([0.], dtype=float32)]