Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2b1efc35
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2b1efc35
编写于
9月 07, 2021
作者:
W
wawltor
提交者:
GitHub
9月 07, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
transfer the static.accurcay to v2 op (#35494)
* transfer the static.accurcay to v2 api * remove the unused code
上级
28b64075
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
53 addition
and
2 deletion
+53
-2
python/paddle/fluid/layers/metric_op.py
python/paddle/fluid/layers/metric_op.py
+17
-2
python/paddle/fluid/tests/unittests/test_accuracy_op.py
python/paddle/fluid/tests/unittests/test_accuracy_op.py
+36
-0
未找到文件。
python/paddle/fluid/layers/metric_op.py
浏览文件 @
2b1efc35
...
@@ -84,7 +84,9 @@ def accuracy(input, label, k=1, correct=None, total=None):
...
@@ -84,7 +84,9 @@ def accuracy(input, label, k=1, correct=None, total=None):
if
total
is
None
:
if
total
is
None
:
total
=
_varbase_creator
(
dtype
=
"int32"
)
total
=
_varbase_creator
(
dtype
=
"int32"
)
topk_out
,
topk_indices
=
nn
.
topk
(
input
,
k
=
k
)
_k
=
k
.
numpy
().
item
(
0
)
if
isinstance
(
k
,
Variable
)
else
k
topk_out
,
topk_indices
=
_C_ops
.
top_k_v2
(
input
,
'k'
,
_k
,
'sorted'
,
False
)
_acc
,
_
,
_
=
_C_ops
.
accuracy
(
topk_out
,
topk_indices
,
label
,
correct
,
_acc
,
_
,
_
=
_C_ops
.
accuracy
(
topk_out
,
topk_indices
,
label
,
correct
,
total
)
total
)
return
_acc
return
_acc
...
@@ -92,7 +94,20 @@ def accuracy(input, label, k=1, correct=None, total=None):
...
@@ -92,7 +94,20 @@ def accuracy(input, label, k=1, correct=None, total=None):
helper
=
LayerHelper
(
"accuracy"
,
**
locals
())
helper
=
LayerHelper
(
"accuracy"
,
**
locals
())
check_variable_and_dtype
(
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
check_variable_and_dtype
(
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'accuracy'
)
'accuracy'
)
topk_out
,
topk_indices
=
nn
.
topk
(
input
,
k
=
k
)
topk_out
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
)
topk_indices
=
helper
.
create_variable_for_type_inference
(
dtype
=
"int64"
)
inputs
=
{
"X"
:
[
input
]}
if
isinstance
(
k
,
Variable
):
inputs
[
'K'
]
=
[
k
]
else
:
attrs
=
{
'k'
:
k
}
attrs
[
'sorted'
]
=
False
helper
.
append_op
(
type
=
"top_k_v2"
,
inputs
=
inputs
,
attrs
=
attrs
,
outputs
=
{
"Out"
:
[
topk_out
],
"Indices"
:
[
topk_indices
]})
acc_out
=
helper
.
create_variable_for_type_inference
(
dtype
=
"float32"
)
acc_out
=
helper
.
create_variable_for_type_inference
(
dtype
=
"float32"
)
if
correct
is
None
:
if
correct
is
None
:
correct
=
helper
.
create_variable_for_type_inference
(
dtype
=
"int32"
)
correct
=
helper
.
create_variable_for_type_inference
(
dtype
=
"int32"
)
...
...
python/paddle/fluid/tests/unittests/test_accuracy_op.py
浏览文件 @
2b1efc35
...
@@ -78,6 +78,42 @@ class TestAccuracyOpError(unittest.TestCase):
...
@@ -78,6 +78,42 @@ class TestAccuracyOpError(unittest.TestCase):
paddle
.
metric
.
accuracy
(
input
=
x3
,
label
=
label
)
paddle
.
metric
.
accuracy
(
input
=
x3
,
label
=
label
)
class
TestAccuracyAPI1
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
predictions
=
paddle
.
static
.
data
(
shape
=
[
2
,
5
],
name
=
"predictions"
,
dtype
=
"float32"
)
self
.
label
=
paddle
.
static
.
data
(
shape
=
[
2
,
1
],
name
=
"labels"
,
dtype
=
"int64"
)
self
.
result
=
paddle
.
static
.
accuracy
(
input
=
self
.
predictions
,
label
=
self
.
label
,
k
=
1
)
self
.
input_predictions
=
np
.
array
(
[[
0.2
,
0.1
,
0.4
,
0.1
,
0.1
],
[
0.2
,
0.3
,
0.1
,
0.15
,
0.25
]],
dtype
=
"float32"
)
self
.
input_labels
=
np
.
array
([[
2
],
[
0
]],
dtype
=
"int64"
)
self
.
expect_value
=
np
.
array
([
0.5
],
dtype
=
'float32'
)
def
test_api
(
self
):
exe
=
paddle
.
static
.
Executor
()
result
,
=
exe
.
run
(
feed
=
{
"predictions"
:
self
.
input_predictions
,
'labels'
:
self
.
input_labels
},
fetch_list
=
[
self
.
result
.
name
])
self
.
assertEqual
((
result
==
self
.
expect_value
).
all
(),
True
)
class
TestAccuracyAPI2
(
unittest
.
TestCase
):
def
test_api
(
self
):
with
fluid
.
dygraph
.
guard
():
predictions
=
paddle
.
to_tensor
(
[[
0.2
,
0.1
,
0.4
,
0.1
,
0.1
],
[
0.2
,
0.3
,
0.1
,
0.15
,
0.25
]],
dtype
=
'float32'
)
label
=
paddle
.
to_tensor
([[
2
],
[
0
]],
dtype
=
"int64"
)
result
=
paddle
.
static
.
accuracy
(
input
=
predictions
,
label
=
label
,
k
=
1
)
expect_value
=
np
.
array
([
0.5
],
dtype
=
'float32'
)
self
.
assertEqual
((
result
.
numpy
()
==
expect_value
).
all
(),
True
)
class
TestAccuracyAPI
(
unittest
.
TestCase
):
class
TestAccuracyAPI
(
unittest
.
TestCase
):
def
test_api
(
self
):
def
test_api
(
self
):
with
fluid
.
dygraph
.
guard
():
with
fluid
.
dygraph
.
guard
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录