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
杨迪
NiuTrans.Tensor
Commits
b7026578
Commit
b7026578
authored
May 09, 2019
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
decoding code
parent
da1d7ca8
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
101 行增加
和
2 行删除
+101
-2
source/sample/transformer/T2TSearch.cpp
+3
-1
source/sample/transformer/T2TSearch.h
+1
-1
source/sample/transformer/T2TTester.cpp
+83
-0
source/sample/transformer/T2TTester.h
+14
-0
没有找到文件。
source/sample/transformer/T2TSearch.cpp
查看文件 @
b7026578
...
...
@@ -49,7 +49,7 @@ initialize the model
>> argc - number of arguments
>> argv - list of pointers to the arguments
*/
void
T2TSearch
::
Init
Model
(
int
argc
,
char
**
argv
)
void
T2TSearch
::
Init
(
int
argc
,
char
**
argv
)
{
LoadParamInt
(
argc
,
argv
,
"beamsize"
,
&
beamSize
,
1
);
LoadParamFloat
(
argc
,
argv
,
"lenalpha"
,
&
alpha
,
0.2
F
);
...
...
@@ -100,6 +100,8 @@ void T2TSearch::Search(T2TModel * model, XTensor * input, XTensor * padding, XTe
}
delete
[]
states
;
Dump
(
output
);
}
/*
...
...
source/sample/transformer/T2TSearch.h
查看文件 @
b7026578
...
...
@@ -67,7 +67,7 @@ public:
~
T2TSearch
();
/* initialize the model */
void
Init
Model
(
int
argc
,
char
**
argv
);
void
Init
(
int
argc
,
char
**
argv
);
/* search for the most promising states */
void
Search
(
T2TModel
*
model
,
XTensor
*
input
,
XTensor
*
padding
,
XTensor
*
output
);
...
...
source/sample/transformer/T2TTester.cpp
查看文件 @
b7026578
...
...
@@ -19,7 +19,12 @@
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2019-03-27
*/
#include "T2TUtility.h"
#include "T2TTester.h"
#include "T2TSearch.h"
#include "../../tensor/XUtility.h"
#include "../../tensor/core/CHeader.h"
#include "../../network/XNoder.h"
using
namespace
nts
;
...
...
@@ -39,6 +44,11 @@ T2TTester::~T2TTester()
/* initialize the model */
void
T2TTester
::
InitModel
(
int
argc
,
char
**
argv
)
{
LoadParamInt
(
argc
,
argv
,
"vsize"
,
&
vSize
,
1
);
LoadParamInt
(
argc
,
argv
,
"vsizetgt"
,
&
vSizeTgt
,
vSize
);
batchLoader
.
Init
(
argc
,
argv
);
seacher
.
Init
(
argc
,
argv
);
}
/*
...
...
@@ -49,6 +59,78 @@ test the model
*/
void
T2TTester
::
Test
(
const
char
*
fn
,
const
char
*
ofn
,
T2TModel
*
model
)
{
int
wc
=
0
;
int
ws
=
0
;
int
wordCount
=
0
;
int
wordCountTotal
=
0
;
int
sentCount
=
0
;
float
loss
=
0
;
/* data files */
FILE
*
file
=
fopen
(
fn
,
"rb"
);
CheckNTErrors
(
file
,
"Cannot read the test file"
);
FILE
*
ofile
=
fopen
(
ofn
,
"wb"
);
CheckNTErrors
(
ofile
,
"Cannot open the output file"
);
int
devID
=
model
->
devID
;
XMem
*
mem
=
model
->
mem
;
XNet
net
;
double
startT
=
GetClockSec
();
wordCount
=
0
;
/* batch of input sequences */
XTensor
batchEnc
;
XTensor
batchDec
;
/* label */
XTensor
label
;
/* padding */
XTensor
paddingEnc
;
XTensor
paddingDec
;
/* gold standard */
XTensor
gold
;
/* an array that keeps the sequences */
int
*
seqs
=
new
int
[
MILLION
];
batchLoader
.
ClearBuf
();
while
(
batchLoader
.
LoadBatch
(
file
,
model
->
isLM
,
&
batchEnc
,
&
paddingEnc
,
&
paddingDec
,
&
paddingDec
,
&
gold
,
&
label
,
seqs
,
vSize
,
vSizeTgt
,
1
,
1
,
false
,
ws
,
wc
,
devID
,
mem
,
false
))
{
CheckNTErrors
(
batchEnc
.
order
==
2
,
"wrong tensor order of the sequence batch!"
);
CheckNTErrors
(
!
model
->
isLM
,
"Only MT model is supported!"
);
XTensor
output
;
seacher
.
Search
(
model
,
&
batchEnc
,
&
paddingEnc
,
&
output
);
output
.
Dump
(
ofile
,
"output:"
);
float
prob
=
0
;
loss
+=
-
prob
;
wordCount
+=
wc
;
wordCountTotal
+=
wc
;
sentCount
+=
1
;
}
fclose
(
file
);
fclose
(
ofile
);
delete
[]
seqs
;
double
elapsed
=
GetClockSec
()
-
startT
;
XPRINT3
(
0
,
stderr
,
"[INFO] test finished (took %.1fs, word=%d, and ppl=%.3f)
\n
"
,
elapsed
,
wordCountTotal
,
exp
(
loss
/
wordCount
));
}
}
\ No newline at end of file
source/sample/transformer/T2TTester.h
查看文件 @
b7026578
...
...
@@ -24,6 +24,7 @@
#define __T2TTESTER_H__
#include "T2TSearch.h"
#include "T2TBatchLoader.h"
namespace
transformer
{
...
...
@@ -32,6 +33,19 @@ namespace transformer
class
T2TTester
{
public
:
/* vocabulary size of the source side */
int
vSize
;
/* vocabulary size of the target side */
int
vSizeTgt
;
/* for batching */
T2TBatchLoader
batchLoader
;
/* decoder for inference */
T2TSearch
seacher
;
public
:
/* constructor */
T2TTester
();
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论