飞桨中accuracy的用法

飞桨中accuracy的用法


本文只作为笔记用。

环境:paddle 1.8.5

一直以来不太清楚这个如何调用,只知道例程里是这样用,不知所以然。
下面记录一下它的用法。
源码如下:

def accuracy(input, label, k=1, correct=None, total=None):
    """
    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.

    Args:
        input(Variable): The input of accuracy layer, which is the predictions of network. A LoDTensor or Tensor with type float32,float64.
            The shape is ``[sample_number, class_dim]`` .
        label(Variable): The label of dataset.  LoDTensor or 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(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:
        Variable: The correct rate. A Tensor with type float32.

    Examples:
        .. code-block:: python

            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)]
    """
    if in_dygraph_mode():
        if correct is None:
            correct = _varbase_creator(dtype="int32")
        if total is None:
            total = _varbase_creator(dtype="int32")

        topk_out, topk_indices = nn.topk(input, k=k)
        _acc, _, _ = core.ops.accuracy(topk_out, topk_indices, label, correct,
                                       total)
        return _acc

    helper = LayerHelper("accuracy", **locals())
    check_variable_and_dtype(input, 'input', ['float16', 'float32', 'float64'],
                             'accuracy')
    topk_out, topk_indices = nn.topk(input, k=k)
    acc_out = helper.create_variable_for_type_inference(dtype="float32")
    if correct is None:
        correct = helper.create_variable_for_type_inference(dtype="int32")
    if total is None:
        total = helper.create_variable_for_type_inference(dtype="int32")
    helper.append_op(
        type="accuracy",
        inputs={
            "Out": [topk_out],
            "Indices": [topk_indices],
            "Label": [label]
        },
        outputs={
            "Accuracy": [acc_out],
            "Correct": [correct],
            "Total": [total],
        })
    return acc_out

一开始我以为,它会像sklearn那样,两个相同的array丢进去就OK了。
但是以下做法是错误的。

batch_data = next(test_loader())
y1 = np.array([1, 2, 3, 4]).reshape(-1, 1).astype(np.int64)
y2 = np.array([1, 2, 3, 4]).reshape(-1, 1).astype(np.int64)
y1 = paddle.fluid.dygraph.to_variable(y1)
y2 = paddle.fluid.dygraph.to_variable(y2)
acc = paddle.fluid.layers.accuracy(y1, y2)
print(acc.numpy())

错误信息:

EnforceNotMet                             Traceback (most recent call last)
<ipython-input-59-971e31ff0c41> in <module>
      8 # pred = paddle.fluid.layers.softmax(y1)
      9 # y2 = paddle.fluid.layers.cast(y2, np.int64)
---> 10 acc = paddle.fluid.layers.accuracy(y1, y2)
     11 print(acc.numpy())

d:\programdata\anaconda3\envs\parl\lib\site-packages\paddle\fluid\layers\metric_op.py in accuracy(input, label, k, correct, total)
     82         topk_out, topk_indices = nn.topk(input, k=k)
     83         _acc, _, _ = core.ops.accuracy(topk_out, topk_indices, label, correct,
---> 84                                        total)
     85         return _acc
     86 

EnforceNotMet: 

--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
Windows not support stack backtrace yet.

----------------------
Error Message Summary:
----------------------
NotFoundError: Operator accuracy does not have kernel for data_type[int64_t]:data_layout[ANY_LAYOUT]:place[CUDAPlace(0)]:library_type[PLAIN].
  [Hint: Expected kernel_iter != kernels.end(), but received kernel_iter == kernels.end().] at (D:\1.8.5\paddle\paddle\fluid\imperative\prepared_operator.cc:118)
  [operator < accuracy > error]

在paddle里,它的accuracy的第一个参数输入的是对应你多种类别分别的概率,第二个参数是类别。
以下是正确的结果的code。

batch_data = next(test_loader())
y1 = np.array([[1.22, 12.43, 0.1, 0.91],[1.22, 12.43, 0.1, 0.91],[1.22, 12.43, 0.1, 0.91],[1.22, 12.43, 0.1, 0.91]]).astype(np.float32)
y2 = np.array([1, 2, 3, 4]).reshape(-1, 1).astype(np.int64)
print(y1.numpy(), y1.numpy().shape, y2.numpy(), y2.numpy().shape)
y1 = paddle.fluid.dygraph.to_variable(y1)
y2 = paddle.fluid.dygraph.to_variable(y2)
acc = paddle.fluid.layers.accuracy(y1, y2)
print(acc.numpy())
# 结果:[0.25]
# y1 
# [[ 1.22 12.43  0.1   0.91]
#  [ 1.22 12.43  0.1   0.91]
#  [ 1.22 12.43  0.1   0.91]
#  [ 1.22 12.43  0.1   0.91]]
# y2
# [[1]
#  [2]
#  [3]
#  [4]]

或者

batch_data = next(test_loader())
y1 = np.array([[1.22, 12.43, 0.1, 0.91],[1.22, 12.43, 0.1, 0.91],[1.22, 12.43, 0.1, 0.91],[1.22, 12.43, 0.1, 0.91]]).astype(np.float32)
y2 = np.array([1, 2, 3, 4]).reshape(-1, 1).astype(np.int64)
y1 = paddle.fluid.dygraph.to_variable(y1)
y2 = paddle.fluid.dygraph.to_variable(y2)
y1 = paddle.fluid.layers.softmax(y1)
print(y1.numpy(), y1.numpy().shape, y2.numpy(), y2.numpy().shape)
acc = paddle.fluid.layers.accuracy(y1, y2)
print(acc.numpy())
# 结果:[0.25]
# y1 
# [[1.3537758e-05 9.9997211e-01 4.4170970e-06 9.9292220e-06]
#  [1.3537758e-05 9.9997211e-01 4.4170970e-06 9.9292220e-06]
#  [1.3537758e-05 9.9997211e-01 4.4170970e-06 9.9292220e-06]
#  [1.3537758e-05 9.9997211e-01 4.4170970e-06 9.9292220e-06]] 
# y2
# [[1]
#  [2]
#  [3]
#  [4]]

对自己的记录,希望对大家有帮助。

你可能感兴趣的:(深度学习,神经网络,Python,深度学习,paddlepaddle)