Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
2160b34b
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看板
提交
2160b34b
编写于
5月 03, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
better design and add prediction logic
上级
43b2bccd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
80 addition
and
16 deletion
+80
-16
word_embedding/network_conf.py
word_embedding/network_conf.py
+7
-15
word_embedding/predict_v2.py
word_embedding/predict_v2.py
+67
-0
word_embedding/train_v2.py
word_embedding/train_v2.py
+6
-1
未找到文件。
word_embedding/network_conf.py
浏览文件 @
2160b34b
...
@@ -44,13 +44,6 @@ def network_conf(hidden_size, embed_size, dict_size):
...
@@ -44,13 +44,6 @@ def network_conf(hidden_size, embed_size, dict_size):
param_attr
=
paddle
.
attr
.
Param
(
param_attr
=
paddle
.
attr
.
Param
(
initial_std
=
1.
/
math
.
sqrt
(
embed_size
*
8
),
learning_rate
=
1
))
initial_std
=
1.
/
math
.
sqrt
(
embed_size
*
8
),
learning_rate
=
1
))
with
paddle
.
layer
.
mixed
(
size
=
dict_size
-
1
,
act
=
paddle
.
activation
.
Sigmoid
(),
bias_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_b'
))
as
prediction
:
prediction
+=
paddle
.
layer
.
trans_full_matrix_projection
(
input
=
hidden_layer
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_w'
))
cost
=
paddle
.
layer
.
hsigmoid
(
cost
=
paddle
.
layer
.
hsigmoid
(
input
=
hidden_layer
,
input
=
hidden_layer
,
label
=
target_word
,
label
=
target_word
,
...
@@ -58,14 +51,13 @@ def network_conf(hidden_size, embed_size, dict_size):
...
@@ -58,14 +51,13 @@ def network_conf(hidden_size, embed_size, dict_size):
param_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_w'
),
param_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_w'
),
bias_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_b'
))
bias_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_b'
))
parameters
=
paddle
.
parameters
.
create
([
cost
,
prediction
])
with
paddle
.
layer
.
mixed
(
size
=
dict_size
-
1
,
adam_optimizer
=
paddle
.
optimizer
.
Adam
(
act
=
paddle
.
activation
.
Sigmoid
(),
learning_rate
=
3e-3
,
bias_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_b'
))
as
prediction
:
regularization
=
paddle
.
optimizer
.
L2Regularization
(
8e-4
))
prediction
+=
paddle
.
layer
.
trans_full_matrix_projection
(
input
=
hidden_layer
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
'sigmoid_w'
))
input_data_lst
=
[
'firstw'
,
'secondw'
,
'thirdw'
,
'fourthw'
,
'fifthw'
]
input_data_lst
=
[
'firstw'
,
'secondw'
,
'thirdw'
,
'fourthw'
,
'fifthw'
]
trainer
=
paddle
.
trainer
.
SGD
(
cost
,
parameters
,
adam_optimizer
)
return
input_data_lst
,
cost
,
prediction
return
input_data_lst
,
trainer
,
prediction
,
parameters
word_embedding/predict_v2.py
0 → 100644
浏览文件 @
2160b34b
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
paddle.v2
as
paddle
from
network_conf
import
network_conf
import
gzip
def
decode_res
(
infer_res
,
dict_size
):
predict_lbls
=
[]
infer_res
=
infer_res
>
0.5
for
i
,
probs
in
enumerate
(
infer_res
):
idx
=
0
result
=
1
while
idx
<
len
(
probs
):
result
<<=
1
if
probs
[
idx
]:
result
|=
1
if
probs
[
idx
]:
idx
=
idx
*
2
+
2
# right child
else
:
idx
=
idx
*
2
+
1
# left child
predict_lbl
=
result
-
dict_size
predict_lbls
.
append
(
predict_lbl
)
return
predict_lbls
def
main
():
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
4
)
word_dict
=
paddle
.
dataset
.
imikolov
.
build_dict
()
dict_size
=
len
(
word_dict
)
_
,
_
,
prediction
=
network_conf
(
hidden_size
=
256
,
embed_size
=
32
,
dict_size
=
dict_size
)
print
(
'Load model ....'
)
with
gzip
.
open
(
'./models/model_pass_00000.tar.gz'
)
as
f
:
parameters
=
paddle
.
parameters
.
Parameters
.
from_tar
(
f
)
ins_num
=
10
ins_lst
=
[]
ins_lbls
=
[]
ins_buffer
=
paddle
.
reader
.
shuffle
(
lambda
:
paddle
.
dataset
.
imikolov
.
train
(
word_dict
,
5
)(),
buf_size
=
1000
)
for
ins
in
ins_buffer
():
ins_lst
.
append
(
ins
[:
-
1
])
ins_lbls
.
append
(
ins
[
-
1
])
if
len
(
ins_lst
)
>=
ins_num
:
break
infer_res
=
paddle
.
infer
(
output_layer
=
prediction
,
parameters
=
parameters
,
input
=
ins_lst
)
idx_word_dict
=
dict
((
v
,
k
)
for
k
,
v
in
word_dict
.
items
())
predict_lbls
=
decode_res
(
infer_res
,
dict_size
)
predict_words
=
[
idx_word_dict
[
lbl
]
for
lbl
in
predict_lbls
]
gt_words
=
[
idx_word_dict
[
lbl
]
for
lbl
in
ins_lbls
]
for
i
,
ins
in
enumerate
(
ins_lst
):
print
idx_word_dict
[
ins
[
0
]]
+
' '
+
idx_word_dict
[
ins
[
1
]]
+
\
' -> '
+
predict_words
[
i
]
+
' ( '
+
gt_words
[
i
]
+
' )'
if
__name__
==
'__main__'
:
main
()
word_embedding/train_v2.py
浏览文件 @
2160b34b
...
@@ -10,7 +10,7 @@ def main():
...
@@ -10,7 +10,7 @@ def main():
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
word_dict
=
paddle
.
dataset
.
imikolov
.
build_dict
()
word_dict
=
paddle
.
dataset
.
imikolov
.
build_dict
()
dict_size
=
len
(
word_dict
)
dict_size
=
len
(
word_dict
)
input_data_lst
,
trainer
,
_
,
parameters
=
network_conf
(
input_data_lst
,
cost
,
prediction
=
network_conf
(
hidden_size
=
256
,
embed_size
=
32
,
dict_size
=
dict_size
)
hidden_size
=
256
,
embed_size
=
32
,
dict_size
=
dict_size
)
def
event_handler
(
event
):
def
event_handler
(
event
):
...
@@ -29,6 +29,11 @@ def main():
...
@@ -29,6 +29,11 @@ def main():
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
)
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
)
feeding
=
dict
(
zip
(
input_data_lst
,
xrange
(
len
(
input_data_lst
))))
feeding
=
dict
(
zip
(
input_data_lst
,
xrange
(
len
(
input_data_lst
))))
parameters
=
paddle
.
parameters
.
create
([
cost
,
prediction
])
adam_optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
3e-3
,
regularization
=
paddle
.
optimizer
.
L2Regularization
(
8e-4
))
trainer
=
paddle
.
trainer
.
SGD
(
cost
,
parameters
,
adam_optimizer
)
trainer
.
train
(
trainer
.
train
(
paddle
.
batch
(
paddle
.
batch
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录