首页 > 编程知识 正文

conquer用法,accuracy音标

时间:2023-05-03 15:24:18 阅读:117545 作者:4804

桨中accuracy的使用方法

正文只作为笔记本使用。

我到现在还不知道这是怎么调用的,但我只知道它在例行程序中是这样使用的,所以莫名其妙。

试着记录一下那个的使用方法吧。

源代码如下:

def accuracy (输入,标签,k=1,correct=None, total=none (: ' ' accuracy layer.refer to the https://en.Wikipedia.org/wiki/precision _ and _ recallthisfunctioncomputestheaccuracyusingtheinputandlabel.ifthecorrectlabelocccursintopkpredictions, thencorrectwillincrementbyone.note : thedtypeofaccuracyisdeterminedbyinput.theinputandlabeldtypecanbedifferent.args 333333 f accuracy layer,whichisthepredictionsofnetwork.alodtensorortensorwithtypefloat 32,float 64.theshapeis ` [ sample _ numu class_dim]`.label(variable ) 3360 thelabelofdataset.lodtensorortensor int 64.theshapeis ` ` [ sample _ nu mber,1 ` . k(int ) : thetopkpredictionsforeachclasswillbechecked.datatypeisint 64 orint 32.correct ) variable )。 3360 thecorrectpredictionscount.atensorwithtypeint 64 orint 32.total (可变) )。 3360 thetotalentriescount.atensorwithtypeint 64 orint 32.returns 3360 variable 3360 the correct rate.atensorwithtypefloat 32.eeturns thonimportpaddle.fluidasfluidimportnumpyasnpdata=fluid.data (name=' input ',shape=[-1] dtype='float32 ) luid dtype='int ' ) fc_out=fluid.label size=10 ) predict=fluid.layers.soft max (input=fc _ out ) result=fluid layers.accuracy k=5) place=fluid.CPUPlace () exe=fluid.executor ) place ) exe.run ) fluid.default _ startup 32 )、astype )、float32 )、y=NP.array([1]、[0]、[1] ) output=exe.run ) feed={'input'3360x,' input dype=float 32 (' ' ifin _ dy graph _ mode ) ) : ifcorrectisnone : correct=_ varbase _ creator ) dtype='int

ndices = nn.topk(input, k=k) _acc, _, _ = core.ops.accuracy(topk_out, topk_indices, label, correct, total) return _acc helper = LayerHelper("accuracy", hldxllocals()) 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())

错误信息:在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]]

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。