Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6c4b2fca
提交
6c4b2fca
authored
12月 13, 2009
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tests for all distributions of raw_random and randomstreams.
Fix #236.
上级
63471cda
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
399 行增加
和
0 行删除
+399
-0
test_randomstreams.py
theano/tensor/tests/test_randomstreams.py
+170
-0
test_raw_random.py
theano/tensor/tests/test_raw_random.py
+229
-0
没有找到文件。
theano/tensor/tests/test_randomstreams.py
浏览文件 @
6c4b2fca
...
@@ -6,6 +6,7 @@ import numpy
...
@@ -6,6 +6,7 @@ import numpy
from
theano.tensor.randomstreams
import
RandomStreams
,
raw_random
from
theano.tensor.randomstreams
import
RandomStreams
,
raw_random
from
theano.compile
import
Module
,
Method
,
Member
from
theano.compile
import
Module
,
Method
,
Member
from
theano.tests
import
unittest_tools
from
theano
import
tensor
from
theano
import
tensor
from
theano
import
compile
,
gof
from
theano
import
compile
,
gof
...
@@ -137,6 +138,175 @@ class T_RandomStreams(unittest.TestCase):
...
@@ -137,6 +138,175 @@ class T_RandomStreams(unittest.TestCase):
assert
m
.
random
is
m
.
m2
.
random
assert
m
.
random
is
m
.
m2
.
random
def
test_ndim
(
self
):
m1
=
Module
()
m1
.
random
=
RandomStreams
(
234
)
m1
.
fn
=
Method
([],
m1
.
random
.
uniform
((
2
,
2
)))
made1
=
m1
.
make
()
made1
.
random
.
initialize
()
m2
=
Module
()
m2
.
random
=
RandomStreams
(
234
)
m2
.
fn
=
Method
([],
m2
.
random
.
uniform
(
2
,
(
2
,
2
)))
made2
=
m2
.
make
()
made2
.
random
.
initialize
()
val1
=
made1
.
fn
()
val2
=
made2
.
fn
()
assert
numpy
.
all
(
val1
==
val2
)
m3
=
Module
()
m3
.
random
=
RandomStreams
(
234
)
m3
.
fn
=
Method
([],
m3
.
random
.
uniform
(
1
,
(
2
,
2
)))
made3
=
m3
.
make
()
made3
.
random
.
initialize
()
self
.
assertRaises
(
ValueError
,
made3
.
fn
)
def
test_uniform
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
uniform
((
2
,
2
),
-
1
,
1
))
made
=
m
.
make
()
made
.
random
.
initialize
()
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
print
fn_val0
print
fn_val1
rng_seed
=
numpy
.
random
.
RandomState
(
234
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
numpy_val0
=
rng
.
uniform
(
-
1
,
1
,
size
=
(
2
,
2
))
numpy_val1
=
rng
.
uniform
(
-
1
,
1
,
size
=
(
2
,
2
))
print
numpy_val0
print
numpy_val1
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_normal
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
normal
((
2
,
2
),
-
1
,
2
))
made
=
m
.
make
()
made
.
random
.
initialize
()
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
rng_seed
=
numpy
.
random
.
RandomState
(
234
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
numpy_val0
=
rng
.
normal
(
-
1
,
2
,
size
=
(
2
,
2
))
numpy_val1
=
rng
.
normal
(
-
1
,
2
,
size
=
(
2
,
2
))
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_random_integers
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
random_integers
((
20
,
20
),
-
5
,
5
))
made
=
m
.
make
()
made
.
random
.
initialize
()
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
rng_seed
=
numpy
.
random
.
RandomState
(
234
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
numpy_val0
=
rng
.
random_integers
(
-
5
,
5
,
size
=
(
20
,
20
))
numpy_val1
=
rng
.
random_integers
(
-
5
,
5
,
size
=
(
20
,
20
))
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_permutation
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
permutation
((
20
,),
10
))
made
=
m
.
make
()
made
.
random
.
initialize
()
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
rng_seed
=
numpy
.
random
.
RandomState
(
234
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
numpy_val0
=
numpy
.
asarray
([
rng
.
permutation
(
10
)
for
i
in
range
(
20
)])
numpy_val1
=
numpy
.
asarray
([
rng
.
permutation
(
10
)
for
i
in
range
(
20
)])
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_multinomial
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
multinomial
((
20
,
20
),
1
,
[
0.1
]
*
10
))
made
=
m
.
make
()
made
.
random
.
initialize
()
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
rng_seed
=
numpy
.
random
.
RandomState
(
234
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
numpy_val0
=
rng
.
multinomial
(
1
,
[
0.1
]
*
10
,
size
=
(
20
,
20
))
numpy_val1
=
rng
.
multinomial
(
1
,
[
0.1
]
*
10
,
size
=
(
20
,
20
))
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_shuffle_row_elements
(
self
):
mm
=
Module
()
mm
.
random
=
RandomStreams
(
234
)
m_input
=
tensor
.
dmatrix
()
mm
.
f
=
Method
([
m_input
],
mm
.
random
.
shuffle_row_elements
(
m_input
))
mmade
=
mm
.
make
()
mmade
.
random
.
initialize
()
val_rng
=
numpy
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
in_mval
=
val_rng
.
uniform
(
-
2
,
2
,
size
=
(
20
,
5
))
fn_mval0
=
mmade
.
f
(
in_mval
)
fn_mval1
=
mmade
.
f
(
in_mval
)
print
in_mval
[
0
]
print
fn_mval0
[
0
]
print
fn_mval1
[
0
]
assert
not
numpy
.
all
(
in_mval
==
fn_mval0
)
assert
not
numpy
.
all
(
in_mval
==
fn_mval1
)
assert
not
numpy
.
all
(
fn_mval0
==
fn_mval1
)
rng_seed
=
numpy
.
random
.
RandomState
(
234
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
numpy_mval0
=
in_mval
.
copy
()
numpy_mval1
=
in_mval
.
copy
()
for
row
in
numpy_mval0
:
rng
.
shuffle
(
row
)
for
row
in
numpy_mval1
:
rng
.
shuffle
(
row
)
assert
numpy
.
all
(
numpy_mval0
==
fn_mval0
)
assert
numpy
.
all
(
numpy_mval1
==
fn_mval1
)
vm
=
Module
()
vm
.
random
=
RandomStreams
(
234
)
v_input
=
tensor
.
dvector
()
vm
.
f
=
Method
([
v_input
],
vm
.
random
.
shuffle_row_elements
(
v_input
))
vmade
=
vm
.
make
()
vmade
.
random
.
initialize
()
in_vval
=
val_rng
.
uniform
(
-
3
,
3
,
size
=
(
12
,))
fn_vval
=
vmade
.
f
(
in_vval
)
numpy_vval
=
in_vval
.
copy
()
vrng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
vrng
.
shuffle
(
numpy_vval
)
print
in_vval
print
fn_vval
print
numpy_vval
assert
numpy
.
all
(
numpy_vval
==
fn_vval
)
self
.
assertRaises
(
TypeError
,
vmade
.
f
,
in_mval
)
self
.
assertRaises
(
TypeError
,
mmade
.
f
,
in_vval
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
theano/tensor/tests/test_raw_random.py
浏览文件 @
6c4b2fca
...
@@ -90,6 +90,235 @@ class T_random_function(unittest.TestCase):
...
@@ -90,6 +90,235 @@ class T_random_function(unittest.TestCase):
assert
not
numpy
.
allclose
(
val0
,
val1
)
assert
not
numpy
.
allclose
(
val0
,
val1
)
def
test_random_function_ndim
(
self
):
"""Test that random_function helper function accepts ndim as first argument"""
rf2
=
random_function
(
numpy
.
random
.
RandomState
.
uniform
,
'float64'
,
-
2.0
,
2.0
)
rng_R
=
random_state_type
()
post_out4
,
out4
=
rf2
(
rng_R
,
(
4
,))
post_out1_4
,
out1_4
=
rf2
(
rng_R
,
1
,
(
4
,))
post_out2_4_4
,
out2_4_4
=
rf2
(
rng_R
,
2
,
(
4
,
4
))
post_out2_4
,
out2_4
=
rf2
(
rng_R
,
2
,
(
4
,))
f_ok
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_out2_4_4
,
mutable
=
True
)],
[
out4
,
out1_4
,
out2_4_4
],
accept_inplace
=
True
)
f_no
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_out2_4
,
mutable
=
True
)],
[
out2_4
],
accept_inplace
=
True
)
o4
,
o1_4
,
o2_4_4
=
f_ok
()
self
.
assertTrue
(
numpy
.
allclose
(
o4
,
o1_4
))
self
.
assertTrue
(
numpy
.
allclose
(
o4
,
o2_4_4
[
0
]))
self
.
assertRaises
(
ValueError
,
f_no
)
def
test_random_function_ndim_added
(
self
):
"""Test that random_function helper function accepts ndim_added as keyword argument"""
# On a uniform distribution, ndim_added=-1 means that the shape
# provided should be one dimension bigger, and its last value
# will be ignored
uni_1
=
random_function
(
numpy
.
random
.
RandomState
.
uniform
,
'float64'
,
-
2.0
,
2.0
,
ndim_added
=
1
)
uni_0
=
random_function
(
numpy
.
random
.
RandomState
.
uniform
,
'float64'
,
-
2.0
,
2.0
,
ndim_added
=
0
)
uni_m1
=
random_function
(
numpy
.
random
.
RandomState
.
uniform
,
'float64'
,
-
2.0
,
2.0
,
ndim_added
=-
1
)
rng_R
=
random_state_type
()
p_uni11
,
uni11
=
uni_1
(
rng_R
,
1
,
(
4
,))
p_uni12
,
uni12
=
uni_1
(
rng_R
,
2
,
(
3
,
4
))
p_uni01
,
uni01
=
uni_0
(
rng_R
,
1
,
(
4
,))
p_uni02
,
uni02
=
uni_0
(
rng_R
,
2
,
(
3
,
4
))
p_unim11
,
unim11
=
uni_m1
(
rng_R
,
1
,
(
4
,))
p_unim12
,
unim12
=
uni_m1
(
rng_R
,
2
,
(
3
,
4
))
self
.
assertEqual
(
uni11
.
ndim
,
2
)
self
.
assertEqual
(
uni12
.
ndim
,
3
)
self
.
assertEqual
(
uni01
.
ndim
,
1
)
self
.
assertEqual
(
uni02
.
ndim
,
2
)
self
.
assertEqual
(
unim11
.
ndim
,
0
)
self
.
assertEqual
(
unim12
.
ndim
,
1
)
f11
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
p_uni11
,
mutable
=
True
)],
[
uni11
],
accept_inplace
=
True
)
f12
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
p_uni12
,
mutable
=
True
)],
[
uni12
],
accept_inplace
=
True
)
fm11
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
p_unim11
,
mutable
=
True
)],
[
unim11
],
accept_inplace
=
True
)
fm12
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
p_unim12
,
mutable
=
True
)],
[
unim12
],
accept_inplace
=
True
)
f0
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
p_uni02
,
mutable
=
True
)],
[
uni01
,
uni02
],
accept_inplace
=
True
)
self
.
assertRaises
(
ValueError
,
f11
)
self
.
assertRaises
(
ValueError
,
f12
)
self
.
assertRaises
(
ValueError
,
fm11
)
self
.
assertRaises
(
ValueError
,
fm12
)
u01
,
u02
=
f0
()
print
u01
print
u02
self
.
assertTrue
(
numpy
.
allclose
(
u01
,
u02
[
0
]))
def
test_uniform
(
self
):
rng_R
=
random_state_type
()
post_r
,
out
=
uniform
(
rng_R
,
(
4
,),
-
2.0
,
2.0
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
55
)
val0
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
uniform
(
-
2.0
,
2.0
,
size
=
(
4
,))
numpy_val1
=
numpy_rng
.
uniform
(
-
2.0
,
2.0
,
size
=
(
4
,))
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
numpy
.
allclose
(
val1
,
numpy_val1
))
def
test_binomial
(
self
):
rng_R
=
random_state_type
()
post_r
,
bin
=
binomial
(
rng_R
,
(
7
,
12
),
5
,
0.8
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r
,
mutable
=
True
)],
[
bin
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
55
)
val0
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
binomial
(
5
,
0.8
,
size
=
(
7
,
12
))
numpy_val1
=
numpy_rng
.
binomial
(
5
,
0.8
,
size
=
(
7
,
12
))
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
numpy
.
all
(
val1
==
numpy_val1
))
def
test_normal
(
self
):
rng_R
=
random_state_type
()
post_r
,
out
=
normal
(
rng_R
,
(
2
,
3
),
4.0
,
2.0
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
55
)
val0
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
normal
(
4.0
,
2.0
,
size
=
(
2
,
3
))
numpy_val1
=
numpy_rng
.
normal
(
4.0
,
2.0
,
size
=
(
2
,
3
))
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
numpy
.
allclose
(
val1
,
numpy_val1
))
def
test_random_integers
(
self
):
rng_R
=
random_state_type
()
post_r
,
out
=
random_integers
(
rng_R
,
(
11
,
8
),
-
3
,
16
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
55
)
val0
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
random_integers
(
-
3
,
16
,
size
=
(
11
,
8
))
numpy_val1
=
numpy_rng
.
random_integers
(
-
3
,
16
,
size
=
(
11
,
8
))
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
numpy
.
allclose
(
val1
,
numpy_val1
))
def
test_permutation_helper
(
self
):
rf
=
RandomFunction
(
permutation_helper
,
tensor
.
imatrix
,
8
,
ndim_added
=
1
)
rng_R
=
random_state_type
()
post_r
,
out
=
rf
(
rng_R
,
(
7
,),
8
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
55
)
val0
=
f
()
val1
=
f
()
numpy_val0
=
numpy
.
asarray
([
numpy_rng
.
permutation
(
8
)
for
i
in
range
(
7
)])
numpy_val1
=
numpy
.
asarray
([
numpy_rng
.
permutation
(
8
)
for
i
in
range
(
7
)])
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
numpy
.
all
(
val1
==
numpy_val1
))
rf0
=
RandomFunction
(
permutation_helper
,
tensor
.
imatrix
,
8
)
post_r0
,
out0
=
rf0
(
rng_R
,
(
7
,),
8
)
f0
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r0
,
mutable
=
True
)],
[
out0
],
accept_inplace
=
True
)
self
.
assertRaises
(
ValueError
,
f0
)
rf2
=
RandomFunction
(
permutation_helper
,
tensor
.
imatrix
,
8
,
ndim_added
=
2
)
post_r2
,
out2
=
rf2
(
rng_R
,
(
7
,),
8
)
f2
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r2
,
mutable
=
True
)],
[
out2
],
accept_inplace
=
True
)
self
.
assertRaises
(
ValueError
,
f2
)
def
test_permutation
(
self
):
rng_R
=
random_state_type
()
post_r
,
out
=
permutation
(
rng_R
,
(
9
,),
6
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
55
)
val0
=
f
()
val1
=
f
()
numpy_val0
=
numpy
.
asarray
([
numpy_rng
.
permutation
(
6
)
for
i
in
range
(
9
)])
numpy_val1
=
numpy
.
asarray
([
numpy_rng
.
permutation
(
6
)
for
i
in
range
(
9
)])
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
numpy
.
all
(
val1
==
numpy_val1
))
def
test_multinomial
(
self
):
rng_R
=
random_state_type
()
post_r
,
out
=
multinomial
(
rng_R
,
(
7
,
3
),
6
,
[
0.2
]
*
5
)
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
numpy_rng
=
numpy
.
random
.
RandomState
(
55
)
val0
,
=
f
()
val1
,
=
f
()
numpy_val0
=
numpy_rng
.
multinomial
(
6
,
[
0.2
]
*
5
,
(
7
,
3
))
numpy_val1
=
numpy_rng
.
multinomial
(
6
,
[
0.2
]
*
5
,
(
7
,
3
))
print
val0
print
numpy_val0
print
val1
print
numpy_val1
self
.
assertTrue
(
numpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
numpy
.
all
(
val1
==
numpy_val1
))
self
.
assertTrue
(
val0
.
shape
==
(
7
,
3
,
5
))
self
.
assertTrue
(
val1
.
shape
==
(
7
,
3
,
5
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
from
theano.tests
import
main
from
theano.tests
import
main
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论