accuracy

paddle.static. 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. :param input: The input of accuracy layer, which is the predictions of network. A Tensor with type float32,float64.

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/metric_op.py:docstring of paddle.fluid.layers.metric_op.accuracy, line 7)

Unexpected indentation.

The shape is [sample_number, class_dim] .

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/metric_op.py:docstring of paddle.fluid.layers.metric_op.accuracy, line 8)

Block quote ends without a blank line; unexpected unindent.

Parameters
  • label (Tensor) – The label of dataset. Tensor with type int32,int64. The shape is [sample_number, 1] .

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

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

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

Returns

The correct rate. A Tensor with type float32.

Return type

Tensor

Examples

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/metric_op.py:docstring of paddle.fluid.layers.metric_op.accuracy, line 23)

Error in “code-block” directive: maximum 1 argument(s) allowed, 63 supplied.

.. code-block:: python
    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)]