Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
ae199073
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
ae199073
编写于
5月 18, 2018
作者:
D
daminglu
提交者:
GitHub
5月 18, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Test word2vec (#10779)
上级
11b6473c
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
39 addition
and
21 deletion
+39
-21
paddle/fluid/inference/tests/test_helper.h
paddle/fluid/inference/tests/test_helper.h
+1
-2
python/paddle/fluid/tests/book/high-level-api/word2vec/test_word2vec_new_api.py
...sts/book/high-level-api/word2vec/test_word2vec_new_api.py
+38
-19
未找到文件。
paddle/fluid/inference/tests/test_helper.h
浏览文件 @
ae199073
...
...
@@ -236,8 +236,7 @@ void TestInference(const std::string& dirname,
// Disable the profiler and print the timing information
paddle
::
platform
::
DisableProfiler
(
paddle
::
platform
::
EventSortingKey
::
kDefault
,
"run_inference_profiler"
);
paddle
::
platform
::
EventSortingKey
::
kDefault
,
"run_inference_profiler"
);
paddle
::
platform
::
ResetProfiler
();
}
...
...
python/paddle/fluid/tests/book/high-level-api/word2vec/
no_
test_word2vec_new_api.py
→
python/paddle/fluid/tests/book/high-level-api/word2vec/test_word2vec_new_api.py
浏览文件 @
ae199073
...
...
@@ -90,7 +90,7 @@ def train_program(is_sparse):
return
avg_cost
def
train
(
use_cuda
,
train_program
,
save_
path
):
def
train
(
use_cuda
,
train_program
,
save_
dirname
):
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imikolov
.
train
(
word_dict
,
N
),
BATCH_SIZE
)
test_reader
=
paddle
.
batch
(
...
...
@@ -99,27 +99,36 @@ def train(use_cuda, train_program, save_path):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
outs
=
trainer
.
test
(
reader
=
test_reader
)
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
outs
=
trainer
.
test
(
reader
=
test_reader
,
feed_order
=
[
'firstw'
,
'secondw'
,
'thirdw'
,
'forthw'
,
'nextw'
])
avg_cost
=
outs
[
0
]
print
(
"loss= "
,
avg_cost
)
if
avg_cost
<
5.0
:
trainer
.
save_params
(
save_path
)
return
if
avg_cost
<
10.0
:
trainer
.
save_params
(
save_dirname
)
trainer
.
stop
()
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
trainer
=
fluid
.
Trainer
(
train_program
,
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
),
place
=
place
)
train_func
=
train_program
,
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
),
place
=
place
)
trainer
.
train
(
reader
=
train_reader
,
num_epochs
=
1
,
event_handler
=
event_handler
)
reader
=
train_reader
,
num_epochs
=
1
,
event_handler
=
event_handler
,
feed_order
=
[
'firstw'
,
'secondw'
,
'thirdw'
,
'forthw'
,
'nextw'
])
def
infer
(
use_cuda
,
inference_program
,
save_
path
):
def
infer
(
use_cuda
,
inference_program
,
save_
dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save_
path
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
save_
dirname
,
place
=
place
)
lod
=
[
0
,
1
]
first_word
=
create_random_lodtensor
(
lod
,
place
,
low
=
0
,
high
=
dict_size
-
1
)
...
...
@@ -127,12 +136,14 @@ def infer(use_cuda, inference_program, save_path):
third_word
=
create_random_lodtensor
(
lod
,
place
,
low
=
0
,
high
=
dict_size
-
1
)
fourth_word
=
create_random_lodtensor
(
lod
,
place
,
low
=
0
,
high
=
dict_size
-
1
)
result
=
inferencer
.
infer
({
result
=
inferencer
.
infer
(
{
'firstw'
:
first_word
,
'secondw'
:
second_word
,
'thirdw'
:
third_word
,
'forthw'
:
fourth_word
})
},
return_numpy
=
False
)
print
(
np
.
array
(
result
[
0
]))
...
...
@@ -140,9 +151,17 @@ def main(use_cuda, is_sparse):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"word2vec.params"
train
(
use_cuda
,
partial
(
train_program
,
is_sparse
),
save_path
)
infer
(
use_cuda
,
partial
(
inference_program
,
is_sparse
),
save_path
)
save_path
=
"word2vec.inference.model"
train
(
use_cuda
=
use_cuda
,
train_program
=
partial
(
train_program
,
is_sparse
),
save_dirname
=
save_path
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
partial
(
inference_program
,
is_sparse
),
save_dirname
=
save_path
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录