Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1b750f2d
提交
1b750f2d
authored
10月 11, 2017
作者:
mrTsjolder
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tests for truncated normal
上级
f454ca19
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
141 行增加
和
4 行删除
+141
-4
test_rng_mrg.py
theano/sandbox/tests/test_rng_mrg.py
+141
-4
没有找到文件。
theano/sandbox/tests/test_rng_mrg.py
浏览文件 @
1b750f2d
...
@@ -298,7 +298,8 @@ def test_broadcastable():
...
@@ -298,7 +298,8 @@ def test_broadcastable():
pvals_2
=
R
.
uniform
(
size
=
size2
)
pvals_2
=
R
.
uniform
(
size
=
size2
)
pvals_2
=
pvals_2
/
tensor
.
sum
(
pvals_2
)
pvals_2
=
pvals_2
/
tensor
.
sum
(
pvals_2
)
for
distribution
in
[
R
.
uniform
,
R
.
binomial
,
R
.
multinomial
,
R
.
multinomial_wo_replacement
,
R
.
normal
]:
for
distribution
in
[
R
.
uniform
,
R
.
normal
,
R
.
truncated_normal
,
R
.
binomial
,
R
.
multinomial
,
R
.
multinomial_wo_replacement
]:
# multinomial or multinomial_wo_replacement does not support "size" argument,
# multinomial or multinomial_wo_replacement does not support "size" argument,
# the sizes of them are implicitly defined with "pvals" argument.
# the sizes of them are implicitly defined with "pvals" argument.
if
distribution
in
[
R
.
multinomial
,
R
.
multinomial_wo_replacement
]:
if
distribution
in
[
R
.
multinomial
,
R
.
multinomial_wo_replacement
]:
...
@@ -378,7 +379,6 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
...
@@ -378,7 +379,6 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
@attr
(
'slow'
)
@attr
(
'slow'
)
def
test_normal0
():
def
test_normal0
():
steps
=
50
steps
=
50
std
=
2.
std
=
2.
if
(
config
.
mode
in
[
'DEBUG_MODE'
,
'DebugMode'
,
'FAST_COMPILE'
]
or
if
(
config
.
mode
in
[
'DEBUG_MODE'
,
'DebugMode'
,
'FAST_COMPILE'
]
or
...
@@ -391,7 +391,7 @@ def test_normal0():
...
@@ -391,7 +391,7 @@ def test_normal0():
sample_size_odd
=
(
sample_size
[
0
],
sample_size
[
1
]
-
1
)
sample_size_odd
=
(
sample_size
[
0
],
sample_size
[
1
]
-
1
)
x
=
tensor
.
matrix
()
x
=
tensor
.
matrix
()
for
size
,
const_size
,
var_input
,
input
,
avg
,
rtol
,
std_tol
in
[
test_cases
=
[
(
sample_size
,
sample_size
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
(
sample_size
,
sample_size
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
(
x
.
shape
,
sample_size
,
[
x
],
(
x
.
shape
,
sample_size
,
[
x
],
[
np
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
[
np
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
...
@@ -409,8 +409,9 @@ def test_normal0():
...
@@ -409,8 +409,9 @@ def test_normal0():
# test with few samples at the same time
# test with few samples at the same time
((
1
,),
(
1
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
((
1
,),
(
1
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
((
3
,),
(
3
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
((
3
,),
(
3
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
]:
]
for
size
,
const_size
,
var_input
,
input
,
avg
,
rtol
,
std_tol
in
test_cases
:
R
=
MRG_RandomStreams
(
234
)
R
=
MRG_RandomStreams
(
234
)
# Note: we specify `nstreams` to avoid a warning.
# Note: we specify `nstreams` to avoid a warning.
n
=
R
.
normal
(
size
=
size
,
avg
=
avg
,
std
=
std
,
n
=
R
.
normal
(
size
=
size
,
avg
=
avg
,
std
=
std
,
...
@@ -438,6 +439,126 @@ def test_normal0():
...
@@ -438,6 +439,126 @@ def test_normal0():
prefix
=
'numpy '
,
allow_01
=
True
,
inputs
=
input
,
mean_rtol
=
rtol
)
prefix
=
'numpy '
,
allow_01
=
True
,
inputs
=
input
,
mean_rtol
=
rtol
)
@attr
(
'slow'
)
def
test_normal_truncation
():
# just a copy of test_normal0 with extra bound check
steps
=
50
std
=
2.
# standard deviation is slightly less than for a regular Gaussian
# constant taken from scipy.stats.truncnorm.std(a=-2, b=2, loc=0., scale=1.)
target_std
=
.
87962566103423978
*
std
if
(
config
.
mode
in
[
'DEBUG_MODE'
,
'DebugMode'
,
'FAST_COMPILE'
]
or
config
.
mode
==
'Mode'
and
config
.
linker
in
[
'py'
]):
sample_size
=
(
25
,
30
)
default_rtol
=
.
02
else
:
sample_size
=
(
999
,
50
)
default_rtol
=
.
01
sample_size_odd
=
(
sample_size
[
0
],
sample_size
[
1
]
-
1
)
x
=
tensor
.
matrix
()
test_cases
=
[
(
sample_size
,
sample_size
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
(
x
.
shape
,
sample_size
,
[
x
],
[
np
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
# test odd value
(
x
.
shape
,
sample_size_odd
,
[
x
],
[
np
.
zeros
(
sample_size_odd
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
(
sample_size
,
sample_size
,
[],
[],
np
.
arange
(
np
.
prod
(
sample_size
),
dtype
=
'float32'
)
.
reshape
(
sample_size
),
10.
*
std
/
np
.
sqrt
(
steps
),
default_rtol
),
# test empty size (scalar)
((),
(),
[],
[],
-
5.
,
default_rtol
,
0.02
),
# test with few samples at the same time
((
1
,),
(
1
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
((
3
,),
(
3
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
]
for
size
,
const_size
,
var_input
,
input
,
avg
,
rtol
,
std_tol
in
test_cases
:
R
=
MRG_RandomStreams
(
234
)
# Note: we specify `nstreams` to avoid a warning.
n
=
R
.
normal
(
size
=
size
,
avg
=
avg
,
std
=
std
,
truncate
=
True
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
f
=
theano
.
function
(
var_input
,
n
)
# check if truncated at 2*std
samples
=
f
(
*
input
)
assert
np
.
all
(
avg
+
2
*
std
-
samples
>=
0
),
\
(
"bad upper bound?
%
s
%
s"
%
(
samples
,
avg
+
2
*
std
))
assert
np
.
all
(
samples
-
(
avg
-
2
*
std
)
>=
0
),
\
(
"bad lower bound?
%
s
%
s"
%
(
samples
,
avg
-
2
*
std
))
# Increase the number of steps if size implies only a few samples
if
np
.
prod
(
const_size
)
<
10
:
steps_
=
steps
*
50
else
:
steps_
=
steps
basictest
(
f
,
steps_
,
const_size
,
target_avg
=
avg
,
target_std
=
target_std
,
prefix
=
'mrg '
,
allow_01
=
True
,
inputs
=
input
,
mean_rtol
=
rtol
,
std_tol
=
std_tol
)
sys
.
stdout
.
flush
()
@attr
(
'slow'
)
def
test_truncated_normal
():
# just a copy of test_normal0 for truncated normal
steps
=
50
std
=
2.
if
(
config
.
mode
in
[
'DEBUG_MODE'
,
'DebugMode'
,
'FAST_COMPILE'
]
or
config
.
mode
==
'Mode'
and
config
.
linker
in
[
'py'
]):
sample_size
=
(
25
,
30
)
default_rtol
=
.
02
else
:
sample_size
=
(
999
,
50
)
default_rtol
=
.
01
sample_size_odd
=
(
sample_size
[
0
],
sample_size
[
1
]
-
1
)
x
=
tensor
.
matrix
()
test_cases
=
[
(
sample_size
,
sample_size
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
(
x
.
shape
,
sample_size
,
[
x
],
[
np
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
# test odd value
(
x
.
shape
,
sample_size_odd
,
[
x
],
[
np
.
zeros
(
sample_size_odd
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
(
sample_size
,
sample_size
,
[],
[],
np
.
arange
(
np
.
prod
(
sample_size
),
dtype
=
'float32'
)
.
reshape
(
sample_size
),
10.
*
std
/
np
.
sqrt
(
steps
),
default_rtol
),
# test empty size (scalar)
((),
(),
[],
[],
-
5.
,
default_rtol
,
0.02
),
# test with few samples at the same time
((
1
,),
(
1
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
((
3
,),
(
3
,),
[],
[],
-
5.
,
default_rtol
,
0.02
),
]
for
size
,
const_size
,
var_input
,
input
,
avg
,
rtol
,
std_tol
in
test_cases
:
R
=
MRG_RandomStreams
(
234
)
# Note: we specify `nstreams` to avoid a warning.
n
=
R
.
truncated_normal
(
size
=
size
,
avg
=
avg
,
std
=
std
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
f
=
theano
.
function
(
var_input
,
n
)
# Increase the number of steps if size implies only a few samples
if
np
.
prod
(
const_size
)
<
10
:
steps_
=
steps
*
60
else
:
steps_
=
steps
basictest
(
f
,
steps_
,
const_size
,
target_avg
=
avg
,
target_std
=
std
,
prefix
=
'mrg '
,
allow_01
=
True
,
inputs
=
input
,
mean_rtol
=
rtol
,
std_tol
=
std_tol
)
sys
.
stdout
.
flush
()
def
basic_multinomialtest
(
f
,
steps
,
sample_size
,
target_pvals
,
n_samples
,
def
basic_multinomialtest
(
f
,
steps
,
sample_size
,
target_pvals
,
n_samples
,
prefix
=
""
,
mean_rtol
=
0.04
):
prefix
=
""
,
mean_rtol
=
0.04
):
...
@@ -519,6 +640,7 @@ class T_MRG(unittest.TestCase):
...
@@ -519,6 +640,7 @@ class T_MRG(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
R
.
binomial
,
size
)
self
.
assertRaises
(
ValueError
,
R
.
binomial
,
size
)
self
.
assertRaises
(
ValueError
,
R
.
multinomial
,
size
,
1
,
[])
self
.
assertRaises
(
ValueError
,
R
.
multinomial
,
size
,
1
,
[])
self
.
assertRaises
(
ValueError
,
R
.
normal
,
size
)
self
.
assertRaises
(
ValueError
,
R
.
normal
,
size
)
self
.
assertRaises
(
ValueError
,
R
.
truncated_normal
,
size
)
def
test_multiple_rng_aliasing
():
def
test_multiple_rng_aliasing
():
...
@@ -734,6 +856,19 @@ def test_undefined_grad():
...
@@ -734,6 +856,19 @@ def test_undefined_grad():
assert_raises
(
theano
.
gradient
.
NullTypeGradError
,
theano
.
grad
,
out
,
assert_raises
(
theano
.
gradient
.
NullTypeGradError
,
theano
.
grad
,
out
,
(
avg
,
std
))
(
avg
,
std
))
# checking truncated normal distribution
avg
=
tensor
.
scalar
()
out
=
srng
.
truncated_normal
((),
avg
=
avg
)
assert_raises
(
theano
.
gradient
.
NullTypeGradError
,
theano
.
grad
,
out
,
avg
)
std
=
tensor
.
scalar
()
out
=
srng
.
truncated_normal
((),
avg
=
0
,
std
=
std
)
assert_raises
(
theano
.
gradient
.
NullTypeGradError
,
theano
.
grad
,
out
,
std
)
out
=
srng
.
truncated_normal
((),
avg
=
avg
,
std
=
std
)
assert_raises
(
theano
.
gradient
.
NullTypeGradError
,
theano
.
grad
,
out
,
(
avg
,
std
))
def
test_f16_nonzero
(
mode
=
None
,
op_to_check
=
rng_mrg
.
mrg_uniform
):
def
test_f16_nonzero
(
mode
=
None
,
op_to_check
=
rng_mrg
.
mrg_uniform
):
srng
=
MRG_RandomStreams
(
seed
=
utt
.
fetch_seed
())
srng
=
MRG_RandomStreams
(
seed
=
utt
.
fetch_seed
())
...
@@ -754,6 +889,8 @@ def test_target_parameter():
...
@@ -754,6 +889,8 @@ def test_target_parameter():
assert
isinstance
(
f
(),
np
.
ndarray
)
assert
isinstance
(
f
(),
np
.
ndarray
)
basic_target_parameter_test
(
srng
.
uniform
((
3
,
2
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
uniform
((
3
,
2
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
normal
((
3
,
2
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
truncated_normal
((
3
,
2
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
binomial
((
3
,
2
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
binomial
((
3
,
2
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
multinomial
(
pvals
=
pvals
.
astype
(
'float32'
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
multinomial
(
pvals
=
pvals
.
astype
(
'float32'
),
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
choice
(
p
=
pvals
.
astype
(
'float32'
),
replace
=
False
,
target
=
'cpu'
))
basic_target_parameter_test
(
srng
.
choice
(
p
=
pvals
.
astype
(
'float32'
),
replace
=
False
,
target
=
'cpu'
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论