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
56326405
Commit
56326405
authored
Sep 16, 2018
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
faster dropout implementation
parent
7f9c0b47
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
21 行增加
和
20 行删除
+21
-20
source/sample/transformer/T2TEncoder.cpp
+1
-1
source/sample/transformer/T2TTrainer.cpp
+2
-10
source/tensor/function/Dropout.cpp
+12
-3
source/tensor/function/Dropout.h
+4
-4
source/tensor/test/TDropout.cpp
+2
-2
没有找到文件。
source/sample/transformer/T2TEncoder.cpp
查看文件 @
56326405
...
...
@@ -154,7 +154,7 @@ XTensor AttEncoder::Make(XTensor &input, XTensor &mask, bool skipInputRes, bool
x
=
fnnLayerNorms
[
i
].
Make
(
res
);
if
(
isTraining
&&
dropoutP
>
0
)
x
=
Dropout
(
x
);
x
=
Dropout
(
x
,
dropoutP
);
}
return
x
;
...
...
source/sample/transformer/T2TTrainer.cpp
查看文件 @
56326405
...
...
@@ -166,16 +166,6 @@ void T2TTrainer::Train(const char * fn, T2TModel * model)
/* get probabilities */
float
prob
=
GetProb
(
&
output
,
&
gold
,
NULL
);
MTYPE
totalUsed
=
0
;
MTYPE
totalSize
=
0
;
for
(
int
i
=
0
;
i
<=
mem
->
curBlockID
;
i
++
)
{
totalSize
+=
mem
->
blocks
[
i
].
size
;
totalUsed
+=
mem
->
blocks
[
i
].
used
;
}
//fprintf(stderr, "%d(%ld,%ld,%f)\n", mem->curBlockID, totalUsed, totalSize, (float)totalUsed/totalSize);
loss
+=
-
prob
;
wordCount
+=
wc
;
...
...
@@ -209,6 +199,8 @@ void T2TTrainer::Train(const char * fn, T2TModel * model)
fclose
(
tf
);
epoch
=
MIN
(
epoch
,
nepoch
);
XPRINT6
(
0
,
stderr
,
"[INFO] lr=%.2e, elapsed=%.1fs, step=%d, epoch=%d, word=%d, ppl=%.3f
\n
"
,
lr
,
elapsed
,
step
,
epoch
,
wordCountTotal
,
exp
(
loss
/
wordCount
));
XPRINT3
(
0
,
stderr
,
"[INFO] training finished (took %.1fs, step=%d and epoch=%d)
\n
"
,
...
...
source/tensor/function/Dropout.cpp
查看文件 @
56326405
...
...
@@ -25,6 +25,7 @@
#include "Dropout.h"
#include "Dropout.cuh"
#include "../core/arithmetic/Multiply.h"
#include "../core/arithmetic/SumDim.h"
#include "../core/math/ScaleAndShift.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor
...
...
@@ -148,13 +149,15 @@ the same inference procedure as that with no use of dropout on the test data.
>> x - input tensor
>> y - output tensor
>> prob - probability to set an element to zero
>> leadDim - the dimension along which we generate the random numbers
*/
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
)
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
,
int
leadDim
)
{
int
n
=
leadDim
<
0
?
x
.
order
-
1
:
leadDim
;
DTYPE
scaleFactor
=
(
DTYPE
)
1.0
/
((
DTYPE
)
1.0
-
prob
);
/* generate a mask tensor with probability p */
int
unitNum
=
x
.
unitNum
;
int
unitNum
=
x
.
dimSize
[
n
]
;
DTYPE
*
maskArray
=
new
DTYPE
[
unitNum
];
srand
((
unsigned
int
)
time
(
NULL
));
...
...
@@ -162,9 +165,15 @@ XTensor Dropout(const XTensor &x, DTYPE prob)
maskArray
[
i
]
=
RandomBernoulli
(
prob
,
scaleFactor
);
XTensor
mask
(
&
x
);
mask
.
SetData
(
maskArray
,
unitNum
);
mask
.
SetZeroAll
();
XTensor
*
maskVector
=
NewTensorBuf
(
1
,
&
unitNum
,
X_FLOAT
,
1.0
F
,
x
.
devID
,
x
.
mem
);
maskVector
->
SetData
(
maskArray
,
unitNum
);
_SumDim
(
&
mask
,
maskVector
,
&
mask
,
n
);
delete
[]
maskArray
;
DelTensorBuf
(
maskVector
);
return
Multiply
(
x
,
mask
);
}
...
...
source/tensor/function/Dropout.h
查看文件 @
56326405
...
...
@@ -30,19 +30,19 @@ namespace nts{ // namespace nts(NiuTrans.Tensor)
/* generate a random bernoulli number */
inline
DTYPE
RandomBernoulli
(
DTYPE
prob
,
DTYPE
value
)
{
return
(
DTYPE
)
rand
()
/
(
DTYPE
)
RAND_MAX
>
prob
?
(
DTYPE
)
value
:
(
DTYPE
)
0
.
0
;
return
(
DTYPE
)
rand
()
/
(
DTYPE
)
RAND_MAX
>
=
prob
?
(
DTYPE
)
value
:
0
;
}
/* dropout function */
void
_Dropout
(
const
XTensor
*
x
,
XTensor
*
y
,
unsigned
int
seed
,
DTYPE
prob
=
0
.
5
);
void
_Dropout
(
const
XTensor
*
x
,
XTensor
*
y
,
unsigned
int
seed
,
DTYPE
prob
);
/* de/dx */
void
_DropoutBackward
(
const
XTensor
*
y
,
const
XTensor
*
x
,
const
XTensor
*
dedy
,
XTensor
*
dedx
,
unsigned
int
seed
,
DTYPE
prob
=
0
.
5
);
unsigned
int
seed
,
DTYPE
prob
);
/* dropout function */
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
=
0
.
5
);
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
,
int
leadDim
=
-
1
);
}
// namespace nts(NiuTrans.Tensor)
...
...
source/tensor/test/TDropout.cpp
查看文件 @
56326405
...
...
@@ -56,7 +56,7 @@ bool TestDropout1()
float
prob
=
0.2
F
;
int
seed
=
20
;
_Dropout
(
x
,
y
,
seed
,
prob
);
yUser
=
Dropout
(
*
x
);
yUser
=
Dropout
(
*
x
,
0.5
F
);
/* check result */
int
zeroNum1
=
0
;
...
...
@@ -92,7 +92,7 @@ bool TestDropout1()
/* call Dropout function */
_Dropout
(
xGPU
,
yGPU
,
seed
,
prob
);
yUserGPU
=
Dropout
(
*
xGPU
);
yUserGPU
=
Dropout
(
*
xGPU
,
0.5
F
);
/* check result */
zeroNum1
=
0
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论