Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
7e06541b
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看板
未验证
提交
7e06541b
编写于
10月 25, 2022
作者:
M
MayYouBeProsperous
提交者:
GitHub
10月 25, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Hackathon No.10] Add unit tests for Normal (#47070)
* add test for rsample * add assert in test_backpropagation * fix bug
上级
c1077ae8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
93 addition
and
40 deletion
+93
-40
python/paddle/fluid/tests/unittests/distribution/test_distribution_normal.py
.../tests/unittests/distribution/test_distribution_normal.py
+93
-40
未找到文件。
python/paddle/fluid/tests/unittests/distribution/test_distribution_normal.py
浏览文件 @
7e06541b
...
...
@@ -516,6 +516,14 @@ class NormalTest10(NormalTest):
)
def
kstest
(
loc
,
scale
,
samples
):
# Uses the Kolmogorov-Smirnov test for goodness of fit.
ks
,
_
=
scipy
.
stats
.
kstest
(
samples
,
scipy
.
stats
.
norm
(
loc
=
loc
,
scale
=
scale
).
cdf
)
return
ks
<
0.02
@
place
(
config
.
DEVICES
)
@
parameterize_cls
(
(
TEST_CASE_NAME
,
'loc'
,
'scale'
),
[(
'sample'
,
xrand
((
4
,)),
xrand
((
4
,)))]
...
...
@@ -526,9 +534,7 @@ class TestNormalSampleDygraph(unittest.TestCase):
self
.
paddle_normal
=
Normal
(
loc
=
self
.
loc
,
scale
=
self
.
scale
)
n
=
100000
self
.
sample_shape
=
(
n
,)
self
.
rsample_shape
=
(
n
,)
self
.
samples
=
self
.
paddle_normal
.
sample
(
self
.
sample_shape
)
self
.
rsamples
=
self
.
paddle_normal
.
rsample
(
self
.
rsample_shape
)
def
test_sample
(
self
):
samples_mean
=
self
.
samples
.
mean
(
axis
=
0
)
...
...
@@ -540,38 +546,16 @@ class TestNormalSampleDygraph(unittest.TestCase):
samples_var
,
self
.
paddle_normal
.
variance
,
rtol
=
0.1
,
atol
=
0
)
rsamples_mean
=
self
.
rsamples
.
mean
(
axis
=
0
)
rsamples_var
=
self
.
rsamples
.
var
(
axis
=
0
)
np
.
testing
.
assert_allclose
(
rsamples_mean
,
self
.
paddle_normal
.
mean
,
rtol
=
0.1
,
atol
=
0
)
np
.
testing
.
assert_allclose
(
rsamples_var
,
self
.
paddle_normal
.
variance
,
rtol
=
0.1
,
atol
=
0
)
batch_shape
=
(
self
.
loc
+
self
.
scale
).
shape
self
.
assertEqual
(
self
.
samples
.
shape
,
list
(
self
.
sample_shape
+
batch_shape
)
)
self
.
assertEqual
(
self
.
rsamples
.
shape
,
list
(
self
.
rsample_shape
+
batch_shape
)
)
for
i
in
range
(
len
(
self
.
scale
)):
self
.
assertTrue
(
self
.
_kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
samples
[:,
i
])
)
self
.
assertTrue
(
self
.
_kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
rsamples
[:,
i
])
kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
samples
[:,
i
])
)
def
_kstest
(
self
,
loc
,
scale
,
samples
):
# Uses the Kolmogorov-Smirnov test for goodness of fit.
ks
,
_
=
scipy
.
stats
.
kstest
(
samples
,
scipy
.
stats
.
norm
(
loc
=
loc
,
scale
=
scale
).
cdf
)
return
ks
<
0.02
@
place
(
config
.
DEVICES
)
@
parameterize_cls
(
...
...
@@ -590,17 +574,15 @@ class TestNormalSampleStaic(unittest.TestCase):
)
n
=
100000
self
.
sample_shape
=
(
n
,)
self
.
rsample_shape
=
(
n
,)
self
.
paddle_normal
=
Normal
(
loc
=
loc
,
scale
=
scale
)
mean
=
self
.
paddle_normal
.
mean
variance
=
self
.
paddle_normal
.
variance
samples
=
self
.
paddle_normal
.
sample
(
self
.
sample_shape
)
rsamples
=
self
.
paddle_normal
.
rsample
(
self
.
rsample_shape
)
fetch_list
=
[
mean
,
variance
,
samples
,
rsamples
]
fetch_list
=
[
mean
,
variance
,
samples
]
self
.
feeds
=
{
'loc'
:
self
.
loc
,
'scale'
:
self
.
scale
}
executor
.
run
(
startup_program
)
[
self
.
mean
,
self
.
variance
,
self
.
samples
,
self
.
rsamples
]
=
executor
.
run
(
[
self
.
mean
,
self
.
variance
,
self
.
samples
]
=
executor
.
run
(
main_program
,
feed
=
self
.
feeds
,
fetch_list
=
fetch_list
)
...
...
@@ -610,31 +592,102 @@ class TestNormalSampleStaic(unittest.TestCase):
np
.
testing
.
assert_allclose
(
samples_mean
,
self
.
mean
,
rtol
=
0.1
,
atol
=
0
)
np
.
testing
.
assert_allclose
(
samples_var
,
self
.
variance
,
rtol
=
0.1
,
atol
=
0
)
batch_shape
=
(
self
.
loc
+
self
.
scale
).
shape
self
.
assertEqual
(
self
.
samples
.
shape
,
self
.
sample_shape
+
batch_shape
)
for
i
in
range
(
len
(
self
.
scale
)):
self
.
assertTrue
(
kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
samples
[:,
i
])
)
@
place
(
config
.
DEVICES
)
@
parameterize_cls
(
(
TEST_CASE_NAME
,
'loc'
,
'scale'
),
[(
'rsample'
,
xrand
((
4
,)),
xrand
((
4
,)))]
)
class
TestNormalRSampleDygraph
(
unittest
.
TestCase
):
def
setUp
(
self
):
paddle
.
disable_static
()
self
.
loc
=
paddle
.
to_tensor
(
self
.
loc
)
self
.
scale
=
paddle
.
to_tensor
(
self
.
scale
)
self
.
loc
.
stop_gradient
=
False
self
.
scale
.
stop_gradient
=
False
self
.
paddle_normal
=
Normal
(
loc
=
self
.
loc
,
scale
=
self
.
scale
)
n
=
100000
self
.
rsample_shape
=
[
n
]
self
.
rsamples
=
self
.
paddle_normal
.
rsample
(
self
.
rsample_shape
)
def
test_rsample
(
self
):
rsamples_mean
=
self
.
rsamples
.
mean
(
axis
=
0
)
rsamples_var
=
self
.
rsamples
.
var
(
axis
=
0
)
np
.
testing
.
assert_allclose
(
rsamples_mean
,
self
.
mean
,
rtol
=
0.1
,
atol
=
0
)
np
.
testing
.
assert_allclose
(
rsamples_var
,
self
.
variance
,
rtol
=
0.1
,
atol
=
0
rsamples_mean
,
self
.
paddle_normal
.
mean
,
rtol
=
0.1
,
atol
=
0
)
np
.
testing
.
assert_allclose
(
rsamples_var
,
self
.
paddle_normal
.
variance
,
rtol
=
0.1
,
atol
=
0
)
batch_shape
=
(
self
.
loc
+
self
.
scale
).
shape
self
.
assertEqual
(
self
.
samples
.
shape
,
self
.
sample_shape
+
batch_shape
)
self
.
assertEqual
(
self
.
rsamples
.
shape
,
self
.
rsample_shape
+
batch_shape
)
for
i
in
range
(
len
(
self
.
scale
)):
self
.
assertTrue
(
self
.
_kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
samples
[:,
i
])
kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
r
samples
[:,
i
])
)
self
.
assertTrue
(
self
.
_kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
rsamples
[:,
i
])
def
test_backpropagation
(
self
):
grads
=
paddle
.
grad
([
self
.
rsamples
],
[
self
.
loc
,
self
.
scale
])
self
.
assertEqual
(
len
(
grads
),
2
)
self
.
assertEqual
(
grads
[
0
].
dtype
,
self
.
loc
.
dtype
)
self
.
assertEqual
(
grads
[
0
].
shape
,
self
.
loc
.
shape
)
self
.
assertEqual
(
grads
[
1
].
dtype
,
self
.
scale
.
dtype
)
self
.
assertEqual
(
grads
[
1
].
shape
,
self
.
scale
.
shape
)
@
place
(
config
.
DEVICES
)
@
parameterize_cls
(
(
TEST_CASE_NAME
,
'loc'
,
'scale'
),
[(
'rsample'
,
xrand
((
4
,)),
xrand
((
4
,)))]
)
class
TestNormalRSampleStaic
(
unittest
.
TestCase
):
def
setUp
(
self
):
paddle
.
enable_static
()
startup_program
=
paddle
.
static
.
Program
()
main_program
=
paddle
.
static
.
Program
()
executor
=
paddle
.
static
.
Executor
(
self
.
place
)
with
paddle
.
static
.
program_guard
(
main_program
,
startup_program
):
loc
=
paddle
.
static
.
data
(
'loc'
,
self
.
loc
.
shape
,
self
.
loc
.
dtype
)
scale
=
paddle
.
static
.
data
(
'scale'
,
self
.
scale
.
shape
,
self
.
scale
.
dtype
)
n
=
100000
self
.
rsample_shape
=
(
n
,)
self
.
paddle_normal
=
Normal
(
loc
=
loc
,
scale
=
scale
)
mean
=
self
.
paddle_normal
.
mean
variance
=
self
.
paddle_normal
.
variance
rsamples
=
self
.
paddle_normal
.
rsample
(
self
.
rsample_shape
)
fetch_list
=
[
mean
,
variance
,
rsamples
]
self
.
feeds
=
{
'loc'
:
self
.
loc
,
'scale'
:
self
.
scale
}
executor
.
run
(
startup_program
)
[
self
.
mean
,
self
.
variance
,
self
.
rsamples
]
=
executor
.
run
(
main_program
,
feed
=
self
.
feeds
,
fetch_list
=
fetch_list
)
def
_kstest
(
self
,
loc
,
scale
,
samples
):
# Uses the Kolmogorov-Smirnov test for goodness of fit.
ks
,
_
=
scipy
.
stats
.
kstest
(
samples
,
scipy
.
stats
.
norm
(
loc
=
loc
,
scale
=
scale
).
cdf
def
test_rsample
(
self
):
rsamples_mean
=
self
.
rsamples
.
mean
(
axis
=
0
)
rsamples_var
=
self
.
rsamples
.
var
(
axis
=
0
)
np
.
testing
.
assert_allclose
(
rsamples_mean
,
self
.
mean
,
rtol
=
0.1
,
atol
=
0
)
np
.
testing
.
assert_allclose
(
rsamples_var
,
self
.
variance
,
rtol
=
0.1
,
atol
=
0
)
return
ks
<
0.02
batch_shape
=
(
self
.
loc
+
self
.
scale
).
shape
self
.
assertEqual
(
self
.
rsamples
.
shape
,
self
.
rsample_shape
+
batch_shape
)
for
i
in
range
(
len
(
self
.
scale
)):
self
.
assertTrue
(
kstest
(
self
.
loc
[
i
],
self
.
scale
[
i
],
self
.
rsamples
[:,
i
])
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录