Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
e6e8bfb4
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
1 年多 前同步成功
通知
696
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e6e8bfb4
编写于
2月 28, 2017
作者:
D
dangqingqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
d60116db
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
34 deletion
+46
-34
demo/semantic_role_labeling/api_train_v2.py
demo/semantic_role_labeling/api_train_v2.py
+25
-30
demo/semantic_role_labeling/model_v2.py
demo/semantic_role_labeling/model_v2.py
+21
-4
未找到文件。
demo/semantic_role_labeling/api_train_v2.py
浏览文件 @
e6e8bfb4
...
@@ -2,6 +2,8 @@ import numpy
...
@@ -2,6 +2,8 @@ import numpy
import
paddle.v2
as
paddle
import
paddle.v2
as
paddle
from
model_v2
import
db_lstm
from
model_v2
import
db_lstm
UNK_IDX
=
0
word_dict_file
=
'./data/wordDict.txt'
word_dict_file
=
'./data/wordDict.txt'
label_dict_file
=
'./data/targetDict.txt'
label_dict_file
=
'./data/targetDict.txt'
predicate_file
=
'./data/verbDict.txt'
predicate_file
=
'./data/verbDict.txt'
...
@@ -29,6 +31,10 @@ word_dict_len = len(word_dict)
...
@@ -29,6 +31,10 @@ word_dict_len = len(word_dict)
label_dict_len
=
len
(
label_dict
)
label_dict_len
=
len
(
label_dict
)
pred_len
=
len
(
predicate_dict
)
pred_len
=
len
(
predicate_dict
)
print
'word_dict_len=%d'
%
word_dict_len
print
'label_dict_len=%d'
%
label_dict_len
print
'pred_len=%d'
%
pred_len
def
train_reader
(
file_name
=
"data/feature"
):
def
train_reader
(
file_name
=
"data/feature"
):
def
reader
():
def
reader
():
...
@@ -63,31 +69,16 @@ def main():
...
@@ -63,31 +69,16 @@ def main():
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
# define network topology
# define network topology
output
=
db_lstm
(
word_dict_len
,
label_dict_len
,
pred_len
)
crf_cost
,
crf_dec
=
db_lstm
(
word_dict_len
,
label_dict_len
,
pred_len
)
target
=
paddle
.
layer
.
data
(
name
=
'target'
,
size
=
label_dict_len
)
crf_cost
=
paddle
.
layer
.
crf_layer
(
#parameters = paddle.parameters.create([crf_cost, crf_dec])
size
=
500
,
parameters
=
paddle
.
parameters
.
create
(
crf_cost
)
input
=
output
,
label
=
target
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
'crfw'
,
initial_std
=
default_std
,
learning_rate
=
mix_hidden_lr
))
crf_dec
=
paddle
.
layer
.
crf_decoding_layer
(
name
=
'crf_dec_l'
,
size
=
label_dict_len
,
input
=
output
,
label
=
target
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
'crfw'
))
topo
=
[
crf_cost
,
crf_dec
]
parameters
=
paddle
.
parameters
.
create
(
topo
)
optimizer
=
paddle
.
optimizer
.
Momentum
(
momentum
=
0.01
,
learning_rate
=
2e-2
)
optimizer
=
paddle
.
optimizer
.
Momentum
(
momentum
=
0.01
,
learning_rate
=
2e-2
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
para
=
parameters
.
get
(
'___fc_2__.w0'
)
print
"Pass %d, Batch %d, Cost %f"
%
(
event
.
pass_id
,
event
.
batch_id
,
print
"Pass %d, Batch %d, Cost %f"
%
(
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
,
para
.
mean
()
)
event
.
cost
)
else
:
else
:
pass
pass
...
@@ -96,23 +87,27 @@ def main():
...
@@ -96,23 +87,27 @@ def main():
reader_dict
=
{
reader_dict
=
{
'word_data'
:
0
,
'word_data'
:
0
,
'
verb
_data'
:
1
,
'
ctx_n2
_data'
:
1
,
'ctx_n
2
_data'
:
2
,
'ctx_n
1
_data'
:
2
,
'ctx_
n1
_data'
:
3
,
'ctx_
0
_data'
:
3
,
'ctx_
0
_data'
:
4
,
'ctx_
p1
_data'
:
4
,
'ctx_p
1
_data'
:
5
,
'ctx_p
2
_data'
:
5
,
'
ctx_p2
_data'
:
6
,
'
verb
_data'
:
6
,
'mark_data'
:
7
,
'mark_data'
:
7
,
'target'
:
8
'target'
:
8
,
}
}
#trn_reader = paddle.reader.batched(
# paddle.reader.shuffle(
# train_reader(), buf_size=8192), batch_size=2)
trn_reader
=
paddle
.
reader
.
batched
(
train_reader
(),
batch_size
=
1
)
trainer
.
train
(
trainer
.
train
(
train_data_reader
=
train_reader
,
reader
=
trn_reader
,
batch_size
=
32
,
cost
=
crf_cost
,
topology
=
topo
,
parameters
=
parameters
,
parameters
=
parameters
,
event_handler
=
event_handler
,
event_handler
=
event_handler
,
num_passes
=
10000
,
num_passes
=
10000
,
reader_dict
=
reader_dict
)
reader_dict
=
reader_dict
)
#cost=[crf_cost, crf_dec],
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
demo/semantic_role_labeling/model_v2.py
浏览文件 @
e6e8bfb4
...
@@ -23,23 +23,25 @@ def db_lstm(word_dict_len, label_dict_len, pred_len):
...
@@ -23,23 +23,25 @@ def db_lstm(word_dict_len, label_dict_len, pred_len):
ctx_p2
=
paddle
.
layer
.
data
(
name
=
'ctx_p2_data'
,
type
=
d_type
(
word_dict_len
))
ctx_p2
=
paddle
.
layer
.
data
(
name
=
'ctx_p2_data'
,
type
=
d_type
(
word_dict_len
))
mark
=
paddle
.
layer
.
data
(
name
=
'mark_data'
,
type
=
d_type
(
mark_dict_len
))
mark
=
paddle
.
layer
.
data
(
name
=
'mark_data'
,
type
=
d_type
(
mark_dict_len
))
target
=
paddle
.
layer
.
data
(
name
=
'target'
,
type
=
d_type
(
label_dict_len
))
default_std
=
1
/
math
.
sqrt
(
hidden_dim
)
/
3.0
default_std
=
1
/
math
.
sqrt
(
hidden_dim
)
/
3.0
emb_para
=
paddle
.
attr
.
Param
(
name
=
'emb'
,
initial_std
=
0.
,
learning_rate
=
0.
)
emb_para
=
paddle
.
attr
.
Param
(
name
=
'emb'
,
initial_std
=
0.
,
learning_rate
=
0.
)
std_0
=
paddle
.
attr
.
Param
(
initial_std
=
0.
)
std_0
=
paddle
.
attr
.
Param
(
initial_std
=
0.
)
std_default
=
paddle
.
attr
.
Param
(
initial_std
=
default_std
)
std_default
=
paddle
.
attr
.
Param
(
initial_std
=
default_std
)
predicate_embedding
=
paddle
.
layer
.
embeding
(
predicate_embedding
=
paddle
.
layer
.
embed
d
ing
(
size
=
word_dim
,
size
=
word_dim
,
input
=
predicate
,
input
=
predicate
,
param_attr
=
paddle
.
attr
.
Param
(
param_attr
=
paddle
.
attr
.
Param
(
name
=
'vemb'
,
initial_std
=
default_std
))
name
=
'vemb'
,
initial_std
=
default_std
))
mark_embedding
=
paddle
.
layer
.
embeding
(
mark_embedding
=
paddle
.
layer
.
embed
d
ing
(
size
=
mark_dim
,
input
=
mark
,
param_attr
=
std_0
)
size
=
mark_dim
,
input
=
mark
,
param_attr
=
std_0
)
word_input
=
[
word
,
ctx_n2
,
ctx_n1
,
ctx_0
,
ctx_p1
,
ctx_p2
]
word_input
=
[
word
,
ctx_n2
,
ctx_n1
,
ctx_0
,
ctx_p1
,
ctx_p2
]
emb_layers
=
[
emb_layers
=
[
paddle
.
layer
.
embeding
(
paddle
.
layer
.
embed
d
ing
(
size
=
word_dim
,
input
=
x
,
param_attr
=
emb_para
)
for
x
in
word_input
size
=
word_dim
,
input
=
x
,
param_attr
=
emb_para
)
for
x
in
word_input
]
]
emb_layers
.
append
(
predicate_embedding
)
emb_layers
.
append
(
predicate_embedding
)
...
@@ -101,4 +103,19 @@ def db_lstm(word_dict_len, label_dict_len, pred_len):
...
@@ -101,4 +103,19 @@ def db_lstm(word_dict_len, label_dict_len, pred_len):
input
=
input_tmp
[
1
],
param_attr
=
lstm_para_attr
)
input
=
input_tmp
[
1
],
param_attr
=
lstm_para_attr
)
],
)
],
)
return
feature_out
crf_cost
=
paddle
.
layer
.
crf
(
size
=
label_dict_len
,
input
=
feature_out
,
label
=
target
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
'crfw'
,
initial_std
=
default_std
,
learning_rate
=
mix_hidden_lr
))
crf_dec
=
paddle
.
layer
.
crf_decoding
(
name
=
'crf_dec_l'
,
size
=
label_dict_len
,
input
=
feature_out
,
label
=
target
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
'crfw'
))
return
crf_cost
,
crf_dec
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录