Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
8
Issues
8
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
NiuTrans
NiuTrans.Tensor
Commits
000a990c
Commit
000a990c
authored
Apr 23, 2019
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add end symbols
parent
03fed61d
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
71 行增加
和
2 行删除
+71
-2
source/sample/transformer/T2TPredictor.cpp
+1
-0
source/sample/transformer/T2TPredictor.h
+3
-0
source/sample/transformer/T2TSearch.cpp
+51
-1
source/sample/transformer/T2TSearch.h
+16
-1
没有找到文件。
source/sample/transformer/T2TPredictor.cpp
查看文件 @
000a990c
...
...
@@ -56,6 +56,7 @@ void T2TStateBundle::MakeStates(int num)
for
(
int
i
=
0
;
i
<
num
;
i
++
){
states
[
i
].
prediction
=
-
1
;
states
[
i
].
pid
=
T2T_PID_EMPTY
;
states
[
i
].
isEnd
=
0
;
states
[
i
].
prob
=
0
;
states
[
i
].
probPath
=
0
;
states
[
i
].
modelScore
=
0
;
...
...
source/sample/transformer/T2TPredictor.h
查看文件 @
000a990c
...
...
@@ -44,6 +44,9 @@ public:
an empty hypothesis if id = -1 */
int
pid
;
/* indicates whether the state is an end */
int
isEnd
;
/* probability of every prediction (last state of the path) */
float
prob
;
...
...
source/sample/transformer/T2TSearch.cpp
查看文件 @
000a990c
...
...
@@ -32,6 +32,7 @@ namespace transformer
T2TSearch
::
T2TSearch
()
{
fullHypos
=
NULL
;
endSymbols
=
NULL
;
}
/* de-constructor */
...
...
@@ -39,6 +40,8 @@ T2TSearch::~T2TSearch()
{
if
(
fullHypos
!=
NULL
)
delete
[]
fullHypos
;
if
(
endSymbols
!=
NULL
)
delete
[]
endSymbols
;
}
/*
...
...
@@ -104,8 +107,11 @@ prepare for search
>> batchSize - size of the batch
>> beamSize - size of the beam
*/
void
T2TSearch
::
Prepare
(
int
batchSize
,
int
b
eamSize
)
void
T2TSearch
::
Prepare
(
int
myBatchSize
,
int
myB
eamSize
)
{
batchSize
=
myBatchSize
;
beamSize
=
myBeamSize
;
if
(
fullHypos
!=
NULL
)
delete
[]
fullHypos
;
...
...
@@ -279,6 +285,14 @@ void T2TSearch::Collect(T2TStateBundle * beam)
T2TState
&
state
=
states
[
i
];
state
.
pid
=
state
.
last
->
pid
;
CheckNTErrors
(
state
.
pid
>=
0
&&
state
.
pid
<
batchSize
,
"Invalid sample id!"
);
if
(
IsEnd
(
state
.
prediction
)){
state
.
isEnd
=
1
;
}
else
state
.
isEnd
=
0
;
}
}
...
...
@@ -290,4 +304,40 @@ void T2TSearch::DumpOutput(T2TStateBundle * beam, XTensor * output)
{
}
/*
check if the token is an end symbol
>> token - token to be checked
*/
bool
T2TSearch
::
IsEnd
(
int
token
)
{
CheckNTErrors
(
endSymbolNum
>
0
,
"No end symbol?"
);
for
(
int
i
=
0
;
i
<
endSymbolNum
;
i
++
){
if
(
endSymbols
[
i
]
==
token
)
return
true
;
}
return
false
;
}
/*
set end symbols for search
>> tokens - end symbols
>> tokenNum - number of the end symbols
*/
void
T2TSearch
::
SetEnd
(
const
int
*
tokens
,
const
int
tokenNum
)
{
if
(
endSymbols
!=
NULL
)
delete
[]
endSymbols
;
if
(
tokenNum
<=
0
)
return
;
/* we may have multiple end symbols */
tokens
=
new
int
[
tokenNum
];
for
(
int
i
=
0
;
i
<
tokenNum
;
i
++
)
endSymbols
[
i
]
=
tokens
[
i
];
endSymbolNum
=
tokenNum
;
}
}
source/sample/transformer/T2TSearch.h
查看文件 @
000a990c
...
...
@@ -47,9 +47,18 @@ private:
/* beam size */
int
beamSize
;
/* batch size */
int
batchSize
;
/* we keep the final hypotheses in a heap for each sentence in the batch. */
XHeap
<
MIN_HEAP
,
float
>
*
fullHypos
;
/* array of the end symbols */
int
*
endSymbols
;
/* number of the end symbols */
int
endSymbolNum
;
public
:
/* constructor */
T2TSearch
();
...
...
@@ -64,7 +73,7 @@ public:
void
Search
(
T2TModel
*
model
,
XTensor
*
input
,
XTensor
*
padding
,
XTensor
*
output
);
/* preparation */
void
Prepare
(
int
batchSize
,
int
b
eamSize
);
void
Prepare
(
int
myBatchSize
,
int
myB
eamSize
);
/* compute the model score for each hypothesis */
void
Score
(
T2TStateBundle
*
prev
,
T2TStateBundle
*
beam
);
...
...
@@ -80,6 +89,12 @@ public:
/* save the output sequences in a tensor */
void
DumpOutput
(
T2TStateBundle
*
beam
,
XTensor
*
output
);
/* check if the token is an end symbol */
bool
IsEnd
(
int
token
);
/* set end symbols for search */
void
SetEnd
(
const
int
*
tokens
,
const
int
tokenNum
);
};
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论