Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
eee2094d
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看板
提交
eee2094d
编写于
6月 19, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine lm.
上级
06f272ab
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
18 addition
and
33 deletion
+18
-33
language_model/network_conf.py
language_model/network_conf.py
+18
-33
未找到文件。
language_model/network_conf.py
浏览文件 @
eee2094d
...
...
@@ -51,56 +51,41 @@ def rnn_lm(vocab_size, emb_dim, rnn_type, hidden_size, num_layer):
return
cost
,
output
def
ngram_lm
(
vocab_size
,
emb_dim
,
hidden_size
,
num_layer
):
def
ngram_lm
(
vocab_size
,
emb_dim
,
hidden_size
,
num_layer
,
gram_num
=
4
):
"""
N-Gram language model definition.
:param vocab_size: size of vocab.
:param emb_dim: embedding vector's dimension.
:param hidden_size: size of unit.
:param num_layer: layer number.
:param num_layer: number of hidden layers.
:param gram_size: gram number in n-gram method
:return: cost and output layer of model.
"""
assert
emb_dim
>
0
and
hidden_size
>
0
and
vocab_size
>
0
and
num_layer
>
0
def
wordemb
(
inlayer
):
wordemb
=
paddle
.
layer
.
table_projection
(
input
=
inlayer
,
size
=
emb_dim
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
"_proj"
,
initial_std
=
0.001
,
learning_rate
=
1
,
l2_rate
=
0
))
return
wordemb
# input layers
first_word
=
paddle
.
layer
.
data
(
name
=
"first_word"
,
type
=
paddle
.
data_type
.
integer_value
(
vocab_size
))
second_word
=
paddle
.
layer
.
data
(
name
=
"second_word"
,
type
=
paddle
.
data_type
.
integer_value
(
vocab_size
))
third_word
=
paddle
.
layer
.
data
(
name
=
"third_word"
,
type
=
paddle
.
data_type
.
integer_value
(
vocab_size
))
fourth_word
=
paddle
.
layer
.
data
(
name
=
"fourth_word"
,
type
=
paddle
.
data_type
.
integer_value
(
vocab_size
))
emb_layers
=
[]
for
i
in
range
(
gram_num
):
word
=
paddle
.
layer
.
data
(
name
=
"__word%02d__"
%
(
i
+
1
),
type
=
paddle
.
data_type
.
integer_value
(
vocab_size
))
emb
=
paddle
.
layer
.
embedding
(
input
=
word
,
size
=
emb_dim
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
"_proj"
,
initial_std
=
1e-3
))
emb_layers
.
append
(
emb
)
next_word
=
paddle
.
layer
.
data
(
name
=
"next_word"
,
type
=
paddle
.
data_type
.
integer_value
(
vocab_size
))
# embedding layer
first_emb
=
wordemb
(
first_word
)
second_emb
=
wordemb
(
second_word
)
third_emb
=
wordemb
(
third_word
)
fourth_emb
=
wordemb
(
fourth_word
)
context_emb
=
paddle
.
layer
.
concat
(
input
=
[
first_emb
,
second_emb
,
third_emb
,
fourth_emb
])
name
=
"__next_word__"
,
type
=
paddle
.
data_type
.
integer_value
(
vocab_size
))
# hidden layer
for
i
in
range
(
num_layer
):
hidden
=
paddle
.
layer
.
fc
(
input
=
context_emb
,
size
=
hidden_size
,
act
=
paddle
.
activation
.
Relu
())
for
_
in
range
(
num_layer
-
1
):
hidden
=
paddle
.
layer
.
fc
(
input
=
hidden
,
size
=
hidden_size
,
act
=
paddle
.
activation
.
Relu
())
input
=
hidden
if
i
else
paddle
.
layer
.
concat
(
input
=
emb_layers
),
size
=
hidden_size
,
act
=
paddle
.
activation
.
Relu
())
# fc(full connected) and output layer
predict_word
=
paddle
.
layer
.
fc
(
input
=
[
hidden
],
size
=
vocab_size
,
act
=
paddle
.
activation
.
Softmax
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录