Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleRec
提交
8ab19129
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看板
提交
8ab19129
编写于
8月 15, 2019
作者:
R
rensilin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
eol
Change-Id: I5b1a5ed7cde4d65fb499e038c530fec7acb4f2b7
上级
fca2f65a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
87 addition
and
87 deletion
+87
-87
paddle/fluid/train/custom_trainer/feed/scripts/create_programs.py
...luid/train/custom_trainer/feed/scripts/create_programs.py
+87
-87
未找到文件。
paddle/fluid/train/custom_trainer/feed/scripts/create_programs.py
浏览文件 @
8ab19129
#!/usr/bin/env python
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#-*- coding:utf-8 -*-
from
__future__
import
print_function
,
division
from
__future__
import
print_function
,
division
import
os
import
os
import
sys
import
sys
import
paddle
import
paddle
from
paddle
import
fluid
from
paddle
import
fluid
import
yaml
import
yaml
def
print_help
(
this_name
):
def
print_help
(
this_name
):
"""Print help
"""Print help
"""
"""
dirname
=
os
.
path
.
dirname
(
this_name
)
dirname
=
os
.
path
.
dirname
(
this_name
)
print
(
"Usage: {} <network building filename> [model_dir]
\n
"
.
format
(
this_name
))
print
(
"Usage: {} <network building filename> [model_dir]
\n
"
.
format
(
this_name
))
print
(
" example: {} {}"
.
format
(
this_name
,
os
.
path
.
join
(
dirname
,
'example.py'
)))
print
(
" example: {} {}"
.
format
(
this_name
,
os
.
path
.
join
(
dirname
,
'example.py'
)))
def
inference
(
filename
):
def
inference
(
filename
):
"""Build inference network(without loss and optimizer)
"""Build inference network(without loss and optimizer)
Args:
Args:
filename: path of file which defined real inference function
filename: path of file which defined real inference function
Returns:
Returns:
list<Variable>: inputs
list<Variable>: inputs
and
and
Variable: ctr_output
Variable: ctr_output
"""
"""
with
open
(
filename
,
'r'
)
as
f
:
with
open
(
filename
,
'r'
)
as
f
:
code
=
f
.
read
()
code
=
f
.
read
()
compiled
=
compile
(
code
,
filename
,
'exec'
)
compiled
=
compile
(
code
,
filename
,
'exec'
)
exec
(
compiled
)
exec
(
compiled
)
return
inference
()
return
inference
()
def
main
(
argv
):
def
main
(
argv
):
"""Create programs
"""Create programs
Args:
Args:
argv: arg list, length should be 2
argv: arg list, length should be 2
"""
"""
if
len
(
argv
)
<
2
or
not
os
.
path
.
exists
(
argv
[
1
]):
if
len
(
argv
)
<
2
or
not
os
.
path
.
exists
(
argv
[
1
]):
print_help
(
argv
[
0
])
print_help
(
argv
[
0
])
exit
(
1
)
exit
(
1
)
network_build_file
=
argv
[
1
]
network_build_file
=
argv
[
1
]
if
len
(
argv
)
>=
2
:
if
len
(
argv
)
>=
2
:
model_dir
=
argv
[
2
]
model_dir
=
argv
[
2
]
else
:
else
:
model_dir
=
'./model'
model_dir
=
'./model'
main_program
=
fluid
.
Program
()
main_program
=
fluid
.
Program
()
startup_program
=
fluid
.
Program
()
startup_program
=
fluid
.
Program
()
with
fluid
.
program_guard
(
main_program
,
startup_program
):
with
fluid
.
program_guard
(
main_program
,
startup_program
):
inputs
,
ctr_output
=
inference
(
network_build_file
)
inputs
,
ctr_output
=
inference
(
network_build_file
)
test_program
=
main_program
.
clone
(
for_test
=
True
)
test_program
=
main_program
.
clone
(
for_test
=
True
)
label_target
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'float32'
)
label_target
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'float32'
)
loss
=
fluid
.
layers
.
square_error_cost
(
input
=
ctr_output
,
label
=
label_target
)
loss
=
fluid
.
layers
.
square_error_cost
(
input
=
ctr_output
,
label
=
label_target
)
loss
=
fluid
.
layers
.
mean
(
loss
,
name
=
'loss'
)
loss
=
fluid
.
layers
.
mean
(
loss
,
name
=
'loss'
)
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
1.0
)
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
1.0
)
params_grads
=
optimizer
.
backward
(
loss
)
params_grads
=
optimizer
.
backward
(
loss
)
if
not
os
.
path
.
exists
(
model_dir
):
if
not
os
.
path
.
exists
(
model_dir
):
os
.
mkdir
(
model_dir
)
os
.
mkdir
(
model_dir
)
programs
=
{
programs
=
{
'startup_program'
:
startup_program
,
'startup_program'
:
startup_program
,
'main_program'
:
main_program
,
'main_program'
:
main_program
,
'test_program'
:
test_program
,
'test_program'
:
test_program
,
}
}
for
save_path
,
program
in
programs
.
items
():
for
save_path
,
program
in
programs
.
items
():
with
open
(
os
.
path
.
join
(
model_dir
,
save_path
),
'w'
)
as
f
:
with
open
(
os
.
path
.
join
(
model_dir
,
save_path
),
'w'
)
as
f
:
f
.
write
(
program
.
desc
.
serialize_to_string
())
f
.
write
(
program
.
desc
.
serialize_to_string
())
model_desc_path
=
os
.
path
.
join
(
model_dir
,
'model.yaml'
)
model_desc_path
=
os
.
path
.
join
(
model_dir
,
'model.yaml'
)
model_desc
=
dict
()
model_desc
=
dict
()
model_desc
[
'inputs'
]
=
{
var
.
name
:
var
.
shape
for
var
in
inputs
}
model_desc
[
'inputs'
]
=
{
var
.
name
:
var
.
shape
for
var
in
inputs
}
model_desc
[
'loss_name'
]
=
loss
.
name
model_desc
[
'loss_name'
]
=
loss
.
name
model_desc
[
'label_name'
]
=
label_target
.
name
model_desc
[
'label_name'
]
=
label_target
.
name
model_desc
[
'ctr_output_name'
]
=
ctr_output
.
name
model_desc
[
'ctr_output_name'
]
=
ctr_output
.
name
with
open
(
model_desc_path
,
'w'
)
as
f
:
with
open
(
model_desc_path
,
'w'
)
as
f
:
yaml
.
safe_dump
(
model_desc
,
f
,
encoding
=
'utf-8'
,
allow_unicode
=
True
)
yaml
.
safe_dump
(
model_desc
,
f
,
encoding
=
'utf-8'
,
allow_unicode
=
True
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
(
sys
.
argv
)
main
(
sys
.
argv
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录