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
7b6840d4
Commit
7b6840d4
authored
Mar 20, 2021
by
huchi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor the dataloader in transformer
parent
3852f15a
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
64 行增加
和
43 行删除
+64
-43
source/sample/transformer/train/TrainDataSet.cpp
+0
-0
source/sample/transformer/train/TrainDataSet.h
+41
-22
source/sample/transformer/train/Trainer.cpp
+23
-21
没有找到文件。
source/sample/transformer/train/TrainDataSet.cpp
查看文件 @
7b6840d4
差异被折叠。
点击展开。
source/sample/transformer/train/TrainDataSet.h
查看文件 @
7b6840d4
...
@@ -83,13 +83,13 @@ public:
...
@@ -83,13 +83,13 @@ public:
FILE
*
fp
;
FILE
*
fp
;
/* number of training samples */
/* number of training samples */
size_
t
totalSampleNum
;
in
t
totalSampleNum
;
/* buffer size */
/* buffer size */
size_
t
bufferSize
;
in
t
bufferSize
;
/* size of the bucket used for grouping sentences */
/* size of the bucket used for grouping sentences */
size_
t
bucketSize
;
in
t
bucketSize
;
/* indicates whether it is used for training */
/* indicates whether it is used for training */
bool
isTraining
;
bool
isTraining
;
...
@@ -112,44 +112,63 @@ public:
...
@@ -112,44 +112,63 @@ public:
/* the maximum length for a target sentence */
/* the maximum length for a target sentence */
int
maxTgtLen
;
int
maxTgtLen
;
/* batch size (number of words) */
int
batchSize
;
/* word-counter */
int
wc
;
/* sentence-counter */
int
sc
;
/* current index of the buffer */
int
curIdx
;
/* the buffer (a list) of samples */
XList
*
buf
;
public
:
public
:
/* get the maximum source sentence length in a range */
/* get the maximum source sentence length in a range */
static
int
MaxSrcLen
(
int
begin
,
int
end
);
int
MaxSrcLen
(
XList
*
buf
,
int
begin
,
int
end
);
/* get the maximum target sentence length in a range */
/* get the maximum target sentence length in a range */
static
int
MaxTgtLen
(
int
begin
,
int
end
);
int
MaxTgtLen
(
XList
*
buf
,
int
begin
,
int
end
);
/* sort the input by source sentence length (in descending order) */
/* sort the input by source sentence length (in descending order) */
void
SortBySrcLength
(
XList
*
buf
);
void
SortBySrcLength
();
/* sort the input by target sentence length (in descending order) */
/* sort the input by target sentence length (in descending order) */
void
SortByTgtLength
(
XList
*
buf
);
void
SortByTgtLength
();
/* sort buckets by key (in descending order) */
/* sort buckets by key (in descending order) */
void
SortBuckets
(
XList
*
buf
);
void
SortBuckets
();
/* load the samples into the buffer (a list) */
/* load the samples into the buffer (a list) */
bool
LoadBatchToBuf
(
XList
*
buf
);
bool
LoadBatchToBuf
();
/* load the samples into tensors from the buffer */
static
bool
LoadBatch
(
XList
*
buf
,
int
&
curIdx
,
XTensor
*
batchEnc
,
XTensor
*
paddingEnc
,
XTensor
*
batchDec
,
XTensor
*
paddingDec
,
XTensor
*
label
,
int
minSentBatch
,
int
batchSize
,
int
devID
,
int
&
wc
,
int
&
sc
);
/* release the samples in a buffer */
/* release the samples in a buffer */
static
void
ClearSamples
();
void
ClearSamples
(
XList
*
buf
);
/* initialization function */
/* initialization function */
void
Init
(
const
char
*
dataFile
,
int
b
ucketSize
,
bool
training
);
void
Init
(
const
char
*
dataFile
,
int
myBatchSize
,
int
myB
ucketSize
,
bool
training
);
/* group data into buckets with similar length */
/* group data into buckets with similar length */
void
BuildBucket
(
XList
*
buf
);
void
BuildBucket
();
/* get the number of sentences in a mini-batch */
int
GetSentNum
();
public
:
/* start the process */
bool
Start
();
/* end the process */
bool
End
();
/* load the samples into tensors from the buffer */
bool
GetBatchSimple
(
XList
*
inputs
,
XList
*
golds
);
/* de-constructor */
/* de-constructor */
~
TrainDataSet
();
~
TrainDataSet
();
...
...
source/sample/transformer/train/Trainer.cpp
查看文件 @
7b6840d4
...
@@ -179,9 +179,8 @@ void Trainer::Train(const char* fn, const char* validFN,
...
@@ -179,9 +179,8 @@ void Trainer::Train(const char* fn, const char* validFN,
double
startT
=
GetClockSec
();
double
startT
=
GetClockSec
();
int
curIdx
=
0
;
int
curIdx
=
0
;
XList
*
buf
=
new
XList
;
batchLoader
.
Init
(
fn
,
bucketSize
,
true
);
batchLoader
.
Init
(
fn
,
wBatchSize
,
bucketSize
,
true
);
for
(
epoch
=
1
;
epoch
<=
nepoch
;
epoch
++
)
{
for
(
epoch
=
1
;
epoch
<=
nepoch
;
epoch
++
)
{
...
@@ -204,13 +203,16 @@ void Trainer::Train(const char* fn, const char* validFN,
...
@@ -204,13 +203,16 @@ void Trainer::Train(const char* fn, const char* validFN,
XTensor
paddingEnc
;
XTensor
paddingEnc
;
XTensor
paddingDec
;
XTensor
paddingDec
;
if
(
curIdx
==
0
||
curIdx
==
buf
->
Size
())
{
TensorList
inputs
;
curIdx
=
0
;
TensorList
golds
;
batchLoader
.
LoadBatchToBuf
(
buf
);
}
inputs
.
Add
(
&
batchEnc
);
inputs
.
Add
(
&
paddingEnc
);
golds
.
Add
(
&
batchDec
);
golds
.
Add
(
&
paddingDec
);
golds
.
Add
(
&
label
);
batchLoader
.
LoadBatch
(
buf
,
curIdx
,
&
batchEnc
,
&
paddingEnc
,
&
batchDec
,
&
paddingDec
,
&
label
,
batchLoader
.
GetBatchSimple
((
XList
*
)(
&
inputs
),
(
XList
*
)(
&
golds
));
sBatchSize
,
wBatchSize
,
devID
,
wc
,
sc
);
CheckNTErrors
(
batchEnc
.
order
==
2
,
"wrong tensor order of the sequence batch"
);
CheckNTErrors
(
batchEnc
.
order
==
2
,
"wrong tensor order of the sequence batch"
);
...
@@ -303,9 +305,6 @@ void Trainer::Train(const char* fn, const char* validFN,
...
@@ -303,9 +305,6 @@ void Trainer::Train(const char* fn, const char* validFN,
MakeCheckpoint
(
model
,
validFN
,
modelFN
,
"epoch"
,
epoch
);
MakeCheckpoint
(
model
,
validFN
,
modelFN
,
"epoch"
,
epoch
);
}
}
batchLoader
.
ClearSamples
(
buf
);
delete
buf
;
double
elapsed
=
GetClockSec
()
-
startT
;
double
elapsed
=
GetClockSec
()
-
startT
;
epoch
=
MIN
(
epoch
,
nepoch
);
epoch
=
MIN
(
epoch
,
nepoch
);
...
@@ -341,16 +340,15 @@ void Trainer::Validate(const char* fn, const char* ofn, Model* model)
...
@@ -341,16 +340,15 @@ void Trainer::Validate(const char* fn, const char* ofn, Model* model)
float
loss
=
0
;
float
loss
=
0
;
/* data files */
/* data files */
batchLoader
.
Init
(
fn
,
0
,
false
);
batchLoader
.
Init
(
fn
,
wBatchSize
,
0
,
false
);
int
curIdx
=
0
;
int
curIdx
=
0
;
XList
*
buf
=
new
XList
;
/* set the buffer size to the size of valiadation set */
/* set the buffer size to the size of valiadation set */
batchLoader
.
bufferSize
=
batchLoader
.
totalSampleNum
;
batchLoader
.
bufferSize
=
batchLoader
.
totalSampleNum
;
batchLoader
.
LoadBatchToBuf
(
buf
);
batchLoader
.
LoadBatchToBuf
();
while
(
curIdx
<
buf
->
count
)
while
(
curIdx
<
b
atchLoader
.
b
uf
->
count
)
{
{
/* batch of input sequences */
/* batch of input sequences */
XTensor
batchEnc
;
XTensor
batchEnc
;
...
@@ -370,8 +368,16 @@ void Trainer::Validate(const char* fn, const char* ofn, Model* model)
...
@@ -370,8 +368,16 @@ void Trainer::Validate(const char* fn, const char* ofn, Model* model)
XTensor
labelOnehot
;
XTensor
labelOnehot
;
XTensor
lossTensor
;
XTensor
lossTensor
;
batchLoader
.
LoadBatch
(
buf
,
curIdx
,
&
batchEnc
,
&
paddingEnc
,
&
batchDec
,
&
paddingDec
,
&
label
,
TensorList
inputs
;
sBatchSize
,
0
,
model
->
devID
,
wc
,
sc
);
TensorList
golds
;
inputs
.
Add
(
&
batchEnc
);
inputs
.
Add
(
&
paddingEnc
);
golds
.
Add
(
&
batchDec
);
golds
.
Add
(
&
paddingDec
);
golds
.
Add
(
&
label
);
batchLoader
.
GetBatchSimple
((
XList
*
)(
&
inputs
),
(
XList
*
)(
&
golds
));
CheckNTErrors
(
batchEnc
.
order
==
2
,
"Wrong tensor order of the sequence batch"
);
CheckNTErrors
(
batchEnc
.
order
==
2
,
"Wrong tensor order of the sequence batch"
);
...
@@ -404,10 +410,6 @@ void Trainer::Validate(const char* fn, const char* ofn, Model* model)
...
@@ -404,10 +410,6 @@ void Trainer::Validate(const char* fn, const char* ofn, Model* model)
model
->
decoder
->
history
->
ClearHistory
(
/*reset=*/
false
);
model
->
decoder
->
history
->
ClearHistory
(
/*reset=*/
false
);
}
}
batchLoader
.
ClearSamples
(
buf
);
delete
buf
;
double
elapsed
=
GetClockSec
()
-
startT
;
double
elapsed
=
GetClockSec
()
-
startT
;
ENABLE_GRAD
;
ENABLE_GRAD
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论