Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
兽拳
Paddle
提交
3c78b03b
P
Paddle
项目概览
兽拳
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3c78b03b
编写于
2月 27, 2017
作者:
Y
Yu Yang
提交者:
GitHub
2月 27, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1447 from reyoung/feature/EvaluatorToEvent
Add metric(like Error rate) to Trainer::event.
上级
c3caa842
d5c697e6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
73 addition
and
15 deletion
+73
-15
demo/mnist/api_train_v2.py
demo/mnist/api_train_v2.py
+3
-8
python/paddle/v2/event.py
python/paddle/v2/event.py
+50
-5
python/paddle/v2/trainer.py
python/paddle/v2/trainer.py
+20
-2
未找到文件。
demo/mnist/api_train_v2.py
浏览文件 @
3c78b03b
...
@@ -27,19 +27,14 @@ def main():
...
@@ -27,19 +27,14 @@ def main():
cost
=
paddle
.
layer
.
classification_cost
(
input
=
inference
,
label
=
label
)
cost
=
paddle
.
layer
.
classification_cost
(
input
=
inference
,
label
=
label
)
parameters
=
paddle
.
parameters
.
create
(
cost
)
parameters
=
paddle
.
parameters
.
create
(
cost
)
for
param_name
in
parameters
.
keys
():
array
=
parameters
.
get
(
param_name
)
array
[:]
=
numpy
.
random
.
uniform
(
low
=-
1.0
,
high
=
1.0
,
size
=
array
.
shape
)
parameters
.
set
(
parameter_name
=
param_name
,
value
=
array
)
adam_optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.01
)
adam_optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.01
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
para
=
parameters
.
get
(
'___fc_2__.w0'
)
if
event
.
batch_id
%
100
==
0
:
print
"Pass %d, Batch %d, Cost %f, Weight Mean Of Fc 2 is %f"
%
(
print
"Pass %d, Batch %d, Cost %f, %s"
%
(
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
,
para
.
mean
())
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
,
event
.
metrics
)
else
:
else
:
pass
pass
...
...
python/paddle/v2/event.py
浏览文件 @
3c78b03b
...
@@ -3,8 +3,6 @@ All training events.
...
@@ -3,8 +3,6 @@ All training events.
There are:
There are:
* BeginTraining
* EndTraining
* BeginIteration
* BeginIteration
* EndIteration
* EndIteration
* BeginPass
* BeginPass
...
@@ -12,15 +10,62 @@ There are:
...
@@ -12,15 +10,62 @@ There are:
TODO(yuyang18): Complete it!
TODO(yuyang18): Complete it!
"""
"""
__all__
=
[
'EndIteration'
]
import
py_paddle.swig_paddle
as
api
__all__
=
[
'EndIteration'
,
'BeginIteration'
,
'BeginPass'
,
'EndPass'
]
class
EndIteration
(
object
):
class
WithMetric
(
object
):
def
__init__
(
self
,
evaluator
):
if
not
isinstance
(
evaluator
,
api
.
Evaluator
):
raise
TypeError
(
"Evaluator should be api.Evaluator type"
)
self
.
__evaluator__
=
evaluator
@
property
def
metrics
(
self
):
names
=
self
.
__evaluator__
.
getNames
()
retv
=
dict
()
for
each_name
in
names
:
val
=
self
.
__evaluator__
.
getValue
(
each_name
)
retv
[
each_name
]
=
val
return
retv
class
BeginPass
(
object
):
"""
Event On One Pass Training Start.
"""
def
__init__
(
self
,
pass_id
):
self
.
pass_id
=
pass_id
class
EndPass
(
WithMetric
):
"""
Event On One Pass Training Complete.
"""
def
__init__
(
self
,
pass_id
,
evaluator
):
self
.
pass_id
=
pass_id
WithMetric
.
__init__
(
self
,
evaluator
)
class
BeginIteration
(
object
):
"""
Event On One Batch Training Start.
"""
def
__init__
(
self
,
pass_id
,
batch_id
):
self
.
pass_id
=
pass_id
self
.
batch_id
=
batch_id
class
EndIteration
(
WithMetric
):
"""
"""
Event On One Batch Training Complete.
Event On One Batch Training Complete.
"""
"""
def
__init__
(
self
,
pass_id
,
batch_id
,
cost
):
def
__init__
(
self
,
pass_id
,
batch_id
,
cost
,
evaluator
):
self
.
pass_id
=
pass_id
self
.
pass_id
=
pass_id
self
.
batch_id
=
batch_id
self
.
batch_id
=
batch_id
self
.
cost
=
cost
self
.
cost
=
cost
WithMetric
.
__init__
(
self
,
evaluator
)
python/paddle/v2/trainer.py
浏览文件 @
3c78b03b
...
@@ -97,22 +97,34 @@ class SGD(ITrainer):
...
@@ -97,22 +97,34 @@ class SGD(ITrainer):
topology
,
api
.
CREATE_MODE_NORMAL
,
self
.
__optimizer__
.
enable_types
())
topology
,
api
.
CREATE_MODE_NORMAL
,
self
.
__optimizer__
.
enable_types
())
assert
isinstance
(
gm
,
api
.
GradientMachine
)
assert
isinstance
(
gm
,
api
.
GradientMachine
)
parameters
.
append_gradient_machine
(
gm
)
parameters
.
append_gradient_machine
(
gm
)
gm
.
randParameters
()
updater
=
self
.
__optimizer__
.
create_local_updater
()
updater
=
self
.
__optimizer__
.
create_local_updater
()
updater
.
init
(
gm
)
updater
.
init
(
gm
)
gm
.
start
()
gm
.
start
()
batch_evaluator
=
gm
.
makeEvaluator
()
assert
isinstance
(
batch_evaluator
,
api
.
Evaluator
)
pass_evaluator
=
gm
.
makeEvaluator
()
assert
isinstance
(
pass_evaluator
,
api
.
Evaluator
)
out_args
=
api
.
Arguments
.
createArguments
(
0
)
out_args
=
api
.
Arguments
.
createArguments
(
0
)
feeder
=
DataFeeder
(
data_types
,
reader_dict
)
feeder
=
DataFeeder
(
data_types
,
reader_dict
)
for
pass_id
in
xrange
(
num_passes
):
for
pass_id
in
xrange
(
num_passes
):
event_handler
(
v2_event
.
BeginPass
(
pass_id
))
pass_evaluator
.
start
()
updater
.
startPass
()
updater
.
startPass
()
for
batch_id
,
data_batch
in
enumerate
(
for
batch_id
,
data_batch
in
enumerate
(
__data_reader_to_batch__
(
train_data_reader
,
batch_size
,
__data_reader_to_batch__
(
train_data_reader
,
batch_size
,
topology
)):
topology
)):
batch_evaluator
.
start
()
event_handler
(
v2_event
.
BeginIteration
(
pass_id
=
pass_id
,
batch_id
=
batch_id
))
pass_type
=
updater
.
startBatch
(
len
(
data_batch
))
pass_type
=
updater
.
startBatch
(
len
(
data_batch
))
gm
.
forwardBackward
(
feeder
(
data_batch
),
out_args
,
pass_type
)
gm
.
forwardBackward
(
feeder
(
data_batch
),
out_args
,
pass_type
)
gm
.
eval
(
pass_evaluator
)
gm
.
eval
(
batch_evaluator
)
for
each_param
in
gm
.
getParameters
():
for
each_param
in
gm
.
getParameters
():
updater
.
update
(
each_param
)
updater
.
update
(
each_param
)
# Get cost. We use numpy to calculate total cost for this batch.
# Get cost. We use numpy to calculate total cost for this batch.
...
@@ -120,11 +132,17 @@ class SGD(ITrainer):
...
@@ -120,11 +132,17 @@ class SGD(ITrainer):
cost_vec
=
cost_vec
.
copyToNumpyMat
()
cost_vec
=
cost_vec
.
copyToNumpyMat
()
cost
=
cost_vec
.
sum
()
/
len
(
data_batch
)
cost
=
cost_vec
.
sum
()
/
len
(
data_batch
)
updater
.
finishBatch
(
cost
)
updater
.
finishBatch
(
cost
)
batch_evaluator
.
finish
()
event_handler
(
event_handler
(
v2_event
.
EndIteration
(
v2_event
.
EndIteration
(
pass_id
=
pass_id
,
batch_id
=
batch_id
,
cost
=
cost
))
pass_id
=
pass_id
,
batch_id
=
batch_id
,
cost
=
cost
,
evaluator
=
batch_evaluator
))
updater
.
finishPass
()
updater
.
finishPass
()
pass_evaluator
.
finish
()
event_handler
(
v2_event
.
EndPass
(
pass_id
,
evaluator
=
pass_evaluator
))
gm
.
finish
()
gm
.
finish
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录