秒懂-深度学习框架的中计算准确率accuracy()原理(基于paddlepaddle)

前言

最近在实操号称人工智能中的’hello world‘–手写数字集识别。这里是使用的是paddlepaddle深度学习框架。
其中我们在训练的时候,需要计算准确度。框架提供计算准确度的方法:fluid.layers.accuracy().它究竟是如何进行精确度的计算的?今天,我带大家一探究竟!

文章目录

accuracy方法介绍

paddle.fluid.layers.accuracy(input,label),
input 为输入为网络预测值
label为数据集的标签

def multilayer_perceptron(x):
    #这个fc方法有个扁平化操作。
    hidden1 = fluid.layers.fc(input=x,size=100,act='relu')
    hidden2 = fluid.layers.fc(input=hidden1,size=100,act='relu')
    #softmax 也称为归一化指数函数。这里包含两个步骤,第一步,使用指数函数将所有数值转化为0-1区间内的数
    #第二部,计算概率值,使用转化后的值/转化后值的和。
    y = fluid.layers.fc(input=hidden2,size=10,act='softmax')#?为什么是softmax
    return y
#定义数据层
x = fluid.layers.data(name='x',shape=[1,28,28],dtype='float32')
label = fluid.layers.data(name='y',shape=[1],dtype='int64')
prediction = multilayer_perceptron(x)
acc = fluid.layers.accuracy(input=prediction, label=label)

这里prediction是经过softmax激活函数处理后的形状为(10,)的数组。大家不知道softmax函数作用的可以看这篇文章,很清楚。
一分钟理解softmax函数(超简单)

一句话就是经过softmax函数处理过后,此时数组的值范围是【0,1】,总和加起来等于1.这里的每个值我们也叫做概率值。

accuracy()部分源码解析

我们通过control+鼠标左键进入源码方法中

def accuracy(input, label, k=1, correct=None, total=None):
    if in_dygraph_mode():
        topk_out, topk_indices = nn.topk(input, k=k)
        inputs = { 
            "Out": [topk_out],
            "Indices": [topk_indices],
            "Label": [label]
        }
        acc_out = _varbase_creator(dtype="float32")
        if correct is None:
            correct = _varbase_creator(dtype="int64")
        if total is None:
            total = _varbase_creator(dtype="int64")
        outputs = { 
            "Accuracy": [acc_out],
            "Correct": [correct],
            "Total": [total]
        }
        outs = core.ops.accuracy(inputs, { }, outputs)
        return outs['Accuracy'][0]

    helper = LayerHelper("accuracy", **locals())
    check_type_and_dtype(input, 'input', Variable,
                         ['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="int64")
    if total is None:
        total = helper.create_variable_for_type_inference(dtype="int64")
    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

大家不用自己去看代码,跟着我的思路走就行。

代码中,首先对是否是动态图模型进行判断。然后我们发现,无论是不是动态图,都会执行这个方法:

 topk_out, topk_indices = nn.topk(input, k=k)

而且参数就是我们输入的prediction,那我们再进入这个方法里面看看:

This OP is used to find values and indices of the k largest entries for the last dimension.

这个操作被用来发现k个最大的值以及其索引值,我们没有传入的值,默认为1

举个例子吧:

data = np.array([[2,5,4,7],[6,2,9,4]])
topk(data,1)
out:[[7],[9]][[3],[2]]
topk(data,2)
out:[[7,5],[9,6]][[3,1],[2,0]]

这一步就是找到最大值的索引值,也就是我们将手写数字集所分的类。

我们再往下看:

 outs = core.ops.accuracy(inputs, { }, outputs)

进入到这个方法中:

def accuracy(self):
      if self._tags_total == 0:
            return 1
        return self._tags_correct / self._tags_total

返回值是correct/total,也就是计算正确率。

总结概括

到这里,我基本上就明白了计算流程:
《秒懂-深度学习框架的中计算准确率accuracy()原理(基于paddlepaddle)》

    原文作者:梁先森-在技术的路上奔跑
    原文地址: https://blog.csdn.net/lzx159951/article/details/104877138
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞