Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
97bd23f1
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
282
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
97bd23f1
编写于
1月 07, 2019
作者:
Z
Zeyu Chen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify test_export_n_load_module.py
上级
6f8fe813
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
30 addition
and
62 deletion
+30
-62
paddle_hub/module.py
paddle_hub/module.py
+0
-8
tests/test_export_n_load_module.py
tests/test_export_n_load_module.py
+29
-54
tests/test_module.py
tests/test_module.py
+1
-0
未找到文件。
paddle_hub/module.py
浏览文件 @
97bd23f1
...
...
@@ -79,10 +79,6 @@ class Module(object):
self
.
config
=
ModuleConfig
(
self
.
module_dir
)
self
.
config
.
load
()
# load assets
# self.dict = defaultdict(int)
# self.dict.setdefault(0)
# self._load_assets(module_dir)
def
_construct_feed_dict
(
self
,
inputs
):
""" Construct feed dict according to user's inputs and module config.
...
...
@@ -244,10 +240,6 @@ class ModuleConfig(object):
"""
for
w
in
word_dict
:
self
.
dict
[
w
]
=
word_dict
[
w
]
# mkdir(self.module_dir)
# with open(os.path.join(self.module_dir, DICT_NAME), "w") as fo:
# for w in word_dict:
# self.dict[w] = word_dict[w]
def
register_feed_signature
(
self
,
feed_desc
,
sign_name
=
"default"
):
""" Register feed signature to the Module
...
...
tests/test_export_n_load_module.py
浏览文件 @
97bd23f1
...
...
@@ -53,12 +53,13 @@ print("imikolov simple dataset batch_size =", batch_size)
def
module_fn
(
trainable
=
False
):
# Define module function for saving module
# create word input
words
=
fluid
.
layers
.
data
(
name
=
"words"
,
shape
=
[
1
],
lod_level
=
1
,
dtype
=
"int64"
)
# create embedding
emb_name
=
"
embedding
"
emb_name
=
"
w2v_emb
"
emb_param_attr
=
fluid
.
ParamAttr
(
name
=
emb_name
,
trainable
=
trainable
)
word_emb
=
fluid
.
layers
.
embedding
(
input
=
words
,
...
...
@@ -133,11 +134,6 @@ def train(use_cuda=False):
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
startup_program
)
# initialization
# fluid.io.load_vars(
# executor=exe,
# dirname="./w2v_model",
# vars=[main_program.global_block().var("embedding")])
# 也可使用predicate方式搜索变量
step
=
0
for
epoch
in
range
(
0
,
PASS_NUM
):
for
mini_batch
in
batch_reader
():
...
...
@@ -159,18 +155,7 @@ def train(use_cuda=False):
if
step
%
100
==
0
:
print
(
"Epoch={} Step={} Cost={}"
.
format
(
epoch
,
step
,
cost
[
0
]))
model_dir
=
"./tmp/w2v_model"
# save part of model
var_list_to_saved
=
[
main_program
.
global_block
().
var
(
"embedding"
)]
print
(
"saving model to %s"
%
model_dir
)
fluid
.
io
.
save_vars
(
executor
=
exe
,
dirname
=
model_dir
+
"_save_vars"
,
vars
=
var_list_to_saved
)
# save the whole model
fluid
.
io
.
save_persistables
(
executor
=
exe
,
dirname
=
model_dir
+
"_save_persistables"
)
saved_model_dir
=
"./tmp/word2vec_inference_model"
saved_model_dir
=
"./tmp/word2vec_test_model"
# save inference model including feed and fetch variable info
fluid
.
io
.
save_inference_model
(
dirname
=
saved_model_dir
,
...
...
@@ -187,7 +172,7 @@ def train(use_cuda=False):
w_id
+=
1
# save word dict to assets folder
config
=
hub
.
ModuleConfig
(
model_dir
)
config
=
hub
.
ModuleConfig
(
saved_
model_dir
)
config
.
save_dict
(
word_dict
=
dictionary
)
config
.
dump
()
...
...
@@ -198,28 +183,27 @@ def test_save_module(use_cuda=False):
exe
=
fluid
.
Executor
(
place
)
main_program
=
fluid
.
Program
()
startup_program
=
fluid
.
Program
()
exe
=
fluid
.
Executor
(
place
)
with
fluid
.
program_guard
(
main_program
,
startup_program
):
words
,
word_emb
=
module_fn
()
exe
.
run
(
startup_program
)
# load inference embedding parameters
saved_model_dir
=
"./tmp/word2vec_
inference
_model"
saved_model_dir
=
"./tmp/word2vec_
test
_model"
fluid
.
io
.
load_inference_model
(
executor
=
exe
,
dirname
=
saved_model_dir
)
feed_var_list
=
[
main_program
.
global_block
().
var
(
"words"
)]
feeder
=
fluid
.
DataFeeder
(
feed_list
=
feed_var_list
,
place
=
place
)
results
=
exe
.
run
(
main_program
,
feed
=
feeder
.
feed
([[[
1
,
2
,
3
,
4
,
5
]]]),
fetch_list
=
[
word_emb
],
return_numpy
=
False
)
#
feed_var_list = [main_program.global_block().var("words")]
#
feeder = fluid.DataFeeder(feed_list=feed_var_list, place=place)
#
results = exe.run(
#
main_program,
#
feed=feeder.feed([[[1, 2, 3, 4, 5]]]),
#
fetch_list=[word_emb],
#
return_numpy=False)
np_result
=
np
.
array
(
results
[
0
])
print
(
np_result
)
#
np_result = np.array(results[0])
#
print(np_result)
# save module_dir
saved_module_dir
=
"./tmp/word2vec_
inference
_module"
saved_module_dir
=
"./tmp/word2vec_
test
_module"
fluid
.
io
.
save_inference_model
(
dirname
=
saved_module_dir
,
feeded_var_names
=
[
"words"
],
...
...
@@ -233,40 +217,31 @@ def test_save_module(use_cuda=False):
w
=
w
.
decode
(
"ascii"
)
dictionary
[
w
]
=
w_id
w_id
+=
1
# save word dict to assets folder
input_desc
=
{
"words"
:
words
.
name
}
output_desc
=
{
"emb"
:
word_emb
.
name
}
config
=
hub
.
ModuleConfig
(
saved_module_dir
)
config
.
register_feed_signature
(
input_desc
,
sign_name
=
"default"
)
config
.
register_fetch_signature
(
output_desc
,
sign_name
=
"default"
)
config
.
save_dict
(
word_dict
=
dictionary
)
config
.
dump
()
def
test_load_module
(
use_cuda
=
False
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
saved_module_dir
=
"./tmp/word2vec_inference_module"
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
saved_module_dir
,
executor
=
exe
)
# Sequence input in Paddle must be LOD Tensor, so we need to convert them inside Module
word_ids
=
[[
1
,
2
,
3
,
4
,
5
]]
lod
=
[[
5
]]
word_ids_lod_tensor
=
fluid
.
create_lod_tensor
(
word_ids
,
lod
,
place
)
results
=
exe
.
run
(
inference_program
,
feed
=
{
feed_target_names
[
0
]:
word_ids_lod_tensor
},
fetch_list
=
fetch_targets
,
return_numpy
=
False
)
saved_module_dir
=
"./tmp/word2vec_test_module"
w2v_module
=
hub
.
Module
(
module_dir
=
saved_module_dir
)
print
(
feed_target_names
)
print
(
fetch_target
s
)
np_result
=
np
.
array
(
results
[
0
]
)
print
(
np_
result
)
word_ids
=
[[
1
,
2
,
3
,
4
,
5
]]
# test sequence
word_ids_lod_tensor
=
w2v_module
.
_preprocess_input
(
word_id
s
)
result
=
w2v_module
({
"words"
:
word_ids_lod_tensor
}
)
print
(
result
)
if
__name__
==
"__main__"
:
use_cuda
=
True
print
(
"train..."
)
train
(
use_cuda
)
print
(
"save module..."
)
test_save_module
()
print
(
"load module..."
)
test_load_module
()
tests/test_module.py
浏览文件 @
97bd23f1
# coding=utf-8
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录