Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
7eb29f26
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
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看板
提交
7eb29f26
编写于
9月 23, 2016
作者:
Y
Yu Yang
提交者:
emailweixu
9月 22, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Try to fix MultinomialSampler (#102)
* Also refine unittest to multiple iteration to prevent luckily random number.
上级
8e957df4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
36 addition
and
31 deletion
+36
-31
paddle/gserver/layers/MultinomialSampler.cpp
paddle/gserver/layers/MultinomialSampler.cpp
+3
-2
paddle/gserver/tests/test_MultinomialSampler.cpp
paddle/gserver/tests/test_MultinomialSampler.cpp
+33
-29
未找到文件。
paddle/gserver/layers/MultinomialSampler.cpp
浏览文件 @
7eb29f26
...
...
@@ -19,7 +19,7 @@ namespace paddle {
MultinomialSampler
::
MultinomialSampler
(
const
real
*
prob
,
int
size
)
:
rand_
(
0.0
,
size
)
{
intervals_
.
res
erv
e
(
size
+
1
);
intervals_
.
res
iz
e
(
size
+
1
);
double
sum
=
0
;
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
sum
+=
prob
[
i
];
...
...
@@ -50,12 +50,13 @@ MultinomialSampler::MultinomialSampler(const real* prob, int size)
int
bigPos
=
nextBigPos
(
0
);
auto
fillIntervals
=
[
&
]()
{
while
(
bigPos
<
size
&&
smallPos
<
size
)
{
while
(
bigPos
<
size
)
{
while
(
intervals_
[
bigPos
].
thresh
>
1
&&
smallPos
<
size
)
{
intervals_
[
smallPos
].
otherId
=
bigPos
;
intervals_
[
bigPos
].
thresh
-=
1
-
intervals_
[
smallPos
].
thresh
;
smallPos
=
nextSmallPos
(
smallPos
+
1
);
}
if
(
smallPos
>=
size
)
break
;
bigPos
=
nextBigPos
(
bigPos
+
1
);
// If intervals_[bigPos].thresh < 1, it becomes a small interval
}
...
...
paddle/gserver/tests/test_MultinomialSampler.cpp
浏览文件 @
7eb29f26
...
...
@@ -41,39 +41,42 @@ public:
TEST
(
MultinomialSampler
,
gen
)
{
int
numGrids
=
1024
*
1024
;
int
size
=
1024
*
4
;
default_random_engine
reng
;
uniform_int_distribution
<
int
>
rand
(
1
,
numGrids
/
size
*
1.8
);
vector
<
real
>
prob
;
int
sum
=
0
;
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
prob
.
push_back
(
rand
(
reng
));
sum
+=
prob
.
back
();
}
CHECK_LE
(
sum
,
numGrids
);
prob
.
back
()
+=
numGrids
-
sum
;
vector
<
int
>
counts
(
size
);
MultinomialSamplerTester
sampler
(
&
prob
[
0
],
size
);
counts
.
assign
(
size
,
0
);
{
double
s
=
(
double
)
size
/
(
double
)
numGrids
;
REGISTER_TIMER
(
"MultinomialSampler"
);
for
(
double
i
=
0
;
i
<
numGrids
;
++
i
)
{
int
ret
=
sampler
.
testGen
([
i
,
s
]()
{
return
s
*
i
;
});
if
(
ret
<
0
||
ret
>=
size
)
{
EXPECT_GE
(
ret
,
0
);
EXPECT_LT
(
ret
,
size
);
break
;
for
(
size_t
iter
=
0
;
iter
<
256
;
++
iter
)
{
uniform_int_distribution
<
int
>
rand
(
1
,
numGrids
/
size
*
1.8
);
vector
<
real
>
prob
;
int
sum
=
0
;
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
prob
.
push_back
(
rand
(
reng
));
sum
+=
prob
.
back
();
}
CHECK_LE
(
sum
,
numGrids
);
prob
.
back
()
+=
numGrids
-
sum
;
vector
<
int
>
counts
(
size
);
MultinomialSamplerTester
sampler
(
&
prob
[
0
],
size
);
counts
.
assign
(
size
,
0
);
{
double
s
=
(
double
)
size
/
(
double
)
numGrids
;
REGISTER_TIMER
(
"MultinomialSampler"
);
for
(
double
i
=
0
;
i
<
numGrids
;
++
i
)
{
int
ret
=
sampler
.
testGen
([
i
,
s
]()
{
return
s
*
i
;
});
if
(
ret
<
0
||
ret
>=
size
)
{
EXPECT_GE
(
ret
,
0
);
EXPECT_LT
(
ret
,
size
);
break
;
}
++
counts
[
ret
];
}
++
counts
[
ret
];
}
}
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
if
(
prob
[
i
]
!=
counts
[
i
])
{
EXPECT_EQ
(
prob
[
i
],
counts
[
i
])
;
LOG
(
INFO
)
<<
"i="
<<
i
;
break
;
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
if
(
prob
[
i
]
!=
counts
[
i
]
)
{
EXPECT_EQ
(
prob
[
i
],
counts
[
i
]);
LOG
(
INFO
)
<<
iter
;
break
;
}
}
}
}
...
...
@@ -135,6 +138,7 @@ void benchmarkRandom() {
LOG
(
INFO
)
<<
"sum1="
<<
sum1
;
}
int
main
(
int
argc
,
char
**
argv
)
{
initMain
(
argc
,
argv
);
testing
::
InitGoogleTest
(
&
argc
,
argv
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录