Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
8a61d326
M
models
项目概览
PaddlePaddle
/
models
大约 2 年 前同步成功
通知
232
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看板
提交
8a61d326
编写于
6月 02, 2017
作者:
Z
zhaopu7
提交者:
GitHub
6月 02, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Delete generate_text.py
上级
4eb5346e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
0 addition
and
117 deletion
+0
-117
language_model/generate_text.py
language_model/generate_text.py
+0
-117
未找到文件。
language_model/generate_text.py
已删除
100644 → 0
浏览文件 @
4eb5346e
# coding=utf-8
import
paddle.v2
as
paddle
import
numpy
as
np
def
next_word
(
model_struct
,
model_params
,
word_id_dict
,
input
):
"""
Demo: generate the next word.
to show the simplest way using trained model to do prediction.
:param model_struct: model's structure, only the output layer will be used for prediction task.
:param model_params: parameters trained before.
:param word_id_dict: vocab.
:type word_id_dict: dictionary with content of '{word, id}', 'word' is string type , 'id' is int type.
:param input: input.
:type input: integer sequence.
:return: predict word.
"""
predictions
=
paddle
.
infer
(
output_layer
=
model_struct
,
parameters
=
model_params
,
input
=
input
,
field
=
[
'value'
])
id_word_dict
=
dict
([(
v
,
k
)
for
k
,
v
in
word_id_dict
.
items
()])
# dictionary with type {id : word}
predictions
[
-
1
][
word_id_dict
[
'<UNK>'
]]
=
-
1
# filter <UNK>
return
id_word_dict
[
np
.
argmax
(
predictions
[
-
1
])]
def
generate_with_greedy
(
model_struct
,
model_params
,
word_id_dict
,
text
,
num_words
):
"""
Demo: generate 'num_words' words using greedy algorithm.
:param model_struct: model's structure, only the output layer will be used for prediction task.
:param model_params: parameters trained before.
:param word_id_dict: vocab.
:type word_id_dict: dictionary with content of '{word, id}', 'word' is string type , 'id' is int type.
:param text: prefix text.
:type text: string.
:param num_words: the number of the words to generate.
:return: text with generated words.
"""
assert
num_words
>
0
# prepare dictionary
id_word_dict
=
dict
([(
v
,
k
)
for
k
,
v
in
word_id_dict
.
items
()])
# generate
for
_
in
range
(
num_words
):
text_ids
=
[[[
word_id_dict
.
get
(
w
,
word_id_dict
[
'<UNK>'
])
for
w
in
text
.
split
()]]]
print
(
'input:'
,
text
.
encode
(
'utf-8'
,
'replace'
),
text_ids
)
predictions
=
paddle
.
infer
(
output_layer
=
model_struct
,
parameters
=
model_params
,
input
=
text_ids
,
field
=
[
'value'
])
predictions
[
-
1
][
word_id_dict
[
'<UNK>'
]]
=
-
1
# filter <UNK>
text
+=
' '
+
id_word_dict
[
np
.
argmax
(
predictions
[
-
1
])]
return
text
def
generate_with_beamSearch
(
model_struct
,
model_params
,
word_id_dict
,
text
,
num_words
,
beam_size
):
"""
Demo: generate 'num_words' words using "beam search" algorithm.
:param model_struct: model's structure, only the output layer will be used for prediction task.
:param model_params: parameters trained before.
:param word_id_dict: vocab.
:type word_id_dict: dictionary with content of '{word, id}', 'word' is string type , 'id' is int type.
:param text: prefix text.
:type text: string.
:param num_words: the number of the words to generate.
:param beam_size: beam with.
:return: text with generated words.
"""
assert
beam_size
>
0
and
num_words
>
0
# load word dictionary
id_word_dict
=
dict
([(
v
,
k
)
for
k
,
v
in
word_id_dict
.
items
()])
# {id : word}
# tools
def
str2ids
(
str
):
return
[[[
word_id_dict
.
get
(
w
,
word_id_dict
[
'<UNK>'
])
for
w
in
str
.
split
()]]]
def
ids2str
(
ids
):
return
[[[
id_word_dict
.
get
(
id
,
' '
)
for
id
in
ids
]]]
# generate
texts
=
{}
# type: {text : prob}
texts
[
text
]
=
1
for
_
in
range
(
num_words
):
texts_new
=
{}
for
(
text
,
prob
)
in
texts
.
items
():
# next word's prob distubution
predictions
=
paddle
.
infer
(
output_layer
=
model_struct
,
parameters
=
model_params
,
input
=
str2ids
(
text
),
field
=
[
'value'
])
predictions
[
-
1
][
word_id_dict
[
'<UNK>'
]]
=
-
1
# filter <UNK>
# find next beam_size words
for
_
in
range
(
beam_size
):
cur_maxProb_index
=
np
.
argmax
(
predictions
[
-
1
])
# next word's id
text_new
=
text
+
' '
+
id_word_dict
[
cur_maxProb_index
]
# text append nextWord
texts_new
[
text_new
]
=
texts
[
text
]
*
predictions
[
-
1
][
cur_maxProb_index
]
predictions
[
-
1
][
cur_maxProb_index
]
=
-
1
texts
.
clear
()
if
len
(
texts_new
)
<=
beam_size
:
texts
=
texts_new
else
:
# cutting
texts
=
dict
(
sorted
(
texts_new
.
items
(),
key
=
lambda
d
:
d
[
1
],
reverse
=
True
)[:
beam_size
])
return
texts
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录