Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
72e068f1
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
72e068f1
编写于
11月 16, 2020
作者:
P
pangyoki
提交者:
GitHub
11月 16, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix test_multinomial (#28558)
* fix test_multinomial * fix test_multinomial add 0 prob
上级
b889a0ce
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
31 addition
and
33 deletion
+31
-33
python/paddle/fluid/tests/unittests/test_multinomial_op.py
python/paddle/fluid/tests/unittests/test_multinomial_op.py
+31
-33
未找到文件。
python/paddle/fluid/tests/unittests/test_multinomial_op.py
浏览文件 @
72e068f1
...
...
@@ -22,6 +22,26 @@ from op_test import OpTest
import
numpy
as
np
def
sample_output_one_dimension
(
out
,
dim
):
# count numbers of different categories
sample_prob
=
np
.
zeros
(
dim
).
astype
(
"float32"
)
sample_index_prob
=
np
.
unique
(
out
,
return_counts
=
True
)
sample_prob
[
sample_index_prob
[
0
]]
=
sample_index_prob
[
1
]
sample_prob
/=
sample_prob
.
sum
()
return
sample_prob
def
sample_output_two_dimension
(
out
,
shape
):
num_dist
=
shape
[
0
]
out_list
=
np
.
split
(
out
,
num_dist
,
axis
=
0
)
sample_prob
=
np
.
zeros
(
shape
).
astype
(
"float32"
)
for
i
in
range
(
num_dist
):
sample_index_prob
=
np
.
unique
(
out_list
[
i
],
return_counts
=
True
)
sample_prob
[
i
][
sample_index_prob
[
0
]]
=
sample_index_prob
[
1
]
sample_prob
/=
sample_prob
.
sum
(
axis
=-
1
,
keepdims
=
True
)
return
sample_prob
class
TestMultinomialOp
(
OpTest
):
def
setUp
(
self
):
paddle
.
enable_static
()
...
...
@@ -39,10 +59,7 @@ class TestMultinomialOp(OpTest):
self
.
check_output_customized
(
self
.
verify_output
)
def
sample_output
(
self
,
out
):
# count numbers of different categories
sample_prob
=
np
.
unique
(
out
,
return_counts
=
True
)[
1
].
astype
(
"float32"
)
sample_prob
/=
sample_prob
.
sum
()
return
sample_prob
return
sample_output_one_dimension
(
out
,
4
)
def
verify_output
(
self
,
outs
):
# normalize the input to get the probability
...
...
@@ -62,14 +79,7 @@ class TestMultinomialOp2(TestMultinomialOp):
self
.
attrs
=
{
"num_samples"
:
100000
,
"replacement"
:
True
}
def
sample_output
(
self
,
out
):
out_list
=
np
.
split
(
out
,
3
,
axis
=
0
)
count_array
=
[
0
]
*
3
for
i
in
range
(
3
):
count_array
[
i
]
=
np
.
unique
(
out_list
[
i
],
return_counts
=
True
)[
1
].
astype
(
"float32"
)
sample_prob
=
np
.
stack
(
count_array
,
axis
=
0
)
sample_prob
/=
sample_prob
.
sum
(
axis
=-
1
,
keepdims
=
True
)
return
sample_prob
return
sample_output_two_dimension
(
out
,
[
3
,
4
])
class
TestMultinomialOp3
(
TestMultinomialOp
):
...
...
@@ -91,15 +101,12 @@ class TestMultinomialApi(unittest.TestCase):
def
test_dygraph
(
self
):
# input probability is a vector, and replacement is True
paddle
.
disable_static
()
x
=
paddle
.
rand
([
4
])
x_numpy
=
np
.
random
.
rand
(
4
)
x
=
paddle
.
to_tensor
(
x_numpy
)
out
=
paddle
.
multinomial
(
x
,
num_samples
=
100000
,
replacement
=
True
)
x_numpy
=
x
.
numpy
()
paddle
.
enable_static
()
sample_prob
=
np
.
unique
(
out
.
numpy
(),
return_counts
=
True
)[
1
].
astype
(
"float32"
)
sample_prob
/=
sample_prob
.
sum
()
sample_prob
=
sample_output_one_dimension
(
out
.
numpy
(),
4
)
prob
=
x_numpy
/
x_numpy
.
sum
(
axis
=-
1
,
keepdims
=
True
)
self
.
assertTrue
(
np
.
allclose
(
...
...
@@ -109,18 +116,11 @@ class TestMultinomialApi(unittest.TestCase):
def
test_dygraph2
(
self
):
# input probability is a matrix, and replacement is True
paddle
.
disable_static
()
x
=
paddle
.
rand
([
3
,
4
])
x_numpy
=
np
.
random
.
rand
(
3
,
4
)
x
=
paddle
.
to_tensor
(
x_numpy
)
out
=
paddle
.
multinomial
(
x
,
num_samples
=
100000
,
replacement
=
True
)
x_numpy
=
x
.
numpy
()
out_list
=
np
.
split
(
out
.
numpy
(),
3
,
axis
=
0
)
count_array
=
[
0
]
*
3
for
i
in
range
(
3
):
count_array
[
i
]
=
np
.
unique
(
out_list
[
i
],
return_counts
=
True
)[
1
].
astype
(
"float32"
)
sample_prob
=
np
.
stack
(
count_array
,
axis
=
0
)
sample_prob
/=
sample_prob
.
sum
(
axis
=-
1
,
keepdims
=
True
)
sample_prob
=
sample_output_two_dimension
(
out
.
numpy
(),
[
3
,
4
])
prob
=
x_numpy
/
x_numpy
.
sum
(
axis
=-
1
,
keepdims
=
True
)
self
.
assertTrue
(
np
.
allclose
(
...
...
@@ -131,9 +131,9 @@ class TestMultinomialApi(unittest.TestCase):
def
test_dygraph3
(
self
):
# replacement is False. number of samples must be less than number of categories.
paddle
.
disable_static
()
x
=
paddle
.
rand
([
1000
])
x_numpy
=
np
.
random
.
rand
(
1000
)
x
=
paddle
.
to_tensor
(
x_numpy
)
out
=
paddle
.
multinomial
(
x
,
num_samples
=
100
,
replacement
=
False
)
x_numpy
=
x
.
numpy
()
unique_out
=
np
.
unique
(
out
.
numpy
())
self
.
assertEqual
(
...
...
@@ -158,9 +158,7 @@ class TestMultinomialApi(unittest.TestCase):
x_np
=
np
.
random
.
rand
(
4
).
astype
(
'float32'
)
out
=
exe
.
run
(
train_program
,
feed
=
{
'x'
:
x_np
},
fetch_list
=
[
out
])
sample_prob
=
np
.
unique
(
out
,
return_counts
=
True
)[
1
].
astype
(
"float32"
)
sample_prob
/=
sample_prob
.
sum
()
sample_prob
=
sample_output_one_dimension
(
out
,
4
)
prob
=
x_np
/
x_np
.
sum
(
axis
=-
1
,
keepdims
=
True
)
self
.
assertTrue
(
np
.
allclose
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录