accuracy

paddle.fluid.layers.accuracy(input, label, k=1, correct=None, total=None)[source]

accuracy layer. Refer to the https://en.wikipedia.org/wiki/Precision_and_recall

This function computes the accuracy using the input and label. If the correct label occurs in top k predictions, then correct will increment by one. Note: the dtype of accuracy is determined by input. the input and label dtype can be different.

Parameters
  • input (Variable) – The input of accuracy layer, which is the predictions of network. A LoDTensor or Tensor with type float32,float64.

  • label (Variable) – The label of dataset. LoDTensor or Tensor with type int32,int64.

  • k (int) – The top k predictions for each class will be checked. Data type is int64 or int32.

  • correct (Variable) – The correct predictions count. A Tensor with type int64 or int32.

  • total (Variable) – The total entries count. A tensor with type int64 or int32.

Returns

The correct rate. A Tensor with type float32.

Return type

Variable

Examples

import paddle.fluid as fluid
import numpy as np

data = fluid.data(name="input", shape=[-1, 32, 32], dtype="float32")
label = fluid.data(name="label", shape=[-1,1], dtype="int")
fc_out = fluid.layers.fc(input=data, size=10)
predict = fluid.layers.softmax(input=fc_out)
result = fluid.layers.accuracy(input=predict, label=label, k=5)

place = fluid.CPUPlace()
exe = fluid.Executor(place)

exe.run(fluid.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.6666667], dtype=float32)]