Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
4661f558
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4661f558
编写于
8月 15, 2018
作者:
T
tangwei12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
random optimize
上级
478f73c1
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
27 addition
and
17 deletion
+27
-17
paddle/fluid/operators/sampling_id_op.cc
paddle/fluid/operators/sampling_id_op.cc
+27
-17
未找到文件。
paddle/fluid/operators/sampling_id_op.cc
浏览文件 @
4661f558
...
...
@@ -36,9 +36,19 @@ class SamplingIdKernel : public framework::OpKernel<T> {
std
::
vector
<
T
>
ins_vector
;
framework
::
TensorToVector
(
*
input
,
context
.
device_context
(),
&
ins_vector
);
unsigned
int
seed
=
static_cast
<
unsigned
int
>
(
ctx
.
Attr
<
int
>
(
"seed"
));
std
::
minstd_rand
engine
;
if
(
seed
==
0
)
{
seed
=
std
::
random_device
()();
}
engine
.
seed
(
seed
);
std
::
uniform_real_distribution
<
T
>
dist
(
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"min"
)),
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"max"
)));
std
::
vector
<
T
>
ids
(
batch_size
);
for
(
size_t
i
=
0
;
i
<
batch_size
;
++
i
)
{
double
r
=
getRandReal
(
);
double
r
=
dist
(
engine
);
int
idx
=
width
-
1
;
for
(
int
j
=
0
;
j
<
width
;
++
j
)
{
if
((
r
-=
ins_vector
[
i
*
width
+
j
])
<
0
)
{
...
...
@@ -57,16 +67,6 @@ class SamplingIdKernel : public framework::OpKernel<T> {
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
framework
::
TensorFromVector
(
ids
,
context
.
device_context
(),
output
);
}
private:
double
getRandReal
()
const
{
std
::
random_device
rd
;
// Will be used to obtain a seed for the random number engine
std
::
mt19937
gen
(
rd
());
// Standard mersenne_twister_engine seeded with
// rd()
std
::
uniform_real_distribution
<>
dis
(
1.0
,
2.0
);
return
dis
(
gen
);
}
};
class
SamplingIdOp
:
public
framework
::
OperatorWithKernel
{
...
...
@@ -78,6 +78,9 @@ class SamplingIdOp : public framework::OperatorWithKernel {
"Input(X) of SamplingIdOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of SamplingIdOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
Attrs
().
Get
<
float
>
(
"min"
)
<
ctx
->
Attrs
().
Get
<
float
>
(
"max"
),
"min must less then max"
);
auto
input_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE
(
input_dims
.
size
()
==
2
,
...
...
@@ -99,7 +102,17 @@ class SamplingIdOpMaker : public framework::OpProtoAndCheckerMaker {
AddComment
(
R"DOC(
SamplingId Operator.
A layer for sampling id from multinomial distribution from the
input layer. Sampling one id for one sample.)DOC"
);
input. Sampling one id for one sample.)DOC"
);
AddAttr
<
float
>
(
"min"
,
"Minimum value of random. [default 0.0]."
)
.
SetDefault
(
0.0
f
);
AddAttr
<
float
>
(
"max"
,
"Maximun value of random. [default 1.0]."
)
.
SetDefault
(
1.0
f
);
AddAttr
<
int
>
(
"seed"
,
"Random seed used for the random number engine. "
"0 means use a seed generated by the system."
"Note that if seed is not 0, this operator will always "
"generate the same random numbers every time. [default 0]."
)
.
SetDefault
(
0
);
}
};
}
// namespace operators
...
...
@@ -109,8 +122,5 @@ namespace ops = paddle::operators;
REGISTER_OPERATOR
(
sampling_id
,
ops
::
SamplingIdOp
,
ops
::
SamplingIdOpMaker
,
paddle
::
framework
::
EmptyGradOpMaker
);
REGISTER_OP_CPU_KERNEL
(
sampling_id
,
ops
::
SamplingIdKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
SamplingIdKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
,
ops
::
SamplingIdKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
SamplingIdKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
);
REGISTER_OP_CPU_KERNEL
(
sampling_id
,
paddle
::
operators
::
SamplingIdKernel
<
float
>
,
paddle
::
operators
::
SamplingIdKernel
<
double
>
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录