Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
c47e02dc
B
book
项目概览
PaddlePaddle
/
book
通知
16
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c47e02dc
编写于
10月 18, 2017
作者:
P
Peng Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug in saving model (invoke the wrong function)
上级
7caad04c
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
26 addition
and
24 deletion
+26
-24
01.fit_a_line/train.py
01.fit_a_line/train.py
+1
-1
02.recognize_digits/train.py
02.recognize_digits/train.py
+1
-1
03.image_classification/train.py
03.image_classification/train.py
+5
-5
04.word2vec/train.py
04.word2vec/train.py
+9
-8
06.understand_sentiment/train.py
06.understand_sentiment/train.py
+5
-4
07.label_semantic_roles/train.py
07.label_semantic_roles/train.py
+1
-1
08.machine_translation/train.py
08.machine_translation/train.py
+4
-4
未找到文件。
01.fit_a_line/train.py
浏览文件 @
c47e02dc
...
...
@@ -41,7 +41,7 @@ def main():
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
if
event
.
pass_id
%
10
==
0
:
with
open
(
'params_pass_%d.tar'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
trainer
.
save_parameter_
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
uci_housing
.
test
(),
batch_size
=
2
),
feeding
=
feeding
)
...
...
02.recognize_digits/train.py
浏览文件 @
c47e02dc
...
...
@@ -87,7 +87,7 @@ def main():
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
# save parameters
with
open
(
'params_pass_%d.tar'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
trainer
.
save_parameter_
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
128
))
...
...
03.image_classification/train.py
浏览文件 @
c47e02dc
...
...
@@ -57,6 +57,10 @@ def main():
learning_rate_decay_b
=
50000
*
100
,
learning_rate_schedule
=
'discexp'
)
# Create trainer
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
momentum_optimizer
)
# End batch and end pass event handler
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
...
...
@@ -69,7 +73,7 @@ def main():
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
# save parameters
with
open
(
'params_pass_%d.tar'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
trainer
.
save_parameter_
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
...
...
@@ -78,10 +82,6 @@ def main():
'label'
:
1
})
print
"
\n
Test with Pass %d, %s"
%
(
event
.
pass_id
,
result
.
metrics
)
# Create trainer
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
momentum_optimizer
)
# Save the inference topology to protobuf.
inference_topology
=
paddle
.
topology
.
Topology
(
layers
=
out
)
with
open
(
"inference_topology.pkl"
,
'wb'
)
as
f
:
...
...
04.word2vec/train.py
浏览文件 @
c47e02dc
...
...
@@ -76,6 +76,14 @@ def main():
bias_attr
=
paddle
.
attr
.
Param
(
learning_rate
=
2
),
act
=
paddle
.
activation
.
Softmax
())
cost
=
paddle
.
layer
.
classification_cost
(
input
=
predictword
,
label
=
nextword
)
parameters
=
paddle
.
parameters
.
create
(
cost
)
adagrad
=
paddle
.
optimizer
.
AdaGrad
(
learning_rate
=
3e-3
,
regularization
=
paddle
.
optimizer
.
L2Regularization
(
8e-4
))
trainer
=
paddle
.
trainer
.
SGD
(
cost
,
parameters
,
adagrad
)
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
if
event
.
batch_id
%
100
==
0
:
...
...
@@ -88,14 +96,7 @@ def main():
print
"Pass %d, Testing metrics %s"
%
(
event
.
pass_id
,
result
.
metrics
)
with
open
(
"model_%d.tar"
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
cost
=
paddle
.
layer
.
classification_cost
(
input
=
predictword
,
label
=
nextword
)
parameters
=
paddle
.
parameters
.
create
(
cost
)
adagrad
=
paddle
.
optimizer
.
AdaGrad
(
learning_rate
=
3e-3
,
regularization
=
paddle
.
optimizer
.
L2Regularization
(
8e-4
))
trainer
=
paddle
.
trainer
.
SGD
(
cost
,
parameters
,
adagrad
)
trainer
.
save_parameter_to_tar
(
f
)
trainer
.
train
(
paddle
.
batch
(
paddle
.
dataset
.
imikolov
.
train
(
word_dict
,
N
),
32
),
num_passes
=
100
,
...
...
06.understand_sentiment/train.py
浏览文件 @
c47e02dc
...
...
@@ -129,6 +129,10 @@ if __name__ == '__main__':
regularization
=
paddle
.
optimizer
.
L2Regularization
(
rate
=
8e-4
),
model_average
=
paddle
.
optimizer
.
ModelAverage
(
average_window
=
0.5
))
# create trainer
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
adam_optimizer
)
# End batch and end pass event handler
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
...
...
@@ -140,14 +144,11 @@ if __name__ == '__main__':
sys
.
stdout
.
flush
()
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
with
open
(
'./params_pass_%d.tar'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
trainer
.
save_parameter_
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
test_reader
,
feeding
=
feeding
)
print
"
\n
Test with Pass %d, %s"
%
(
event
.
pass_id
,
result
.
metrics
)
# create trainer
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
adam_optimizer
)
# Save the inference topology to protobuf.
inference_topology
=
paddle
.
topology
.
Topology
(
layers
=
output
)
with
open
(
"./inference_topology.pkl"
,
'wb'
)
as
f
:
...
...
07.label_semantic_roles/train.py
浏览文件 @
c47e02dc
...
...
@@ -188,7 +188,7 @@ def main():
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
# save parameters
with
open
(
'params_pass_%d.tar'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
trainer
.
save_parameter_
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
test_reader
,
feeding
=
feeding
)
print
"
\n
Test with Pass %d, %s"
%
(
event
.
pass_id
,
result
.
metrics
)
...
...
08.machine_translation/train.py
浏览文件 @
c47e02dc
...
...
@@ -5,9 +5,9 @@ import paddle.v2 as paddle
with_gpu
=
os
.
getenv
(
'WITH_GPU'
,
'0'
)
!=
'0'
def
save_model
(
parameters
,
save_path
):
def
save_model
(
trainer
,
parameters
,
save_path
):
with
open
(
save_path
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
trainer
.
save_parameter_
to_tar
(
f
)
def
seq_to_seq_net
(
source_dict_dim
,
...
...
@@ -175,12 +175,12 @@ def main():
if
not
event
.
batch_id
%
10
:
save_path
=
'params_pass_%05d_batch_%05d.tar'
%
(
event
.
pass_id
,
event
.
batch_id
)
save_model
(
parameters
,
save_path
)
save_model
(
trainer
,
parameters
,
save_path
)
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
# save parameters
save_path
=
'params_pass_%05d.tar'
%
(
event
.
pass_id
)
save_model
(
parameters
,
save_path
)
save_model
(
trainer
,
parameters
,
save_path
)
# start to train
trainer
.
train
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录