Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
ef10dd94
B
book
项目概览
PaddlePaddle
/
book
通知
17
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ef10dd94
编写于
6月 28, 2018
作者:
N
Nicky Chan
提交者:
GitHub
6月 28, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #559 from nickyfantasy/updateBeamSearch
update beam search API in machine translation book example
上级
3f0b8ece
801e400d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
85 addition
and
24 deletion
+85
-24
08.machine_translation/README.cn.md
08.machine_translation/README.cn.md
+17
-4
08.machine_translation/README.md
08.machine_translation/README.md
+17
-4
08.machine_translation/index.cn.html
08.machine_translation/index.cn.html
+17
-6
08.machine_translation/index.html
08.machine_translation/index.html
+17
-6
08.machine_translation/infer.py
08.machine_translation/infer.py
+17
-4
未找到文件。
08.machine_translation/README.cn.md
浏览文件 @
ef10dd94
...
@@ -253,9 +253,18 @@ def decode(context, is_sparse):
...
@@ -253,9 +253,18 @@ def decode(context, is_sparse):
current_score
=
pd
.
fc
(
input
=
current_state_with_lod
,
current_score
=
pd
.
fc
(
input
=
current_state_with_lod
,
size
=
target_dict_dim
,
size
=
target_dict_dim
,
act
=
'softmax'
)
act
=
'softmax'
)
topk_scores
,
topk_indices
=
pd
.
topk
(
current_score
,
k
=
topk_size
)
topk_scores
,
topk_indices
=
pd
.
topk
(
current_score
,
k
=
beam_size
)
# calculate accumulated scores after topk to reduce computation cost
accu_scores
=
pd
.
elementwise_add
(
x
=
pd
.
log
(
topk_scores
),
y
=
pd
.
reshape
(
pre_score
,
shape
=
[
-
1
]),
axis
=
0
)
selected_ids
,
selected_scores
=
pd
.
beam_search
(
selected_ids
,
selected_scores
=
pd
.
beam_search
(
pre_ids
,
topk_indices
,
topk_scores
,
beam_size
,
end_id
=
10
,
level
=
0
)
pre_ids
,
pre_score
,
topk_indices
,
accu_scores
,
beam_size
,
end_id
=
10
,
level
=
0
)
pd
.
increment
(
x
=
counter
,
value
=
1
,
in_place
=
True
)
pd
.
increment
(
x
=
counter
,
value
=
1
,
in_place
=
True
)
...
@@ -264,10 +273,14 @@ def decode(context, is_sparse):
...
@@ -264,10 +273,14 @@ def decode(context, is_sparse):
pd
.
array_write
(
selected_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
selected_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
selected_scores
,
array
=
scores_array
,
i
=
counter
)
pd
.
array_write
(
selected_scores
,
array
=
scores_array
,
i
=
counter
)
pd
.
less_than
(
x
=
counter
,
y
=
array_len
,
cond
=
cond
)
# update the break condition: up to the max length or all candidates of
# source sentences have ended.
length_cond
=
pd
.
less_than
(
x
=
counter
,
y
=
array_len
)
finish_cond
=
pd
.
logical_not
(
pd
.
is_empty
(
x
=
selected_ids
))
pd
.
logical_and
(
x
=
length_cond
,
y
=
finish_cond
,
out
=
cond
)
translation_ids
,
translation_scores
=
pd
.
beam_search_decode
(
translation_ids
,
translation_scores
=
pd
.
beam_search_decode
(
ids
=
ids_array
,
scores
=
scores_array
)
ids
=
ids_array
,
scores
=
scores_array
,
beam_size
=
beam_size
,
end_id
=
10
)
return
translation_ids
,
translation_scores
return
translation_ids
,
translation_scores
```
```
...
...
08.machine_translation/README.md
浏览文件 @
ef10dd94
...
@@ -290,9 +290,18 @@ def decode(context, is_sparse):
...
@@ -290,9 +290,18 @@ def decode(context, is_sparse):
current_score
=
pd
.
fc
(
input
=
current_state_with_lod
,
current_score
=
pd
.
fc
(
input
=
current_state_with_lod
,
size
=
target_dict_dim
,
size
=
target_dict_dim
,
act
=
'softmax'
)
act
=
'softmax'
)
topk_scores
,
topk_indices
=
pd
.
topk
(
current_score
,
k
=
topk_size
)
topk_scores
,
topk_indices
=
pd
.
topk
(
current_score
,
k
=
beam_size
)
# calculate accumulated scores after topk to reduce computation cost
accu_scores
=
pd
.
elementwise_add
(
x
=
pd
.
log
(
topk_scores
),
y
=
pd
.
reshape
(
pre_score
,
shape
=
[
-
1
]),
axis
=
0
)
selected_ids
,
selected_scores
=
pd
.
beam_search
(
selected_ids
,
selected_scores
=
pd
.
beam_search
(
pre_ids
,
topk_indices
,
topk_scores
,
beam_size
,
end_id
=
10
,
level
=
0
)
pre_ids
,
pre_score
,
topk_indices
,
accu_scores
,
beam_size
,
end_id
=
10
,
level
=
0
)
pd
.
increment
(
x
=
counter
,
value
=
1
,
in_place
=
True
)
pd
.
increment
(
x
=
counter
,
value
=
1
,
in_place
=
True
)
...
@@ -301,10 +310,14 @@ def decode(context, is_sparse):
...
@@ -301,10 +310,14 @@ def decode(context, is_sparse):
pd
.
array_write
(
selected_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
selected_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
selected_scores
,
array
=
scores_array
,
i
=
counter
)
pd
.
array_write
(
selected_scores
,
array
=
scores_array
,
i
=
counter
)
pd
.
less_than
(
x
=
counter
,
y
=
array_len
,
cond
=
cond
)
# update the break condition: up to the max length or all candidates of
# source sentences have ended.
length_cond
=
pd
.
less_than
(
x
=
counter
,
y
=
array_len
)
finish_cond
=
pd
.
logical_not
(
pd
.
is_empty
(
x
=
selected_ids
))
pd
.
logical_and
(
x
=
length_cond
,
y
=
finish_cond
,
out
=
cond
)
translation_ids
,
translation_scores
=
pd
.
beam_search_decode
(
translation_ids
,
translation_scores
=
pd
.
beam_search_decode
(
ids
=
ids_array
,
scores
=
scores_array
)
ids
=
ids_array
,
scores
=
scores_array
,
beam_size
=
beam_size
,
end_id
=
10
)
return
translation_ids
,
translation_scores
return
translation_ids
,
translation_scores
```
```
...
...
08.machine_translation/index.cn.html
浏览文件 @
ef10dd94
...
@@ -201,7 +201,6 @@ decoder_size = hidden_dim
...
@@ -201,7 +201,6 @@ decoder_size = hidden_dim
```python
```python
def encoder(is_sparse):
def encoder(is_sparse):
# encoder
src_word_id = pd.data(
src_word_id = pd.data(
name="src_word_id", shape=[1], dtype='int64', lod_level=1)
name="src_word_id", shape=[1], dtype='int64', lod_level=1)
src_embedding = pd.embedding(
src_embedding = pd.embedding(
...
@@ -221,7 +220,6 @@ decoder_size = hidden_dim
...
@@ -221,7 +220,6 @@ decoder_size = hidden_dim
```python
```python
def train_decoder(context, is_sparse):
def train_decoder(context, is_sparse):
# decoder
trg_language_word = pd.data(
trg_language_word = pd.data(
name="target_language_word", shape=[1], dtype='int64', lod_level=1)
name="target_language_word", shape=[1], dtype='int64', lod_level=1)
trg_embedding = pd.embedding(
trg_embedding = pd.embedding(
...
@@ -297,9 +295,18 @@ def decode(context, is_sparse):
...
@@ -297,9 +295,18 @@ def decode(context, is_sparse):
current_score = pd.fc(input=current_state_with_lod,
current_score = pd.fc(input=current_state_with_lod,
size=target_dict_dim,
size=target_dict_dim,
act='softmax')
act='softmax')
topk_scores, topk_indices = pd.topk(current_score, k=topk_size)
topk_scores, topk_indices = pd.topk(current_score, k=beam_size)
# calculate accumulated scores after topk to reduce computation cost
accu_scores = pd.elementwise_add(
x=pd.log(topk_scores), y=pd.reshape(pre_score, shape=[-1]), axis=0)
selected_ids, selected_scores = pd.beam_search(
selected_ids, selected_scores = pd.beam_search(
pre_ids, topk_indices, topk_scores, beam_size, end_id=10, level=0)
pre_ids,
pre_score,
topk_indices,
accu_scores,
beam_size,
end_id=10,
level=0)
pd.increment(x=counter, value=1, in_place=True)
pd.increment(x=counter, value=1, in_place=True)
...
@@ -308,10 +315,14 @@ def decode(context, is_sparse):
...
@@ -308,10 +315,14 @@ def decode(context, is_sparse):
pd.array_write(selected_ids, array=ids_array, i=counter)
pd.array_write(selected_ids, array=ids_array, i=counter)
pd.array_write(selected_scores, array=scores_array, i=counter)
pd.array_write(selected_scores, array=scores_array, i=counter)
pd.less_than(x=counter, y=array_len, cond=cond)
# update the break condition: up to the max length or all candidates of
# source sentences have ended.
length_cond = pd.less_than(x=counter, y=array_len)
finish_cond = pd.logical_not(pd.is_empty(x=selected_ids))
pd.logical_and(x=length_cond, y=finish_cond, out=cond)
translation_ids, translation_scores = pd.beam_search_decode(
translation_ids, translation_scores = pd.beam_search_decode(
ids=ids_array, scores=scores_array)
ids=ids_array, scores=scores_array
, beam_size=beam_size, end_id=10
)
return translation_ids, translation_scores
return translation_ids, translation_scores
```
```
...
...
08.machine_translation/index.html
浏览文件 @
ef10dd94
...
@@ -238,7 +238,6 @@ Then we implement encoder as follows:
...
@@ -238,7 +238,6 @@ Then we implement encoder as follows:
```python
```python
def encoder(is_sparse):
def encoder(is_sparse):
# encoder
src_word_id = pd.data(
src_word_id = pd.data(
name="src_word_id", shape=[1], dtype='int64', lod_level=1)
name="src_word_id", shape=[1], dtype='int64', lod_level=1)
src_embedding = pd.embedding(
src_embedding = pd.embedding(
...
@@ -258,7 +257,6 @@ Implement the decoder for training as follows:
...
@@ -258,7 +257,6 @@ Implement the decoder for training as follows:
```python
```python
def train_decoder(context, is_sparse):
def train_decoder(context, is_sparse):
# decoder
trg_language_word = pd.data(
trg_language_word = pd.data(
name="target_language_word", shape=[1], dtype='int64', lod_level=1)
name="target_language_word", shape=[1], dtype='int64', lod_level=1)
trg_embedding = pd.embedding(
trg_embedding = pd.embedding(
...
@@ -334,9 +332,18 @@ def decode(context, is_sparse):
...
@@ -334,9 +332,18 @@ def decode(context, is_sparse):
current_score = pd.fc(input=current_state_with_lod,
current_score = pd.fc(input=current_state_with_lod,
size=target_dict_dim,
size=target_dict_dim,
act='softmax')
act='softmax')
topk_scores, topk_indices = pd.topk(current_score, k=topk_size)
topk_scores, topk_indices = pd.topk(current_score, k=beam_size)
# calculate accumulated scores after topk to reduce computation cost
accu_scores = pd.elementwise_add(
x=pd.log(topk_scores), y=pd.reshape(pre_score, shape=[-1]), axis=0)
selected_ids, selected_scores = pd.beam_search(
selected_ids, selected_scores = pd.beam_search(
pre_ids, topk_indices, topk_scores, beam_size, end_id=10, level=0)
pre_ids,
pre_score,
topk_indices,
accu_scores,
beam_size,
end_id=10,
level=0)
pd.increment(x=counter, value=1, in_place=True)
pd.increment(x=counter, value=1, in_place=True)
...
@@ -345,10 +352,14 @@ def decode(context, is_sparse):
...
@@ -345,10 +352,14 @@ def decode(context, is_sparse):
pd.array_write(selected_ids, array=ids_array, i=counter)
pd.array_write(selected_ids, array=ids_array, i=counter)
pd.array_write(selected_scores, array=scores_array, i=counter)
pd.array_write(selected_scores, array=scores_array, i=counter)
pd.less_than(x=counter, y=array_len, cond=cond)
# update the break condition: up to the max length or all candidates of
# source sentences have ended.
length_cond = pd.less_than(x=counter, y=array_len)
finish_cond = pd.logical_not(pd.is_empty(x=selected_ids))
pd.logical_and(x=length_cond, y=finish_cond, out=cond)
translation_ids, translation_scores = pd.beam_search_decode(
translation_ids, translation_scores = pd.beam_search_decode(
ids=ids_array, scores=scores_array)
ids=ids_array, scores=scores_array
, beam_size=beam_size, end_id=10
)
return translation_ids, translation_scores
return translation_ids, translation_scores
```
```
...
...
08.machine_translation/infer.py
浏览文件 @
ef10dd94
...
@@ -97,9 +97,18 @@ def decode(context):
...
@@ -97,9 +97,18 @@ def decode(context):
# use score to do beam search
# use score to do beam search
current_score
=
pd
.
fc
(
current_score
=
pd
.
fc
(
input
=
current_state_with_lod
,
size
=
target_dict_dim
,
act
=
'softmax'
)
input
=
current_state_with_lod
,
size
=
target_dict_dim
,
act
=
'softmax'
)
topk_scores
,
topk_indices
=
pd
.
topk
(
current_score
,
k
=
topk_size
)
topk_scores
,
topk_indices
=
pd
.
topk
(
current_score
,
k
=
beam_size
)
# calculate accumulated scores after topk to reduce computation cost
accu_scores
=
pd
.
elementwise_add
(
x
=
pd
.
log
(
topk_scores
),
y
=
pd
.
reshape
(
pre_score
,
shape
=
[
-
1
]),
axis
=
0
)
selected_ids
,
selected_scores
=
pd
.
beam_search
(
selected_ids
,
selected_scores
=
pd
.
beam_search
(
pre_ids
,
topk_indices
,
topk_scores
,
beam_size
,
end_id
=
10
,
level
=
0
)
pre_ids
,
pre_score
,
topk_indices
,
accu_scores
,
beam_size
,
end_id
=
10
,
level
=
0
)
with
pd
.
Switch
()
as
switch
:
with
pd
.
Switch
()
as
switch
:
with
switch
.
case
(
pd
.
is_empty
(
selected_ids
)):
with
switch
.
case
(
pd
.
is_empty
(
selected_ids
)):
...
@@ -113,10 +122,14 @@ def decode(context):
...
@@ -113,10 +122,14 @@ def decode(context):
pd
.
array_write
(
selected_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
selected_ids
,
array
=
ids_array
,
i
=
counter
)
pd
.
array_write
(
selected_scores
,
array
=
scores_array
,
i
=
counter
)
pd
.
array_write
(
selected_scores
,
array
=
scores_array
,
i
=
counter
)
pd
.
less_than
(
x
=
counter
,
y
=
array_len
,
cond
=
cond
)
# update the break condition: up to the max length or all candidates of
# source sentences have ended.
length_cond
=
pd
.
less_than
(
x
=
counter
,
y
=
array_len
)
finish_cond
=
pd
.
logical_not
(
pd
.
is_empty
(
x
=
selected_ids
))
pd
.
logical_and
(
x
=
length_cond
,
y
=
finish_cond
,
out
=
cond
)
translation_ids
,
translation_scores
=
pd
.
beam_search_decode
(
translation_ids
,
translation_scores
=
pd
.
beam_search_decode
(
ids
=
ids_array
,
scores
=
scores_array
)
ids
=
ids_array
,
scores
=
scores_array
,
beam_size
=
beam_size
,
end_id
=
10
)
return
translation_ids
,
translation_scores
return
translation_ids
,
translation_scores
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录