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
2e20824a
Commit
2e20824a
authored
Sep 15, 2018
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better code of dropout
parent
df76b612
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
69 行增加
和
75 行删除
+69
-75
source/tensor/function/Dropout.cpp
+65
-70
source/tensor/function/Dropout.h
+4
-5
没有找到文件。
source/tensor/function/Dropout.cpp
查看文件 @
2e20824a
...
...
@@ -39,20 +39,19 @@ DTYPE RandomBernoulli(DTYPE prob)
/*
dropout function
It randomly zeroes some of the elements of the input tensor
with probability p via a Bernoulli distribution.
During training, randomly zeroes some of the elements of the input tensor
with probability p using samples from a Bernoulli distribution.
The elements to zero are randomized on every forward call.
See "Improving neural networks by preventing co-adaptation of feature detectors"
for more details.
This has proven to be an effective technique for regularization and
preventing the co-adaptation of neurons as described in the paper
"Improving neural networks by preventing co-adaptation of feature detectors".
Furthermore, the outputs are scaled by a factor of \frac{1}{1-p} during training.
This means that during evaluation the module simply computes an identity function.
Here, the output is scaled by a factor of \frac{1}{1-p} so that we do not need
to mark the tensor with probability p in the inference phase. Instead we perform
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 zero
>> prob - probability to set an element
to
zero
*/
void
_Dropout
(
const
XTensor
*
x
,
XTensor
*
y
,
unsigned
int
seed
,
DTYPE
prob
)
{
...
...
@@ -89,56 +88,8 @@ void _Dropout(const XTensor *x, XTensor *y, unsigned int seed, DTYPE prob)
delete
[]
maskArray
;
}
/*
dropout function (return a XTensor structure)
make a new tensor to keep the result and return it
During training, randomly zeroes some of the elements of the input tensor
with probability p using samples from a Bernoulli distribution.
The elements to zero are randomized on every forward call.
This has proven to be an effective technique for regularization and
preventing the co-adaptation of neurons as described in the paper
"Improving neural networks by preventing co-adaptation of feature detectors".
Furthermore, the outputs are scaled by a factor of \frac{1}{1-p} during training.
This means that during evaluation the module simply computes an identity function.
>> x - input tensor
>> y - output tensor
>> prob - probability to set an element zero
*/
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
)
{
XTensor
y
(
&
x
);
y
.
SetTMP
();
DTYPE
scaleFactor
=
(
DTYPE
)
1.0
/
((
DTYPE
)
1.0
-
prob
);
/* generate a mask tensor again with special probability */
srand
((
unsigned
int
)
time
(
NULL
));
int
unitNum
=
x
.
unitNum
;
DTYPE
*
maskArray
=
new
DTYPE
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
maskArray
[
i
]
=
RandomBernoulli
(
prob
);
XTensor
maskTensor
(
&
x
);
maskTensor
.
SetData
(
maskArray
,
unitNum
);
XTensor
inter
;
inter
=
Multiply
(
x
,
maskTensor
);
y
=
ScaleAndShift
(
inter
,
scaleFactor
,
0
);
delete
[]
maskArray
;
///* tensor connection */
//XLink::MakeLink(&x, NULL, &y, FUNC_DROPOUT);
//XLink::AddParamToHead(&y, prob);
return
y
;
}
/*
backward computation of dropout function
backward computation of
the
dropout function
dE/dx = dE/dy * dy/dx
...
...
@@ -166,15 +117,15 @@ void _DropoutBackward(const XTensor * y, const XTensor * x,
XTensor
*
maskTensor
=
NewTensorBuf
(
x
,
x
->
devID
,
x
->
mem
);
maskTensor
->
SetData
(
maskArray
,
unitNum
);
#ifdef USE_CUDA
if
(
x
->
devID
>=
0
||
y
->
devID
>=
0
){
_CudaDropoutBackward
(
y
,
x
,
dedy
,
dedx
,
maskTensor
,
scaleFactor
);
DelTensorBuf
(
maskTensor
);
delete
[]
maskArray
;
return
;
}
#endif
#ifdef USE_CUDA
if
(
x
->
devID
>=
0
||
y
->
devID
>=
0
){
_CudaDropoutBackward
(
y
,
x
,
dedy
,
dedx
,
maskTensor
,
scaleFactor
);
DelTensorBuf
(
maskTensor
);
delete
[]
maskArray
;
return
;
}
#endif
DTYPE
*
dedyp
=
(
DTYPE
*
)
dedy
->
data
;
DTYPE
*
dedxp
=
(
DTYPE
*
)
dedx
->
data
;
...
...
@@ -189,5 +140,50 @@ void _DropoutBackward(const XTensor * y, const XTensor * x,
else
ShowNTErrors
(
"TODO!"
);
}
/*
dropout function (we make tensor connections here)
It randomly zeroes some of the elements of the input tensor
with probability p via a Bernoulli distribution.
See "Improving neural networks by preventing co-adaptation of feature detectors"
for more details.
Here, the output is scaled by a factor of \frac{1}{1-p} so that we do not need
to mark the tensor with probability p in the inference phase. Instead we perform
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
*/
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
)
{
DTYPE
scaleFactor
=
(
DTYPE
)
1.0
/
((
DTYPE
)
1.0
-
prob
);
/* generate a mask tensor again with special probability */
srand
((
unsigned
int
)
time
(
NULL
));
int
unitNum
=
x
.
unitNum
;
DTYPE
*
maskArray
=
new
DTYPE
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
maskArray
[
i
]
=
RandomBernoulli
(
prob
);
XTensor
maskTensor
(
&
x
);
maskTensor
.
SetData
(
maskArray
,
unitNum
);
XTensor
y
;
XTensor
inter
;
inter
=
Multiply
(
x
,
maskTensor
);
y
=
ScaleAndShift
(
inter
,
scaleFactor
,
0
);
delete
[]
maskArray
;
///* tensor connection */
//XLink::MakeLink(&x, NULL, &y, FUNC_DROPOUT);
//XLink::AddParamToHead(&y, prob);
return
y
;
}
}
//
namespace
nts
(
NiuTrans
.
Tensor
)
\ No newline at end of file
}
// namespace nts(NiuTrans.Tensor)
source/tensor/function/Dropout.h
查看文件 @
2e20824a
...
...
@@ -30,14 +30,14 @@ namespace nts{ // namespace nts(NiuTrans.Tensor)
/* dropout function */
void
_Dropout
(
const
XTensor
*
x
,
XTensor
*
y
,
unsigned
int
seed
,
DTYPE
prob
=
0
.
5
);
/* dropout function */
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
=
0
.
5
);
/* de/dx */
void
_DropoutBackward
(
const
XTensor
*
y
,
const
XTensor
*
x
,
const
XTensor
*
dedy
,
XTensor
*
dedx
,
unsigned
int
seed
,
DTYPE
prob
=
0
.
5
);
/* dropout function */
XTensor
Dropout
(
const
XTensor
&
x
,
DTYPE
prob
=
0
.
5
);
}
// namespace nts(NiuTrans.Tensor)
#endif // __DROPOUT_H__
\ No newline at end of file
#endif // __DROPOUT_H__
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论