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
47c31021
Commit
47c31021
authored
Jun 22, 2019
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fill the heap with incomplete hypotheses
parent
f34f70ed
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
58 行增加
和
10 行删除
+58
-10
source/sample/transformer/T2TSearch.cpp
+44
-9
source/sample/transformer/T2TSearch.h
+3
-0
source/sample/transformer/T2TTester.cpp
+11
-1
没有找到文件。
source/sample/transformer/T2TSearch.cpp
查看文件 @
47c31021
...
...
@@ -124,10 +124,13 @@ void T2TSearch::Search(T2TModel * model, XTensor * input, XTensor * padding, XTe
/* push complete hypotheses into the heap */
Collect
(
next
);
}
delete
[]
states
;
/* fill the heap with imcomplete hypotheses if neccesary */
FillHeap
(
&
states
[
maxLength
]);
Dump
(
output
);
delete
[]
states
;
}
/*
...
...
@@ -333,14 +336,21 @@ void T2TSearch::Expand(T2TStateBundle * prev, T2TStateBundle * beam)
it needs much more coding work and the speed-up is not obvious. */
for
(
int
i
=
0
;
i
<
beam
->
stateNum
;
i
++
){
T2TState
&
state
=
states
[
i
];
int
offset
=
id
.
GetInt
(
i
);
T2TState
*
last
=
prev
->
states
+
offset
;
CheckNTErrors
(
offset
>=
0
,
"Wrong state index!"
);
/* pointer to the previous state */
if
(
prev
->
isStart
)
state
.
last
=
NULL
;
if
(
prev
->
isStart
)
{
state
.
last
=
NULL
;
state
.
pid
=
offset
;
}
else
{
int
offset
=
id
.
GetInt
(
i
)
;
state
.
last
=
prev
->
states
+
offset
;
CheckNTErrors
(
offset
>=
0
&&
offset
<
prev
->
stateNum
,
"Wrong state index!"
);
state
.
last
=
last
;
state
.
pid
=
state
.
last
->
pid
;
CheckNTErrors
(
offset
<
prev
->
stateNum
,
"Wrong state index!"
);
}
/* scores */
...
...
@@ -376,7 +386,6 @@ void T2TSearch::Collect(T2TStateBundle * beam)
for
(
int
i
=
0
;
i
<
beam
->
stateNum
;
i
++
)
{
T2TState
&
state
=
states
[
i
];
state
.
pid
=
state
.
last
->
pid
;
CheckNTErrors
(
state
.
pid
>=
0
&&
state
.
pid
<
batchSize
,
"Invalid sample id!"
);
...
...
@@ -387,6 +396,32 @@ void T2TSearch::Collect(T2TStateBundle * beam)
}
/*
fill the hypotheis heap with incomplete hypothses
>> beam - the beam that keeps a number of states (final)
*/
void
T2TSearch
::
FillHeap
(
T2TStateBundle
*
beam
)
{
bool
*
emptyFlags
=
new
bool
[
batchSize
];
for
(
int
i
=
0
;
i
<
batchSize
;
i
++
)
emptyFlags
[
i
]
=
(
fullHypos
[
i
].
Count
()
==
0
);
T2TState
*
states
=
beam
->
states
;
for
(
int
i
=
0
;
i
<
beam
->
stateNum
;
i
++
)
{
T2TState
&
state
=
states
[
i
];
CheckNTErrors
(
state
.
pid
>=
0
&&
state
.
pid
<
batchSize
,
"Invalid sample id!"
);
/* we push the imcomplete hypothesis into the heap */
if
(
emptyFlags
[
state
.
pid
]
&&
state
.
isEnd
==
0
)
fullHypos
[
state
.
pid
].
Push
(
HeapNode
<
float
>
(
&
state
,
state
.
modelScore
));
}
delete
[]
emptyFlags
;
}
/*
save the output sequences in a tensor
>> output - output sequences (for return)
*/
...
...
@@ -404,7 +439,7 @@ void T2TSearch::Dump(XTensor * output)
XHeap
<
MIN_HEAP
,
float
>
&
heap
=
fullHypos
[
h
];
/* for each output in the beam */
for
(
int
i
=
0
;
i
<
beamSize
;
i
++
){
for
(
int
i
=
0
;
i
<
beamSize
&&
heap
.
Count
()
>
0
;
i
++
){
T2TState
*
state
=
(
T2TState
*
)
heap
.
Pop
().
index
;
int
count
=
0
;
...
...
source/sample/transformer/T2TSearch.h
查看文件 @
47c31021
...
...
@@ -87,6 +87,9 @@ public:
/* collect hypotheses with ending symbol */
void
Collect
(
T2TStateBundle
*
beam
);
/* fill the hypotheis heap with incomplete hypothses */
void
FillHeap
(
T2TStateBundle
*
beam
);
/* save the output sequences in a tensor */
void
Dump
(
XTensor
*
output
);
...
...
source/sample/transformer/T2TTester.cpp
查看文件 @
47c31021
...
...
@@ -65,6 +65,7 @@ void T2TTester::Test(const char * fn, const char * ofn, T2TModel * model)
int
wordCount
=
0
;
int
wordCountTotal
=
0
;
int
sentCount
=
0
;
int
batchCount
=
0
;
float
loss
=
0
;
/* data files */
...
...
@@ -118,9 +119,18 @@ void T2TTester::Test(const char * fn, const char * ofn, T2TModel * model)
float
prob
=
0
;
loss
+=
-
prob
;
wc
=
batchEnc
.
GetDim
(
-
1
);
wordCount
+=
wc
;
wordCountTotal
+=
wc
;
sentCount
+=
1
;
sentCount
+=
batchEnc
.
GetDim
(
-
2
);
batchCount
+=
1
;
if
(
batchCount
%
1
==
0
)
{
double
elapsed
=
GetClockSec
()
-
startT
;
XPRINT3
(
0
,
stderr
,
"[INFO] elapsed=%.1fs, sentence=%d, sword=%d
\n
"
,
elapsed
,
sentCount
,
wordCount
);
}
}
fclose
(
file
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论