Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
b102d256
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看板
提交
b102d256
编写于
12月 05, 2018
作者:
J
JiabinYang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
polish code
上级
db405c78
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
36 deletion
+30
-36
fluid/PaddleRec/word2vec/infer.py
fluid/PaddleRec/word2vec/infer.py
+27
-33
fluid/PaddleRec/word2vec/train.py
fluid/PaddleRec/word2vec/train.py
+3
-3
未找到文件。
fluid/PaddleRec/word2vec/infer.py
浏览文件 @
b102d256
...
...
@@ -7,7 +7,6 @@ from Queue import PriorityQueue
import
logging
import
argparse
from
sklearn.metrics.pairwise
import
cosine_similarity
from
paddle.fluid.executor
import
global_scope
word_to_id
=
dict
()
id_to_word
=
dict
()
...
...
@@ -42,6 +41,12 @@ def parse_args():
required
=
False
,
default
=
False
,
help
=
'if using infer_once, (default: False)'
)
parser
.
add_argument
(
'--infer_during_train'
,
action
=
'store_true'
,
required
=
False
,
default
=
True
,
help
=
'if using infer_during_train, (default: True)'
)
return
parser
.
parse_args
()
...
...
@@ -78,40 +83,17 @@ def build_test_case(emb):
[
emb5
,
desc5
]]
def
inference_test
(
model_dir
,
args
):
def
inference_test
(
scope
,
model_dir
,
args
):
BuildWord_IdMap
(
args
.
dict_path
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
Scope
=
fluid
.
Scope
()
logger
.
info
(
"model_dir is: {}"
.
format
(
model_dir
+
"/"
))
with
fluid
.
scope_guard
(
Scope
):
inference_prog
()
fluid
.
io
.
load_persistables
(
executor
=
exe
,
dirname
=
model_dir
+
"/"
)
emb
=
np
.
array
(
Scope
.
find_var
(
"embeding"
).
get_tensor
())
test_cases
=
build_test_case
(
emb
)
logger
.
info
(
"inference result: ===================="
)
for
case
in
test_cases
:
pq
=
topK
(
args
.
rank_num
,
emb
,
case
[
0
])
logger
.
info
(
"Test result for {}"
.
format
(
case
[
1
]))
pq_tmps
=
list
()
for
i
in
range
(
args
.
rank_num
):
pq_tmps
.
append
(
pq
.
get
())
for
i
in
range
(
len
(
pq_tmps
)):
logger
.
info
(
"{} nearest is {}, rate is {}"
.
format
(
i
,
id_to_word
[
pq_tmps
[
len
(
pq_tmps
)
-
1
-
i
].
id
],
pq_tmps
[
len
(
pq_tmps
)
-
1
-
i
].
priority
))
del
pq_tmps
[:]
def
infer_with_in_train
(
model_dir
,
rank_num
,
dict_path
):
BuildWord_IdMap
(
dict_path
)
emb
=
np
.
array
(
global_scope
().
find_var
(
"embeding"
).
get_tensor
())
emb
=
np
.
array
(
scope
.
find_var
(
"embeding"
).
get_tensor
())
test_cases
=
build_test_case
(
emb
)
logger
.
info
(
"inference result: ===================="
)
for
case
in
test_cases
:
pq
=
topK
(
rank_num
,
emb
,
case
[
0
])
pq
=
topK
(
args
.
rank_num
,
emb
,
case
[
0
])
logger
.
info
(
"Test result for {}"
.
format
(
case
[
1
]))
pq_tmps
=
list
()
for
i
in
range
(
rank_num
):
for
i
in
range
(
args
.
rank_num
):
pq_tmps
.
append
(
pq
.
get
())
for
i
in
range
(
len
(
pq_tmps
)):
logger
.
info
(
"{} nearest is {}, rate is {}"
.
format
(
i
,
id_to_word
[
...
...
@@ -149,6 +131,9 @@ def topK(k, emb, test_emb):
def
infer_during_train
(
args
):
model_file_list
=
list
()
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
Scope
=
fluid
.
Scope
()
inference_prog
()
while
True
:
time
.
sleep
(
1
)
current_list
=
os
.
listdir
(
args
.
model_output_dir
)
...
...
@@ -167,15 +152,24 @@ def infer_during_train(args):
model_dir
=
args
.
model_output_dir
+
"/"
+
model
if
os
.
path
.
exists
(
model_dir
+
"/_success"
):
logger
.
info
(
"using models from "
+
model_dir
)
inference_test
(
model_dir
,
args
)
with
fluid
.
scope_guard
(
Scope
):
fluid
.
io
.
load_persistables
(
executor
=
exe
,
dirname
=
model_dir
+
"/"
)
inference_test
(
Scope
,
model_dir
,
args
)
model_file_list
=
current_list
def
infer_once
(
args
):
if
os
.
path
.
exists
(
args
.
model_output_dir
+
"/_success"
):
# check models file has already been finished
# check models file has already been finished
if
os
.
path
.
exists
(
args
.
model_output_dir
+
"/_success"
):
logger
.
info
(
"using models from "
+
args
.
model_output_dir
)
inference_test
(
args
.
model_output_dir
,
args
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
Scope
=
fluid
.
Scope
()
inference_prog
()
with
fluid
.
scope_guard
(
Scope
):
fluid
.
io
.
load_persistables
(
executor
=
exe
,
dirname
=
args
.
model_output_dir
+
"/"
)
inference_test
(
Scope
,
args
.
model_output_dir
,
args
)
if
__name__
==
'__main__'
:
...
...
@@ -183,5 +177,5 @@ if __name__ == '__main__':
# while setting infer_once please specify the dir to models file with --model_output_dir
if
args
.
infer_once
:
infer_once
(
args
)
else
:
if
args
.
infer_during_train
:
infer_during_train
(
args
)
fluid/PaddleRec/word2vec/train.py
浏览文件 @
b102d256
...
...
@@ -12,10 +12,11 @@ os.environ["CUDA_VISIBLE_DEVICES"] = ""
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.executor
import
global_scope
import
reader
from
network_conf
import
skip_gram_word2vec
from
infer
import
infer
_with_train
from
infer
import
infer
ence_test
logging
.
basicConfig
(
format
=
'%(asctime)s - %(levelname)s - %(message)s'
)
logger
=
logging
.
getLogger
(
"fluid"
)
...
...
@@ -206,8 +207,7 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id):
if
batch_id
%
1000
==
0
and
batch_id
!=
0
:
model_dir
=
args
.
model_output_dir
+
'/batch-'
+
str
(
batch_id
)
infer_with_in_train
(
model_dir
,
args
.
rank_num
,
args
.
dict_path
)
inference_test
(
global_scope
(),
model_dir
,
args
)
batch_id
+=
1
except
fluid
.
core
.
EOFException
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录