Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleRec
提交
bf0adc5b
P
PaddleRec
项目概览
PaddlePaddle
/
PaddleRec
通知
68
Star
12
Fork
5
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
27
列表
看板
标记
里程碑
合并请求
10
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleRec
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
27
Issue
27
列表
看板
标记
里程碑
合并请求
10
合并请求
10
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
bf0adc5b
编写于
7月 21, 2020
作者:
M
malin10
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add precision metrics
上级
9b89d8f7
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
124 addition
and
4 deletion
+124
-4
core/metric.py
core/metric.py
+0
-3
core/metrics/__init__.py
core/metrics/__init__.py
+4
-0
core/metrics/precision.py
core/metrics/precision.py
+107
-0
core/model.py
core/model.py
+9
-1
core/trainers/framework/runner.py
core/trainers/framework/runner.py
+4
-0
未找到文件。
core/metric.py
浏览文件 @
bf0adc5b
...
...
@@ -24,7 +24,6 @@ class Metric(object):
""" """
pass
@
abc
.
abstractmethod
def
clear
(
self
,
scope
,
params
):
"""
clear current value
...
...
@@ -34,7 +33,6 @@ class Metric(object):
"""
pass
@
abc
.
abstractmethod
def
calculate
(
self
,
scope
,
params
):
"""
calculate result
...
...
@@ -52,7 +50,6 @@ class Metric(object):
"""
pass
@
abc
.
abstractmethod
def
__str__
(
self
):
"""
Return:
...
...
core/metrics/__init__.py
浏览文件 @
bf0adc5b
...
...
@@ -11,3 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
precision
import
Precision
__all__
=
[
'Precision'
]
core/metrics/precision.py
0 → 100755
浏览文件 @
bf0adc5b
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
math
import
numpy
as
np
import
paddle.fluid
as
fluid
from
paddlerec.core.metric
import
Metric
from
paddle.fluid.layers
import
nn
,
accuracy
from
paddle.fluid.initializer
import
Constant
from
paddle.fluid.layer_helper
import
LayerHelper
class
Precision
(
Metric
):
"""
Metric For Fluid Model
"""
def
__init__
(
self
,
**
kwargs
):
""" """
helper
=
LayerHelper
(
"PaddleRec_Precision"
,
**
kwargs
)
self
.
batch_accuracy
=
accuracy
(
kwargs
.
get
(
"input"
),
kwargs
.
get
(
"label"
),
kwargs
.
get
(
"k"
))
local_ins_num
,
_
=
helper
.
create_or_get_global_variable
(
name
=
"local_ins_num"
,
persistable
=
True
,
dtype
=
'float32'
,
shape
=
[
1
])
local_pos_num
,
_
=
helper
.
create_or_get_global_variable
(
name
=
"local_pos_num"
,
persistable
=
True
,
dtype
=
'float32'
,
shape
=
[
1
])
batch_pos_num
,
_
=
helper
.
create_or_get_global_variable
(
name
=
"batch_pos_num"
,
persistable
=
False
,
dtype
=
'float32'
,
shape
=
[
1
])
batch_ins_num
,
_
=
helper
.
create_or_get_global_variable
(
name
=
"batch_ins_num"
,
persistable
=
False
,
dtype
=
'float32'
,
shape
=
[
1
])
tmp_ones
=
helper
.
create_global_variable
(
name
=
"batch_size_like_ones"
,
persistable
=
False
,
dtype
=
'float32'
,
shape
=
[
-
1
])
for
var
in
[
batch_pos_num
,
batch_ins_num
,
local_pos_num
,
local_ins_num
]:
print
(
var
,
type
(
var
))
helper
.
set_variable_initializer
(
var
,
Constant
(
value
=
0.0
,
force_cpu
=
True
))
helper
.
append_op
(
type
=
'fill_constant_batch_size_like'
,
inputs
=
{
"Input"
:
kwargs
.
get
(
"label"
)},
outputs
=
{
'Out'
:
[
tmp_ones
]},
attrs
=
{
'shape'
:
[
-
1
,
1
],
'dtype'
:
tmp_ones
.
dtype
,
'value'
:
float
(
1.0
),
})
helper
.
append_op
(
type
=
"reduce_sum"
,
inputs
=
{
"X"
:
[
tmp_ones
]},
outputs
=
{
"Out"
:
[
batch_ins_num
]})
helper
.
append_op
(
type
=
"elementwise_mul"
,
inputs
=
{
"X"
:
[
batch_ins_num
],
"Y"
:
[
self
.
batch_accuracy
]},
outputs
=
{
"Out"
:
[
batch_pos_num
]})
helper
.
append_op
(
type
=
"elementwise_add"
,
inputs
=
{
"X"
:
[
local_pos_num
],
"Y"
:
[
batch_pos_num
]},
outputs
=
{
"Out"
:
[
local_pos_num
]})
helper
.
append_op
(
type
=
"elementwise_add"
,
inputs
=
{
"X"
:
[
local_ins_num
],
"Y"
:
[
batch_ins_num
]},
outputs
=
{
"Out"
:
[
local_ins_num
]})
self
.
accuracy
=
local_pos_num
/
local_ins_num
self
.
metrics
=
dict
()
metric_varname
=
"P@%d"
%
kwargs
.
get
(
"k"
)
self
.
metrics
[
metric_varname
]
=
self
.
accuracy
def
get_result
(
self
):
return
self
.
metrics
core/model.py
浏览文件 @
bf0adc5b
...
...
@@ -15,7 +15,9 @@
import
abc
import
os
import
paddle.fluid
as
fluid
from
paddle.fluid.framework
import
Variable
from
paddlerec.core.metric
import
Metric
from
paddlerec.core.utils
import
envs
...
...
@@ -120,7 +122,13 @@ class ModelBase(object):
def
get_metrics
(
self
):
"""R
"""
return
self
.
_metrics
res
=
dict
()
for
key
in
self
.
_metrics
:
if
isinstance
(
self
.
_metrics
[
key
],
Metric
):
res
.
update
(
self
.
_metrics
[
key
].
get_result
())
elif
isinstance
(
self
.
_metrics
[
key
],
Variable
):
res
[
key
]
=
self
.
_metrics
[
key
]
return
res
def
get_fetch_period
(
self
):
return
self
.
_fetch_interval
...
...
core/trainers/framework/runner.py
浏览文件 @
bf0adc5b
...
...
@@ -20,6 +20,7 @@ import numpy as np
import
paddle.fluid
as
fluid
from
paddlerec.core.utils
import
envs
from
paddle.fluid.data_feeder
import
convert_dtype
__all__
=
[
"RunnerBase"
,
"SingleRunner"
,
"PSRunner"
,
"CollectiveRunner"
,
"PslibRunner"
...
...
@@ -221,6 +222,9 @@ class RunnerBase(object):
program
=
context
[
"model"
][
model_name
][
"main_program"
].
clone
()
_exe_strategy
,
_build_strategy
=
self
.
_get_strategy
(
model_dict
,
context
)
with
open
(
"program.proto"
,
'w'
)
as
fout
:
fout
.
write
(
str
(
program
))
program
=
fluid
.
compiler
.
CompiledProgram
(
program
).
with_data_parallel
(
loss_name
=
model_class
.
get_avg_cost
().
name
,
build_strategy
=
_build_strategy
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录