Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
0f3eeda1
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0f3eeda1
编写于
10月 23, 2018
作者:
Q
Qiao Longfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update train and test
上级
cc56cb2e
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
59 addition
and
31 deletion
+59
-31
fluid/recommendation/ctr/infer.py
fluid/recommendation/ctr/infer.py
+40
-25
fluid/recommendation/ctr/network_conf.py
fluid/recommendation/ctr/network_conf.py
+10
-5
fluid/recommendation/ctr/train.py
fluid/recommendation/ctr/train.py
+9
-1
未找到文件。
fluid/recommendation/ctr/infer.py
浏览文件 @
0f3eeda1
...
...
@@ -2,17 +2,19 @@ import os
import
gzip
import
argparse
import
itertools
import
numpy
as
np
import
paddle
.v2
as
paddle
import
paddle
import
paddle.fluid
as
fluid
from
network_conf
import
DeepFM
import
reader
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
"PaddlePaddle DeepFM example"
)
parser
.
add_argument
(
'--model_
gz_
path'
,
'--model_path'
,
type
=
str
,
required
=
True
,
help
=
"The path of model parameters gz file"
)
...
...
@@ -21,11 +23,6 @@ def parse_args():
type
=
str
,
required
=
True
,
help
=
"The path of the dataset to infer"
)
parser
.
add_argument
(
'--prediction_output_path'
,
type
=
str
,
required
=
True
,
help
=
"The path to output the prediction"
)
parser
.
add_argument
(
'--factor_size'
,
type
=
int
,
...
...
@@ -38,25 +35,43 @@ def parse_args():
def
infer
():
args
=
parse_args
()
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
model
=
DeepFM
(
args
.
factor_size
,
infer
=
True
)
parameters
=
paddle
.
parameters
.
Parameters
.
from_tar
(
gzip
.
open
(
args
.
model_gz_path
,
'r'
))
inferer
=
paddle
.
inference
.
Inference
(
output_layer
=
model
,
parameters
=
parameters
)
place
=
fluid
.
CPUPlace
()
inference_scope
=
fluid
.
core
.
Scope
()
dataset
=
reader
.
Dataset
()
infer_reader
=
paddle
.
batch
(
dataset
.
infer
(
args
.
data_path
),
batch_size
=
1000
)
with
open
(
args
.
prediction_output_path
,
'w'
)
as
out
:
for
id
,
batch
in
enumerate
(
infer_reader
()):
res
=
inferer
.
infer
(
input
=
batch
)
predictions
=
[
x
for
x
in
itertools
.
chain
.
from_iterable
(
res
)]
out
.
write
(
'
\n
'
.
join
(
map
(
str
,
predictions
))
+
'
\n
'
)
test_reader
=
paddle
.
batch
(
dataset
.
train
(
args
.
data_path
),
batch_size
=
1000
)
startup_program
=
fluid
.
framework
.
Program
()
test_program
=
fluid
.
framework
.
Program
()
with
fluid
.
framework
.
program_guard
(
test_program
,
startup_program
):
loss
,
data_list
,
auc_var
,
batch_auc_var
=
DeepFM
(
args
.
factor_size
)
exe
=
fluid
.
Executor
(
place
)
#exe.run(startup_program)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
data_list
,
place
=
place
)
with
fluid
.
scope_guard
(
inference_scope
):
[
inference_program
,
_
,
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
args
.
model_path
,
exe
)
print
(
fetch_targets
)
def
set_zero
(
var_name
):
param
=
inference_scope
.
var
(
var_name
).
get_tensor
()
param_array
=
np
.
zeros
(
param
.
_get_dims
()).
astype
(
"int64"
)
param
.
set
(
param_array
,
place
)
auc_states_names
=
[
'_generated_var_2'
,
'_generated_var_3'
]
for
name
in
auc_states_names
:
set_zero
(
name
)
batch_id
=
0
for
data
in
test_reader
():
loss_val
,
auc_val
=
exe
.
run
(
inference_program
,
feed
=
feeder
.
feed
(
data
),
fetch_list
=
fetch_targets
)
if
batch_id
%
100
==
0
:
print
(
"loss: "
+
str
(
loss_val
)
+
" auc_val:"
+
str
(
auc_val
))
batch_id
+=
1
if
__name__
==
'__main__'
:
...
...
fluid/recommendation/ctr/network_conf.py
浏览文件 @
0f3eeda1
import
paddle.fluid
as
fluid
import
math
dense_feature_dim
=
13
sparse_feature_dim
=
117568
...
...
@@ -17,15 +18,19 @@ def DeepFM(factor_size, infer=False):
return
fluid
.
layers
.
embedding
(
input
=
input
,
size
=
[
sparse_feature_dim
,
factor_size
],
param_attr
=
fluid
.
ParamAttr
(
name
=
"SparseFeatFactors"
))
param_attr
=
fluid
.
ParamAttr
(
name
=
"SparseFeatFactors"
,
initializer
=
fluid
.
initializer
.
Normal
(
scale
=
1
/
math
.
sqrt
(
sparse_feature_dim
))
))
sparse_embed_seq
=
map
(
embedding_layer
,
sparse_input_ids
)
concated
=
fluid
.
layers
.
concat
(
sparse_embed_seq
+
[
dense_input
],
axis
=
1
)
fc1
=
fluid
.
layers
.
fc
(
input
=
concated
,
size
=
400
,
act
=
'relu'
)
fc2
=
fluid
.
layers
.
fc
(
input
=
fc1
,
size
=
400
,
act
=
'relu'
)
fc3
=
fluid
.
layers
.
fc
(
input
=
fc2
,
size
=
400
,
act
=
'relu'
)
predict
=
fluid
.
layers
.
fc
(
input
=
fc3
,
size
=
2
,
act
=
'sigmoid'
)
fc1
=
fluid
.
layers
.
fc
(
input
=
concated
,
size
=
400
,
act
=
'relu'
,
param_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Normal
(
scale
=
1
/
math
.
sqrt
(
concated
.
shape
[
1
]))))
fc2
=
fluid
.
layers
.
fc
(
input
=
fc1
,
size
=
400
,
act
=
'relu'
,
param_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Normal
(
scale
=
1
/
math
.
sqrt
(
fc1
.
shape
[
1
]))))
fc3
=
fluid
.
layers
.
fc
(
input
=
fc2
,
size
=
400
,
act
=
'relu'
,
param_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Normal
(
scale
=
1
/
math
.
sqrt
(
fc2
.
shape
[
1
]))))
predict
=
fluid
.
layers
.
fc
(
input
=
fc3
,
size
=
2
,
act
=
'softmax'
,
param_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Normal
(
scale
=
1
/
math
.
sqrt
(
fc3
.
shape
[
1
]))))
data_list
=
[
dense_input
]
+
sparse_input_ids
...
...
fluid/recommendation/ctr/train.py
浏览文件 @
0f3eeda1
...
...
@@ -68,17 +68,25 @@ def train():
place
=
fluid
.
CPUPlace
()
feeder
=
fluid
.
DataFeeder
(
feed_list
=
data_list
,
place
=
place
)
data_name_list
=
[
var
.
name
for
var
in
data_list
]
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
for
pass_id
in
range
(
args
.
num_passes
):
batch_id
=
0
for
data
in
train_reader
():
loss_val
,
auc_val
,
batch_auc_val
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
loss
,
auc_var
,
batch_auc_var
]
)
print
(
'loss :'
+
str
(
loss_val
)
+
" auc : "
+
str
(
auc_val
)
+
" batch_auc : "
+
str
(
batch_auc_val
))
print
(
'pass:'
+
str
(
pass_id
)
+
' batch:'
+
str
(
batch_id
)
+
' loss: '
+
str
(
loss_val
)
+
" auc: "
+
str
(
auc_val
)
+
" batch_auc: "
+
str
(
batch_auc_val
))
batch_id
+=
1
if
batch_id
%
100
==
0
and
batch_id
!=
0
:
model_dir
=
'output/batch-'
+
str
(
batch_id
)
fluid
.
io
.
save_inference_model
(
model_dir
,
data_name_list
,
[
loss
,
auc_var
],
exe
)
model_dir
=
'output/pass-'
+
str
(
pass_id
)
fluid
.
io
.
save_inference_model
(
model_dir
,
data_name_list
,
[
loss_var
,
auc_var
],
exe
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录