Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
Emmay
NiuTrans.Tensor
Commits
df09abef
Commit
df09abef
authored
Apr 02, 2019
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generation via beam pruning
parent
8eae2dbf
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
29 行增加
和
12 行删除
+29
-12
source/sample/transformer/T2TSearch.cpp
+25
-8
source/sample/transformer/T2TSearch.h
+4
-4
没有找到文件。
source/sample/transformer/T2TSearch.cpp
查看文件 @
df09abef
...
...
@@ -75,35 +75,52 @@ void T2TSearch::Search(T2TModel * model, XTensor * input, XTensor * padding, XTe
/* predict the next state */
predictor
.
Predict
(
next
,
&
encoding
,
input
,
padding
);
/* pruning */
Prun
e
(
next
);
/*
beam
pruning */
Generat
e
(
next
);
}
delete
[]
states
;
}
/*
beam pruning
generate tokens for the next state via beam pruning
>> beam - the beam that keeps a number of states
*/
void
T2TSearch
::
Prun
e
(
T2TStateBundle
*
beam
)
void
T2TSearch
::
Generat
e
(
T2TStateBundle
*
beam
)
{
int
dims
[
MAX_TENSOR_DIM_NUM
];
int
dimsBeam
[
MAX_TENSOR_DIM_NUM
];
int
dimsTopK
[
MAX_TENSOR_DIM_NUM
];
XTensor
scoreTopK
;
XTensor
&
score
=
beam
->
score
;
XTensor
&
index
=
beam
->
prediction
;
int
order
=
score
.
order
;
for
(
int
i
=
0
;
i
<
score
.
order
;
i
++
)
CheckNTErrors
(
order
>=
2
,
"The tensor must be of order 2 or larger."
);
CheckNTErrors
(
dimsBeam
[
order
-
2
]
%
beamSize
==
0
,
"Wrong dimension size!"
);
for
(
int
i
=
0
;
i
<
order
;
i
++
)
{
dims
[
i
]
=
score
.
GetDim
(
i
);
dims
[
score
.
order
-
1
]
=
beamSize
;
dimsBeam
[
i
]
=
score
.
GetDim
(
i
);
dimsTopK
[
i
]
=
score
.
GetDim
(
i
);
}
dimsBeam
[
order
-
2
]
/=
beamSize
;
dimsBeam
[
order
-
1
]
*=
beamSize
;
dimsTopK
[
order
-
2
]
=
dimsBeam
[
order
-
2
];
dimsTopK
[
order
-
1
]
=
beamSize
;
InitTensor
(
&
scoreTopK
,
score
.
order
,
score
.
dimSize
,
score
.
dataType
,
InitTensor
(
&
scoreTopK
,
order
,
dimsTopK
,
score
.
dataType
,
1.0
F
,
score
.
devID
,
score
.
mem
);
InitTensor
(
&
index
,
score
.
order
,
score
.
dimSize
,
X_INT
,
InitTensor
(
&
index
,
order
,
dimsTopK
,
X_INT
,
1.0
F
,
score
.
devID
,
score
.
mem
);
score
.
Reshape
(
order
,
dimsBeam
);
TopK
(
score
,
scoreTopK
,
index
,
0
,
beamSize
);
score
.
Reshape
(
order
,
dims
);
}
/*
...
...
source/sample/transformer/T2TSearch.h
查看文件 @
df09abef
...
...
@@ -28,9 +28,9 @@
namespace
transformer
{
/* The class orgnizes the search process. It calls
“predictors”
to generate
/* The class orgnizes the search process. It calls
"predictors"
to generate
distributions of the predictions and prunes the search space by beam pruning.
It results in
a graph where each path respresents a translation hypothsis.
This makes
a graph where each path respresents a translation hypothsis.
The output can be the path with the highest model score. */
class
T2TSearch
{
...
...
@@ -57,8 +57,8 @@ public:
/* search for the most promising states */
void
Search
(
T2TModel
*
model
,
XTensor
*
input
,
XTensor
*
padding
,
XTensor
*
output
);
/* beam pruning */
void
Prun
e
(
T2TStateBundle
*
beam
);
/*
generate token indices via
beam pruning */
void
Generat
e
(
T2TStateBundle
*
beam
);
/* save the output sequences in a tensor */
void
DumpOutput
(
T2TStateBundle
*
beam
,
XTensor
*
output
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论