Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
40ca043a
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看板
未验证
提交
40ca043a
编写于
2月 17, 2020
作者:
1
123malin
提交者:
GitHub
2月 17, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Upgrade word2vec model (#4292)
上级
9a3fb2be
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
27 addition
and
33 deletion
+27
-33
PaddleRec/word2vec/cluster_train.py
PaddleRec/word2vec/cluster_train.py
+9
-9
PaddleRec/word2vec/infer.py
PaddleRec/word2vec/infer.py
+2
-6
PaddleRec/word2vec/net.py
PaddleRec/word2vec/net.py
+7
-8
PaddleRec/word2vec/train.py
PaddleRec/word2vec/train.py
+9
-10
未找到文件。
PaddleRec/word2vec/cluster_train.py
浏览文件 @
40ca043a
...
...
@@ -134,10 +134,10 @@ def convert_python_to_tensor(weight, batch_size, sample_reader):
return
__reader__
def
train_loop
(
args
,
train_program
,
reader
,
py_re
ader
,
loss
,
trainer_id
,
def
train_loop
(
args
,
train_program
,
reader
,
data_lo
ader
,
loss
,
trainer_id
,
weight
):
py_reader
.
decorate_tensor_provide
r
(
data_loader
.
set_batch_generato
r
(
convert_python_to_tensor
(
weight
,
args
.
batch_size
,
reader
.
train
()))
place
=
fluid
.
CPUPlace
()
...
...
@@ -149,7 +149,7 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id,
train_exe
=
exe
for
pass_id
in
range
(
args
.
num_passes
):
py_re
ader
.
start
()
data_lo
ader
.
start
()
time
.
sleep
(
10
)
epoch_start
=
time
.
time
()
batch_id
=
0
...
...
@@ -164,7 +164,7 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id,
logger
.
info
(
"TRAIN --> pass: {} batch: {} loss: {} reader queue:{}"
.
format
(
pass_id
,
batch_id
,
loss_val
.
mean
(),
py_re
ader
.
queue
.
size
()))
loss_val
.
mean
(),
data_lo
ader
.
queue
.
size
()))
if
args
.
with_speed
:
if
batch_id
%
500
==
0
and
batch_id
!=
0
:
elapsed
=
(
time
.
time
()
-
start
)
...
...
@@ -178,18 +178,18 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id,
model_dir
=
args
.
model_output_dir
+
'/pass-'
+
str
(
pass_id
)
+
(
'/batch-'
+
str
(
batch_id
))
if
trainer_id
==
0
:
fluid
.
io
.
save_params
(
executor
=
exe
,
dirname
=
model_dir
)
fluid
.
save
(
fluid
.
default_main_program
(),
model_path
=
model_dir
)
print
(
"model saved in %s"
%
model_dir
)
batch_id
+=
1
except
fluid
.
core
.
EOFException
:
py_re
ader
.
reset
()
data_lo
ader
.
reset
()
epoch_end
=
time
.
time
()
logger
.
info
(
"Epoch: {0}, Train total expend: {1} "
.
format
(
pass_id
,
epoch_end
-
epoch_start
))
model_dir
=
args
.
model_output_dir
+
'/pass-'
+
str
(
pass_id
)
if
trainer_id
==
0
:
fluid
.
io
.
save_params
(
executor
=
exe
,
dirname
=
model_dir
)
fluid
.
save
(
fluid
.
default_main_program
(),
model_path
=
model_dir
)
print
(
"model saved in %s"
%
model_dir
)
...
...
@@ -210,7 +210,7 @@ def train(args):
np_power
=
np
.
power
(
np
.
array
(
word2vec_reader
.
id_frequencys
),
0.75
)
id_frequencys_pow
=
np_power
/
np_power
.
sum
()
loss
,
py_re
ader
=
skip_gram_word2vec
(
loss
,
data_lo
ader
=
skip_gram_word2vec
(
word2vec_reader
.
dict_size
,
args
.
embedding_size
,
is_sparse
=
args
.
is_sparse
,
...
...
@@ -241,7 +241,7 @@ def train(args):
elif
args
.
role
==
"trainer"
:
print
(
"run trainer"
)
train_loop
(
args
,
t
.
get_trainer_program
(),
word2vec_reader
,
py_re
ader
,
loss
,
t
.
get_trainer_program
(),
word2vec_reader
,
data_lo
ader
,
loss
,
args
.
trainer_id
,
id_frequencys_pow
)
...
...
PaddleRec/word2vec/infer.py
浏览文件 @
40ca043a
...
...
@@ -70,8 +70,7 @@ def infer_epoch(args, vocab_size, test_reader, use_cuda, i2w):
for
epoch
in
range
(
start_index
,
last_index
+
1
):
copy_program
=
main_program
.
clone
()
model_path
=
model_dir
+
"/pass-"
+
str
(
epoch
)
fluid
.
io
.
load_params
(
executor
=
exe
,
dirname
=
model_path
,
main_program
=
copy_program
)
fluid
.
load
(
copy_program
,
model_path
,
exe
)
accum_num
=
0
accum_num_sum
=
0.0
t0
=
time
.
time
()
...
...
@@ -131,10 +130,7 @@ def infer_step(args, vocab_size, test_reader, use_cuda, i2w):
copy_program
=
main_program
.
clone
()
model_path
=
model_dir
+
"/pass-"
+
str
(
epoch
)
+
(
'/batch-'
+
str
(
batchid
*
args
.
print_step
))
fluid
.
io
.
load_params
(
executor
=
exe
,
dirname
=
model_path
,
main_program
=
copy_program
)
fluid
.
load
(
copy_program
,
model_path
,
exe
)
accum_num
=
0
accum_num_sum
=
0.0
t0
=
time
.
time
()
...
...
PaddleRec/word2vec/net.py
浏览文件 @
40ca043a
...
...
@@ -22,20 +22,19 @@ import paddle.fluid as fluid
def
skip_gram_word2vec
(
dict_size
,
embedding_size
,
is_sparse
=
False
,
neg_num
=
5
):
data
s
=
[]
word
s
=
[]
input_word
=
fluid
.
data
(
name
=
"input_word"
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
true_word
=
fluid
.
data
(
name
=
'true_label'
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
neg_word
=
fluid
.
data
(
name
=
"neg_label"
,
shape
=
[
None
,
neg_num
],
dtype
=
'int64'
)
data
s
.
append
(
input_word
)
data
s
.
append
(
true_word
)
data
s
.
append
(
neg_word
)
word
s
.
append
(
input_word
)
word
s
.
append
(
true_word
)
word
s
.
append
(
neg_word
)
py_reader
=
fluid
.
layers
.
create_py_reader_by_data
(
capacity
=
64
,
feed_list
=
datas
,
name
=
'py_reader'
,
use_double_buffer
=
Tru
e
)
data_loader
=
fluid
.
io
.
DataLoader
.
from_generator
(
capacity
=
64
,
feed_list
=
words
,
iterable
=
Fals
e
)
words
=
fluid
.
layers
.
read_file
(
py_reader
)
init_width
=
0.5
/
embedding_size
input_emb
=
fluid
.
embedding
(
input
=
words
[
0
],
...
...
@@ -104,7 +103,7 @@ def skip_gram_word2vec(dict_size, embedding_size, is_sparse=False, neg_num=5):
fluid
.
layers
.
reduce_sum
(
neg_xent
,
dim
=
1
))
avg_cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
return
avg_cost
,
py_re
ader
return
avg_cost
,
data_lo
ader
def
infer_network
(
vocab_size
,
emb_size
):
...
...
PaddleRec/word2vec/train.py
浏览文件 @
40ca043a
...
...
@@ -121,10 +121,9 @@ def convert_python_to_tensor(weight, batch_size, sample_reader):
return
__reader__
def
train_loop
(
args
,
train_program
,
reader
,
py_re
ader
,
loss
,
trainer_id
,
def
train_loop
(
args
,
train_program
,
reader
,
data_lo
ader
,
loss
,
trainer_id
,
weight
):
py_reader
.
decorate_tensor_provider
(
data_loader
.
set_batch_generator
(
convert_python_to_tensor
(
weight
,
args
.
batch_size
,
reader
.
train
()))
place
=
fluid
.
CPUPlace
()
...
...
@@ -149,7 +148,7 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id,
exec_strategy
=
exec_strategy
)
for
pass_id
in
range
(
args
.
num_passes
):
py_re
ader
.
start
()
data_lo
ader
.
start
()
time
.
sleep
(
10
)
epoch_start
=
time
.
time
()
batch_id
=
0
...
...
@@ -164,7 +163,7 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id,
logger
.
info
(
"TRAIN --> pass: {} batch: {} loss: {} reader queue:{}"
.
format
(
pass_id
,
batch_id
,
loss_val
.
mean
(),
py_re
ader
.
queue
.
size
()))
loss_val
.
mean
(),
data_lo
ader
.
queue
.
size
()))
if
args
.
with_speed
:
if
batch_id
%
500
==
0
and
batch_id
!=
0
:
elapsed
=
(
time
.
time
()
-
start
)
...
...
@@ -178,18 +177,18 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id,
model_dir
=
args
.
model_output_dir
+
'/pass-'
+
str
(
pass_id
)
+
(
'/batch-'
+
str
(
batch_id
))
if
trainer_id
==
0
:
fluid
.
io
.
save_params
(
executor
=
exe
,
dirname
=
model_dir
)
fluid
.
save
(
fluid
.
default_main_program
(),
model_path
=
model_dir
)
print
(
"model saved in %s"
%
model_dir
)
batch_id
+=
1
except
fluid
.
core
.
EOFException
:
py_re
ader
.
reset
()
data_lo
ader
.
reset
()
epoch_end
=
time
.
time
()
logger
.
info
(
"Epoch: {0}, Train total expend: {1} "
.
format
(
pass_id
,
epoch_end
-
epoch_start
))
model_dir
=
args
.
model_output_dir
+
'/pass-'
+
str
(
pass_id
)
if
trainer_id
==
0
:
fluid
.
io
.
save_params
(
executor
=
exe
,
dirname
=
model_dir
)
fluid
.
save
(
fluid
.
default_main_program
(),
model_path
=
model_dir
)
print
(
"model saved in %s"
%
model_dir
)
...
...
@@ -215,7 +214,7 @@ def train(args):
np_power
=
np
.
power
(
np
.
array
(
word2vec_reader
.
id_frequencys
),
0.75
)
id_frequencys_pow
=
np_power
/
np_power
.
sum
()
loss
,
py_re
ader
=
skip_gram_word2vec
(
loss
,
data_lo
ader
=
skip_gram_word2vec
(
word2vec_reader
.
dict_size
,
args
.
embedding_size
,
is_sparse
=
args
.
is_sparse
,
...
...
@@ -233,7 +232,7 @@ def train(args):
# do local training
logger
.
info
(
"run local training"
)
main_program
=
fluid
.
default_main_program
()
train_loop
(
args
,
main_program
,
word2vec_reader
,
py_re
ader
,
loss
,
0
,
train_loop
(
args
,
main_program
,
word2vec_reader
,
data_lo
ader
,
loss
,
0
,
id_frequencys_pow
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录