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
c6f50a22
Commit
c6f50a22
authored
Oct 09, 2018
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
load batch of sequence on both langauge sides
parent
430f0dfc
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
86 行增加
和
12 行删除
+86
-12
source/sample/transformer/T2TDecoder.cpp
+5
-5
source/sample/transformer/T2TDecoder.h
+1
-1
source/sample/transformer/T2TModel.cpp
+54
-3
source/sample/transformer/T2TModel.h
+8
-2
source/sample/transformer/T2TTrainer.cpp
+0
-0
source/sample/transformer/T2TTrainer.h
+18
-1
没有找到文件。
source/sample/transformer/T2TDecoder.cpp
查看文件 @
c6f50a22
...
...
@@ -67,17 +67,17 @@ void AttDecoder::InitModel(int argc, char ** argv,
/*
make the decoding network
>> input - the input tensor of the decoder
>>
encoderOutput
- the output tensor of the encoder
>> input
Dec
- the input tensor of the decoder
>>
outputEnc
- the output tensor of the encoder
>> mask - the mask that indicate each position is valid
>> isTraining - indicates whether the model is used for training
<< return - the output tensor of the encoder
*/
XTensor
AttDecoder
::
Make
(
XTensor
&
input
,
XTensor
&
encoderOutput
,
XTensor
&
mask
,
bool
isTraining
)
XTensor
AttDecoder
::
Make
(
XTensor
&
input
Dec
,
XTensor
&
outputEnc
,
XTensor
&
mask
,
bool
isTraining
)
{
XTensor
x
;
x
=
embedder
.
Make
(
input
);
x
=
embedder
.
Make
(
input
Dec
);
/* dropout */
if
(
isTraining
&&
dropoutP
>
0
)
...
...
@@ -106,7 +106,7 @@ XTensor AttDecoder::Make(XTensor &input, XTensor &encoderOutput, XTensor &mask,
/*****************************/
/* encoder-decoder attention */
ende
=
attentionsEnde
[
i
].
Make
(
encoderOutput
,
x
,
encoderOutput
,
mask
,
isTraining
);
ende
=
attentionsEnde
[
i
].
Make
(
outputEnc
,
x
,
outputEnc
,
mask
,
isTraining
);
/* dropout */
if
(
isTraining
&&
dropoutP
>
0
)
...
...
source/sample/transformer/T2TDecoder.h
查看文件 @
c6f50a22
...
...
@@ -48,7 +48,7 @@ public:
int
myDevID
=
-
1
,
XMem
*
myMem
=
NULL
);
/* make the decoding network */
XTensor
Make
(
XTensor
&
input
,
XTensor
&
encoderOutput
,
XTensor
&
mask
,
bool
isTraining
);
XTensor
Make
(
XTensor
&
input
Dec
,
XTensor
&
outputEnc
,
XTensor
&
mask
,
bool
isTraining
);
};
}
...
...
source/sample/transformer/T2TModel.cpp
查看文件 @
c6f50a22
...
...
@@ -90,13 +90,27 @@ make the encoding network
>> isTraining - indicates whether we are training the model
<< return - encoding result
*/
XTensor
T2TModel
::
MakeEncod
ing
(
XTensor
&
input
,
XTensor
&
mask
,
bool
isTraining
)
XTensor
T2TModel
::
MakeEncod
er
(
XTensor
&
input
,
XTensor
&
mask
,
bool
isTraining
)
{
return
encoder
.
Make
(
input
,
mask
,
isTraining
);
}
/*
make the entire network for language modeling (with the output softmax layer)
make the decoding network
>> inputDec - input tensor of the decoder
>> outputEnc - output tensor of the encoder
>> output - output tensor (distribution)
>> mask - the mask for positions that are/not involved in computation
>> isTraining - indicates whether we are training the model
<< return - encoding result
*/
XTensor
T2TModel
::
MakeDecoder
(
XTensor
&
inputDec
,
XTensor
&
outputEnc
,
XTensor
&
mask
,
bool
isTraining
)
{
return
decoder
.
Make
(
inputDec
,
outputEnc
,
mask
,
isTraining
);
}
/*
make the network for language modeling (with the output softmax layer)
>> input - input tensor
>> output - output tensor (distribution)
>> padding - padding of the sequences
...
...
@@ -145,7 +159,7 @@ void T2TModel::MakeLM(XTensor &input, XTensor &output, XTensor &padding, bool is
//_Sum(&mask, padding3, &mask);
encoding
=
MakeEncod
ing
(
input
,
mask
,
isTraining
);
encoding
=
MakeEncod
er
(
input
,
mask
,
isTraining
);
outputLayer
.
Make
(
encoding
,
output
);
delete
[]
dims
;
...
...
@@ -156,6 +170,43 @@ void T2TModel::MakeLM(XTensor &input, XTensor &output, XTensor &padding, bool is
}
/*
make the network for machine translation (with the output softmax layer)
>> inputEnc - input tensor of the encoder
>> inputDec - input tensor of the decoder
>> output - output tensor (distribution)
>> padding - padding of the sequences
>> isTraining - indicates whether the model is for training
*/
void
T2TModel
::
MakeMT
(
XTensor
&
inputEnc
,
XTensor
&
inputDec
,
XTensor
&
output
,
XTensor
&
padding
,
bool
isTraining
)
{
XTensor
encoding
;
XTensor
decoding
;
XTensor
maskEnc
;
XTensor
maskDec
;
/* generate mask to see "previous" words on the decoder side */
int
len
=
inputDec
.
GetDim
(
inputDec
.
order
-
2
);
int
*
dims
=
new
int
[
inputDec
.
order
+
1
];
for
(
int
i
=
0
;
i
<
inputDec
.
order
;
i
++
)
dims
[
i
+
1
]
=
inputDec
.
GetDim
(
i
);
dims
[
0
]
=
nhead
;
dims
[
inputDec
.
order
]
=
len
;
InitTensor
(
&
maskDec
,
inputDec
.
order
+
1
,
dims
,
X_FLOAT
,
1.0
F
,
inputDec
.
devID
,
inputDec
.
mem
);
/* a upper triangular matrix where the cells of the upper triangular are set to -1e-9.
this matrix can be used to prevent the attention to current or following words in
a given sequence. */
_SetDataLowTri
(
&
maskDec
,
1e9
F
,
0
);
_ScaleAndShiftMe
(
&
maskDec
,
1.0
F
,
-
1e9
F
);
encoding
=
MakeEncoder
(
inputEnc
,
maskEnc
,
isTraining
);
decoding
=
MakeDecoder
(
inputDec
,
encoding
,
maskDec
,
isTraining
);
outputLayer
.
Make
(
decoding
,
output
);
delete
[]
dims
;
}
/*
get parameter matrics
>> list - the list that keeps the parameter matrics
*/
...
...
source/sample/transformer/T2TModel.h
查看文件 @
c6f50a22
...
...
@@ -69,11 +69,17 @@ public:
void
InitModel
(
int
argc
,
char
**
argv
);
/* make the encoding network */
XTensor
MakeEncod
ing
(
XTensor
&
input
,
XTensor
&
mask
,
bool
isTraining
);
XTensor
MakeEncod
er
(
XTensor
&
input
,
XTensor
&
mask
,
bool
isTraining
);
/* make the entire network for langauge modeling (with the output softmax layer) */
/* make the encoding network */
XTensor
MakeDecoder
(
XTensor
&
inputEnc
,
XTensor
&
inputDec
,
XTensor
&
mask
,
bool
isTraining
);
/* make the network for langauge modeling (with the output softmax layer) */
void
MakeLM
(
XTensor
&
input
,
XTensor
&
output
,
XTensor
&
padding
,
bool
isTraining
);
/* make the network for machine translation (with the output softmax layer) */
void
MakeMT
(
XTensor
&
inputEnc
,
XTensor
&
inputDec
,
XTensor
&
output
,
XTensor
&
padding
,
bool
isTraining
);
/* get parameter matrics */
void
GetParams
(
XList
&
list
);
...
...
source/sample/transformer/T2TTrainer.cpp
查看文件 @
c6f50a22
差异被折叠。
点击展开。
source/sample/transformer/T2TTrainer.h
查看文件 @
c6f50a22
...
...
@@ -79,6 +79,9 @@ public:
/* vocabulary size of the source side */
int
vSize
;
/* vocabulary size of the target side */
int
vSizeTgt
;
/* learning rate */
float
lrate
;
...
...
@@ -160,10 +163,24 @@ public:
int
LoadBatch
(
FILE
*
file
,
bool
isLM
,
XTensor
*
batch
,
XTensor
*
padding
,
XTensor
*
output
,
int
*
seqs
,
int
step
,
int
vs
,
int
sBatch
,
int
wBatch
,
int
vsEnc
,
int
vsDec
,
int
sBatch
,
int
wBatch
,
bool
isSorted
,
int
&
wCount
,
int
devID
,
XMem
*
mem
);
/* load a batch of sequences (for language modeling) */
int
LoadBatchLM
(
FILE
*
file
,
XTensor
*
batch
,
XTensor
*
padding
,
XTensor
*
output
,
int
*
seqs
,
int
vs
,
int
sBatch
,
int
wBatch
,
bool
isSorted
,
int
&
wCount
,
int
devID
,
XMem
*
mem
);
/* load a batch of sequences (for machine translation) */
int
LoadBatchMT
(
FILE
*
file
,
XTensor
*
batch
,
XTensor
*
padding
,
XTensor
*
output
,
int
*
seqs
,
int
vsEnc
,
int
vsDec
,
int
sBatch
,
int
wBatch
,
bool
isSorted
,
int
&
wCount
,
int
devID
,
XMem
*
mem
);
/* shuffle the data file */
void
Shuffle
(
const
char
*
srcFile
,
const
char
*
tgtFile
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论