Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7e05e00e
提交
7e05e00e
authored
3月 24, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1. Updated numpy as np
2. Fixed indentation
上级
3c237906
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
145 行增加
和
145 行删除
+145
-145
test_raw_random.py
theano/tensor/tests/test_raw_random.py
+145
-145
没有找到文件。
theano/tensor/tests/test_raw_random.py
浏览文件 @
7e05e00e
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
pickle
import
pickle
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
...
@@ -19,7 +19,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -19,7 +19,7 @@ class T_random_function(utt.InferShapeTester):
utt
.
seed_rng
()
utt
.
seed_rng
()
def
test_basic_usage
(
self
):
def
test_basic_usage
(
self
):
rf
=
RandomFunction
(
n
umpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
)
rf
=
RandomFunction
(
n
p
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
)
assert
not
rf
.
inplace
assert
not
rf
.
inplace
assert
getattr
(
rf
,
'destroy_map'
,
{})
==
{}
assert
getattr
(
rf
,
'destroy_map'
,
{})
==
{}
...
@@ -33,23 +33,23 @@ class T_random_function(utt.InferShapeTester):
...
@@ -33,23 +33,23 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
([
rng_R
],
out
)
f
=
compile
.
function
([
rng_R
],
out
)
rng_state0
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_state0
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
f_0
=
f
(
rng_state0
)
f_0
=
f
(
rng_state0
)
f_1
=
f
(
rng_state0
)
f_1
=
f
(
rng_state0
)
assert
n
umpy
.
all
(
f_0
==
f_1
)
assert
n
p
.
all
(
f_0
==
f_1
)
def
test_inplace_norun
(
self
):
def
test_inplace_norun
(
self
):
rf
=
RandomFunction
(
n
umpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
,
rf
=
RandomFunction
(
n
p
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
,
inplace
=
True
)
inplace
=
True
)
assert
rf
.
inplace
assert
rf
.
inplace
assert
getattr
(
rf
,
'destroy_map'
,
{})
!=
{}
assert
getattr
(
rf
,
'destroy_map'
,
{})
!=
{}
def
test_args
(
self
):
def
test_args
(
self
):
"""Test that arguments to RandomFunction are honored"""
"""Test that arguments to RandomFunction are honored"""
rf2
=
RandomFunction
(
n
umpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
)
rf2
=
RandomFunction
(
n
p
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
)
rf4
=
RandomFunction
(
n
umpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
,
rf4
=
RandomFunction
(
n
p
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
,
inplace
=
True
)
inplace
=
True
)
rng_R
=
random_state_type
()
rng_R
=
random_state_type
()
...
@@ -64,7 +64,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -64,7 +64,7 @@ class T_random_function(utt.InferShapeTester):
# be maintained by post_r4
# be maintained by post_r4
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r4
,
update
=
post_r4
,
mutable
=
True
)],
mutable
=
True
)],
[
out2
,
out4
,
out2_4
,
out2_4_4
],
[
out2
,
out4
,
out2_4
,
out2_4_4
],
...
@@ -84,19 +84,19 @@ class T_random_function(utt.InferShapeTester):
...
@@ -84,19 +84,19 @@ class T_random_function(utt.InferShapeTester):
# print f2_4_4b
# print f2_4_4b
# setting bounds is same as multiplying by 2
# setting bounds is same as multiplying by 2
assert
n
umpy
.
allclose
(
f2
*
2
,
f4
),
(
f2
,
f4
)
assert
n
p
.
allclose
(
f2
*
2
,
f4
),
(
f2
,
f4
)
# retrieving from non-inplace generator
# retrieving from non-inplace generator
# is same as inplace one for first call
# is same as inplace one for first call
assert
n
umpy
.
allclose
(
f2_4_4
,
f4
),
(
f2_4_4
,
f4
)
assert
n
p
.
allclose
(
f2_4_4
,
f4
),
(
f2_4_4
,
f4
)
# f4 changes from call to call, that the update has worked
# f4 changes from call to call, that the update has worked
assert
not
n
umpy
.
allclose
(
f4
,
f4b
),
(
f4
,
f4b
)
assert
not
n
p
.
allclose
(
f4
,
f4b
),
(
f4
,
f4b
)
def
test_inplace_optimization
(
self
):
def
test_inplace_optimization
(
self
):
"""Test that FAST_RUN includes the random_make_inplace optimization"""
"""Test that FAST_RUN includes the random_make_inplace optimization"""
#inplace = False
#inplace = False
rf2
=
RandomFunction
(
n
umpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
)
rf2
=
RandomFunction
(
n
p
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
)
rng_R
=
random_state_type
()
rng_R
=
random_state_type
()
# If calling RandomFunction directly, all args have to be specified,
# If calling RandomFunction directly, all args have to be specified,
...
@@ -105,7 +105,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -105,7 +105,7 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r2
,
update
=
post_r2
,
mutable
=
True
)],
mutable
=
True
)],
out2
,
out2
,
...
@@ -121,7 +121,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -121,7 +121,7 @@ class T_random_function(utt.InferShapeTester):
val1
=
f
()
val1
=
f
()
assert
id0
==
id
(
f
[
rng_R
])
assert
id0
==
id
(
f
[
rng_R
])
assert
not
n
umpy
.
allclose
(
val0
,
val1
)
assert
not
n
p
.
allclose
(
val0
,
val1
)
def
test_no_inplace
(
self
):
def
test_no_inplace
(
self
):
"""Test that when not running inplace, the RandomState is
"""Test that when not running inplace, the RandomState is
...
@@ -131,10 +131,10 @@ class T_random_function(utt.InferShapeTester):
...
@@ -131,10 +131,10 @@ class T_random_function(utt.InferShapeTester):
post_r
,
out
=
rf
(
rng_R
,
(
3
,),
0.
,
1.
)
post_r
,
out
=
rf
(
rng_R
,
(
3
,),
0.
,
1.
)
f
=
compile
.
function
([
rng_R
],
[
post_r
,
out
])
f
=
compile
.
function
([
rng_R
],
[
post_r
,
out
])
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng0
,
val0
=
f
(
rng
)
rng0
,
val0
=
f
(
rng
)
rng_
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# rng should still be in a fresh state
# rng should still be in a fresh state
self
.
assertTrue
(
rng_R
.
type
.
values_eq
(
rng
,
rng_
))
self
.
assertTrue
(
rng_R
.
type
.
values_eq
(
rng
,
rng_
))
# rng0 should be in an updated state
# rng0 should be in an updated state
...
@@ -178,7 +178,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -178,7 +178,7 @@ class T_random_function(utt.InferShapeTester):
f_ok
=
compile
.
function
(
f_ok
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_out2_4_4
,
update
=
post_out2_4_4
,
mutable
=
True
)],
mutable
=
True
)],
[
out4
,
out1_4
,
out2_4_4
],
[
out4
,
out1_4
,
out2_4_4
],
...
@@ -188,8 +188,8 @@ class T_random_function(utt.InferShapeTester):
...
@@ -188,8 +188,8 @@ class T_random_function(utt.InferShapeTester):
o4
,
o1_4
,
o2_4_4
=
f_ok
()
o4
,
o1_4
,
o2_4_4
=
f_ok
()
# Check the sanity of the answers
# Check the sanity of the answers
self
.
assertTrue
(
n
umpy
.
allclose
(
o4
,
o1_4
))
self
.
assertTrue
(
n
p
.
allclose
(
o4
,
o1_4
))
self
.
assertTrue
(
n
umpy
.
allclose
(
o4
,
o2_4_4
[
0
]))
self
.
assertTrue
(
n
p
.
allclose
(
o4
,
o2_4_4
[
0
]))
def
test_random_function_noshape_args
(
self
):
def
test_random_function_noshape_args
(
self
):
'''Test if random_function helper works with args but without shape'''
'''Test if random_function helper works with args but without shape'''
...
@@ -199,7 +199,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -199,7 +199,7 @@ class T_random_function(utt.InferShapeTester):
post_out
,
out
=
uniform
(
rng_R
,
size
=
None
,
ndim
=
2
)
post_out
,
out
=
uniform
(
rng_R
,
size
=
None
,
ndim
=
2
)
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_out
,
update
=
post_out
,
mutable
=
True
)],
mutable
=
True
)],
[
out
],
[
out
],
...
@@ -219,7 +219,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -219,7 +219,7 @@ class T_random_function(utt.InferShapeTester):
[
low
,
[
low
,
high
,
high
,
compile
.
In
(
rng_R
,
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_out2
,
update
=
post_out2
,
mutable
=
True
)],
mutable
=
True
)],
[
out2
],
[
out2
],
...
@@ -242,7 +242,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -242,7 +242,7 @@ class T_random_function(utt.InferShapeTester):
# If using numpy's uniform distribution, ndim_added should be 0,
# If using numpy's uniform distribution, ndim_added should be 0,
# because the shape provided as argument is the output shape.
# because the shape provided as argument is the output shape.
# Specifying a different ndim_added will change the Op's output ndim,
# Specifying a different ndim_added will change the Op's output ndim,
# so n
umpy
.uniform will produce a result of incorrect shape,
# so n
p
.uniform will produce a result of incorrect shape,
# and a ValueError should be raised.
# and a ValueError should be raised.
def
ndim_added_deco
(
ndim_added
):
def
ndim_added_deco
(
ndim_added
):
def
randomfunction
(
random_state
,
size
=
(),
low
=
0.0
,
high
=
0.0
,
def
randomfunction
(
random_state
,
size
=
(),
low
=
0.0
,
high
=
0.0
,
...
@@ -282,27 +282,27 @@ class T_random_function(utt.InferShapeTester):
...
@@ -282,27 +282,27 @@ class T_random_function(utt.InferShapeTester):
f11
=
compile
.
function
(
f11
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
p_uni11
,
mutable
=
True
)],
update
=
p_uni11
,
mutable
=
True
)],
[
uni11
],
accept_inplace
=
True
)
[
uni11
],
accept_inplace
=
True
)
f12
=
compile
.
function
(
f12
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
p_uni12
,
mutable
=
True
)],
update
=
p_uni12
,
mutable
=
True
)],
[
uni12
],
accept_inplace
=
True
)
[
uni12
],
accept_inplace
=
True
)
fm11
=
compile
.
function
(
fm11
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
p_unim11
,
mutable
=
True
)],
update
=
p_unim11
,
mutable
=
True
)],
[
unim11
],
accept_inplace
=
True
)
[
unim11
],
accept_inplace
=
True
)
fm12
=
compile
.
function
(
fm12
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
p_unim12
,
mutable
=
True
)],
update
=
p_unim12
,
mutable
=
True
)],
[
unim12
],
accept_inplace
=
True
)
[
unim12
],
accept_inplace
=
True
)
f0
=
compile
.
function
(
f0
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
p_uni02
,
mutable
=
True
)],
update
=
p_uni02
,
mutable
=
True
)],
[
uni01
,
uni02
],
accept_inplace
=
True
)
[
uni01
,
uni02
],
accept_inplace
=
True
)
self
.
assertRaises
(
ValueError
,
f11
)
self
.
assertRaises
(
ValueError
,
f11
)
...
@@ -310,7 +310,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -310,7 +310,7 @@ class T_random_function(utt.InferShapeTester):
self
.
assertRaises
(
ValueError
,
fm11
)
self
.
assertRaises
(
ValueError
,
fm11
)
self
.
assertRaises
(
ValueError
,
fm12
)
self
.
assertRaises
(
ValueError
,
fm12
)
u01
,
u02
=
f0
()
u01
,
u02
=
f0
()
self
.
assertTrue
(
n
umpy
.
allclose
(
u01
,
u02
[
0
]))
self
.
assertTrue
(
n
p
.
allclose
(
u01
,
u02
[
0
]))
def
test_uniform
(
self
):
def
test_uniform
(
self
):
"""Test that raw_random.uniform generates the same results as numpy."""
"""Test that raw_random.uniform generates the same results as numpy."""
...
@@ -321,17 +321,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -321,17 +321,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
uniform
(
-
2.0
,
2.0
,
size
=
(
4
,))
numpy_val0
=
numpy_rng
.
uniform
(
-
2.0
,
2.0
,
size
=
(
4
,))
numpy_val1
=
numpy_rng
.
uniform
(
-
2.0
,
2.0
,
size
=
(
4
,))
numpy_val1
=
numpy_rng
.
uniform
(
-
2.0
,
2.0
,
size
=
(
4
,))
self
.
assertTrue
(
n
umpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
p
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val1
,
numpy_val1
))
self
.
assertTrue
(
n
p
.
allclose
(
val1
,
numpy_val1
))
def
test_binomial
(
self
):
def
test_binomial
(
self
):
"""Test that raw_random.binomial generates the same results
"""Test that raw_random.binomial generates the same results
...
@@ -344,17 +344,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -344,17 +344,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
bin
],
accept_inplace
=
True
)
[
bin
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
binomial
(
5
,
0.8
,
size
=
(
7
,
12
))
numpy_val0
=
numpy_rng
.
binomial
(
5
,
0.8
,
size
=
(
7
,
12
))
numpy_val1
=
numpy_rng
.
binomial
(
5
,
0.8
,
size
=
(
7
,
12
))
numpy_val1
=
numpy_rng
.
binomial
(
5
,
0.8
,
size
=
(
7
,
12
))
self
.
assertTrue
(
n
umpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
p
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
all
(
val1
==
numpy_val1
))
self
.
assertTrue
(
n
p
.
all
(
val1
==
numpy_val1
))
def
test_normal
(
self
):
def
test_normal
(
self
):
"""Test that raw_random.normal generates the same results as numpy."""
"""Test that raw_random.normal generates the same results as numpy."""
...
@@ -365,17 +365,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -365,17 +365,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
normal
(
4.0
,
2.0
,
size
=
(
2
,
3
))
numpy_val0
=
numpy_rng
.
normal
(
4.0
,
2.0
,
size
=
(
2
,
3
))
numpy_val1
=
numpy_rng
.
normal
(
4.0
,
2.0
,
size
=
(
2
,
3
))
numpy_val1
=
numpy_rng
.
normal
(
4.0
,
2.0
,
size
=
(
2
,
3
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
p
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val1
,
numpy_val1
))
self
.
assertTrue
(
n
p
.
allclose
(
val1
,
numpy_val1
))
def
test_random_integers
(
self
):
def
test_random_integers
(
self
):
# Test that raw_random.random_integers generates the same
# Test that raw_random.random_integers generates the same
...
@@ -390,17 +390,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -390,17 +390,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
randint
(
-
3
,
17
,
size
=
(
11
,
8
))
numpy_val0
=
numpy_rng
.
randint
(
-
3
,
17
,
size
=
(
11
,
8
))
numpy_val1
=
numpy_rng
.
randint
(
-
3
,
17
,
size
=
(
11
,
8
))
numpy_val1
=
numpy_rng
.
randint
(
-
3
,
17
,
size
=
(
11
,
8
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
p
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val1
,
numpy_val1
))
self
.
assertTrue
(
n
p
.
allclose
(
val1
,
numpy_val1
))
def
test_permutation_helper
(
self
):
def
test_permutation_helper
(
self
):
"""Test that raw_random.permutation_helper generates the same
"""Test that raw_random.permutation_helper generates the same
...
@@ -418,21 +418,21 @@ class T_random_function(utt.InferShapeTester):
...
@@ -418,21 +418,21 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
# numpy_rng.permutation outputs one vector at a time,
# numpy_rng.permutation outputs one vector at a time,
# so we call it iteratively to generate all the samples.
# so we call it iteratively to generate all the samples.
numpy_val0
=
n
umpy
.
asarray
([
numpy_rng
.
permutation
(
8
)
numpy_val0
=
n
p
.
asarray
([
numpy_rng
.
permutation
(
8
)
for
i
in
range
(
7
)])
for
i
in
range
(
7
)])
numpy_val1
=
n
umpy
.
asarray
([
numpy_rng
.
permutation
(
8
)
numpy_val1
=
n
p
.
asarray
([
numpy_rng
.
permutation
(
8
)
for
i
in
range
(
7
)])
for
i
in
range
(
7
)])
self
.
assertTrue
(
n
umpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
p
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
all
(
val1
==
numpy_val1
))
self
.
assertTrue
(
n
p
.
all
(
val1
==
numpy_val1
))
# This call lacks "ndim_added=1", so ndim_added defaults to 0.
# This call lacks "ndim_added=1", so ndim_added defaults to 0.
# A ValueError should be raised.
# A ValueError should be raised.
...
@@ -440,7 +440,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -440,7 +440,7 @@ class T_random_function(utt.InferShapeTester):
post_r0
,
out0
=
rf0
(
rng_R
,
(
7
,),
8
)
post_r0
,
out0
=
rf0
(
rng_R
,
(
7
,),
8
)
f0
=
compile
.
function
(
f0
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r0
,
mutable
=
True
)],
update
=
post_r0
,
mutable
=
True
)],
[
out0
],
accept_inplace
=
True
)
[
out0
],
accept_inplace
=
True
)
self
.
assertRaises
(
ValueError
,
f0
)
self
.
assertRaises
(
ValueError
,
f0
)
...
@@ -451,7 +451,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -451,7 +451,7 @@ class T_random_function(utt.InferShapeTester):
post_r2
,
out2
=
rf2
(
rng_R
,
(
7
,),
8
)
post_r2
,
out2
=
rf2
(
rng_R
,
(
7
,),
8
)
f2
=
compile
.
function
(
f2
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r2
,
mutable
=
True
)],
update
=
post_r2
,
mutable
=
True
)],
[
out2
],
accept_inplace
=
True
)
[
out2
],
accept_inplace
=
True
)
self
.
assertRaises
(
ValueError
,
f2
)
self
.
assertRaises
(
ValueError
,
f2
)
...
@@ -467,17 +467,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -467,17 +467,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
numpy_val0
=
numpy_rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
numpy_val1
=
numpy_rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
numpy_val1
=
numpy_rng
.
choice
(
10
,
(
11
,
8
),
True
,
None
)
self
.
assertTrue
(
n
umpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
p
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val1
,
numpy_val1
))
self
.
assertTrue
(
n
p
.
allclose
(
val1
,
numpy_val1
))
def
test_poisson
(
self
):
def
test_poisson
(
self
):
"""Test that raw_random.poisson generates the same
"""Test that raw_random.poisson generates the same
...
@@ -490,17 +490,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -490,17 +490,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
numpy_val0
=
numpy_rng
.
poisson
(
5
,
size
=
(
11
,
8
))
numpy_val0
=
numpy_rng
.
poisson
(
5
,
size
=
(
11
,
8
))
numpy_val1
=
numpy_rng
.
poisson
(
5
,
size
=
(
11
,
8
))
numpy_val1
=
numpy_rng
.
poisson
(
5
,
size
=
(
11
,
8
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
p
.
allclose
(
val0
,
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
allclose
(
val1
,
numpy_val1
))
self
.
assertTrue
(
n
p
.
allclose
(
val1
,
numpy_val1
))
def
test_permutation
(
self
):
def
test_permutation
(
self
):
"""Test that raw_random.permutation generates the same
"""Test that raw_random.permutation generates the same
...
@@ -509,33 +509,33 @@ class T_random_function(utt.InferShapeTester):
...
@@ -509,33 +509,33 @@ class T_random_function(utt.InferShapeTester):
post_r
,
out
=
permutation
(
rng_R
,
size
=
(
9
,),
n
=
6
)
post_r
,
out
=
permutation
(
rng_R
,
size
=
(
9
,),
n
=
6
)
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Check over two calls to see if the random state is correctly updated.
# Check over two calls to see if the random state is correctly updated.
# numpy_rng.permutation outputs one vector at a time,
# numpy_rng.permutation outputs one vector at a time,
# so we call it iteratively to generate all the samples.
# so we call it iteratively to generate all the samples.
val0
=
f
()
val0
=
f
()
val1
=
f
()
val1
=
f
()
numpy_val0
=
n
umpy
.
asarray
([
numpy_rng
.
permutation
(
6
)
numpy_val0
=
n
p
.
asarray
([
numpy_rng
.
permutation
(
6
)
for
i
in
range
(
9
)])
for
i
in
range
(
9
)])
numpy_val1
=
n
umpy
.
asarray
([
numpy_rng
.
permutation
(
6
)
numpy_val1
=
n
p
.
asarray
([
numpy_rng
.
permutation
(
6
)
for
i
in
range
(
9
)])
for
i
in
range
(
9
)])
self
.
assertTrue
(
n
umpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
p
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
all
(
val1
==
numpy_val1
))
self
.
assertTrue
(
n
p
.
all
(
val1
==
numpy_val1
))
# Test that we can generate a list: have size=None or ().
# Test that we can generate a list: have size=None or ().
for
ndim
in
[
1
,
None
]:
for
ndim
in
[
1
,
None
]:
post_r
,
out
=
permutation
(
rng_R
,
n
=
10
,
size
=
None
,
ndim
=
ndim
)
post_r
,
out
=
permutation
(
rng_R
,
n
=
10
,
size
=
None
,
ndim
=
ndim
)
inp
=
compile
.
In
(
rng_R
,
inp
=
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)
update
=
post_r
,
mutable
=
True
)
f
=
theano
.
function
([
inp
],
out
)
f
=
theano
.
function
([
inp
],
out
)
o
=
f
()
o
=
f
()
assert
o
.
shape
==
(
10
,)
assert
o
.
shape
==
(
10
,)
assert
(
n
umpy
.
sort
(
o
)
==
numpy
.
arange
(
10
))
.
all
()
assert
(
n
p
.
sort
(
o
)
==
np
.
arange
(
10
))
.
all
()
# Wrong number of dimensions asked
# Wrong number of dimensions asked
self
.
assertRaises
(
TypeError
,
permutation
,
rng_R
,
size
=
None
,
ndim
=
2
)
self
.
assertRaises
(
TypeError
,
permutation
,
rng_R
,
size
=
None
,
ndim
=
2
)
...
@@ -548,17 +548,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -548,17 +548,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
(
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
[
compile
.
In
(
rng_R
,
value
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
()),
value
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
()),
update
=
post_r
,
mutable
=
True
)],
update
=
post_r
,
mutable
=
True
)],
[
out
],
accept_inplace
=
True
)
[
out
],
accept_inplace
=
True
)
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
val0
,
=
f
()
val0
,
=
f
()
val1
,
=
f
()
val1
,
=
f
()
numpy_val0
=
numpy_rng
.
multinomial
(
6
,
[
0.2
]
*
5
,
(
7
,
3
))
numpy_val0
=
numpy_rng
.
multinomial
(
6
,
[
0.2
]
*
5
,
(
7
,
3
))
numpy_val1
=
numpy_rng
.
multinomial
(
6
,
[
0.2
]
*
5
,
(
7
,
3
))
numpy_val1
=
numpy_rng
.
multinomial
(
6
,
[
0.2
]
*
5
,
(
7
,
3
))
self
.
assertTrue
(
n
umpy
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
p
.
all
(
val0
==
numpy_val0
))
self
.
assertTrue
(
n
umpy
.
all
(
val1
==
numpy_val1
))
self
.
assertTrue
(
n
p
.
all
(
val1
==
numpy_val1
))
self
.
assertTrue
(
val0
.
shape
==
(
7
,
3
,
5
))
self
.
assertTrue
(
val0
.
shape
==
(
7
,
3
,
5
))
self
.
assertTrue
(
val1
.
shape
==
(
7
,
3
,
5
))
self
.
assertTrue
(
val1
.
shape
==
(
7
,
3
,
5
))
...
@@ -568,7 +568,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -568,7 +568,7 @@ class T_random_function(utt.InferShapeTester):
shape
=
tensor
.
lvector
()
shape
=
tensor
.
lvector
()
post_r
,
out
=
uniform
(
rng_R
,
shape
,
ndim
=
2
)
post_r
,
out
=
uniform
(
rng_R
,
shape
,
ndim
=
2
)
f
=
compile
.
function
([
rng_R
,
shape
],
out
)
f
=
compile
.
function
([
rng_R
,
shape
],
out
)
rng_state0
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_state0
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
assert
f
(
rng_state0
,
[
2
,
3
])
.
shape
==
(
2
,
3
)
assert
f
(
rng_state0
,
[
2
,
3
])
.
shape
==
(
2
,
3
)
assert
f
(
rng_state0
,
[
4
,
8
])
.
shape
==
(
4
,
8
)
assert
f
(
rng_state0
,
[
4
,
8
])
.
shape
==
(
4
,
8
)
...
@@ -583,7 +583,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -583,7 +583,7 @@ class T_random_function(utt.InferShapeTester):
shape
=
(
shape0
,
3
)
shape
=
(
shape0
,
3
)
post_r
,
u
=
uniform
(
rng_R
,
size
=
shape
,
ndim
=
2
)
post_r
,
u
=
uniform
(
rng_R
,
size
=
shape
,
ndim
=
2
)
f
=
compile
.
function
([
rng_R
,
shape0
],
u
)
f
=
compile
.
function
([
rng_R
,
shape0
],
u
)
rng_state0
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_state0
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
assert
f
(
rng_state0
,
2
)
.
shape
==
(
2
,
3
)
assert
f
(
rng_state0
,
2
)
.
shape
==
(
2
,
3
)
assert
f
(
rng_state0
,
8
)
.
shape
==
(
8
,
3
)
assert
f
(
rng_state0
,
8
)
.
shape
==
(
8
,
3
)
...
@@ -601,7 +601,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -601,7 +601,7 @@ class T_random_function(utt.InferShapeTester):
post_r
,
u
=
uniform
(
rng_R
,
size
=
shape
,
ndim
=
2
)
post_r
,
u
=
uniform
(
rng_R
,
size
=
shape
,
ndim
=
2
)
assert
u
.
broadcastable
==
(
False
,
True
)
assert
u
.
broadcastable
==
(
False
,
True
)
f
=
compile
.
function
([
rng_R
,
shape0
],
u
)
f
=
compile
.
function
([
rng_R
,
shape0
],
u
)
rng_state0
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_state0
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
assert
f
(
rng_state0
,
2
)
.
shape
==
(
2
,
1
)
assert
f
(
rng_state0
,
2
)
.
shape
==
(
2
,
1
)
assert
f
(
rng_state0
,
8
)
.
shape
==
(
8
,
1
)
assert
f
(
rng_state0
,
8
)
.
shape
==
(
8
,
1
)
...
@@ -617,25 +617,25 @@ class T_random_function(utt.InferShapeTester):
...
@@ -617,25 +617,25 @@ class T_random_function(utt.InferShapeTester):
post_r
,
out
=
uniform
(
rng_R
)
post_r
,
out
=
uniform
(
rng_R
)
f
=
compile
.
function
([
rng_R
],
[
post_r
,
out
],
accept_inplace
=
True
)
f
=
compile
.
function
([
rng_R
],
[
post_r
,
out
],
accept_inplace
=
True
)
rng_state0
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_state0
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
post0
,
val0
=
f
(
rng_state0
)
post0
,
val0
=
f
(
rng_state0
)
post1
,
val1
=
f
(
post0
)
post1
,
val1
=
f
(
post0
)
numpy_val0
=
n
umpy
.
asarray
(
numpy_rng
.
uniform
(),
numpy_val0
=
n
p
.
asarray
(
numpy_rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
numpy_val1
=
n
umpy
.
asarray
(
numpy_rng
.
uniform
(),
numpy_val1
=
n
p
.
asarray
(
numpy_rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
assert
n
umpy
.
all
(
val0
==
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
)
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
post_r
,
out
=
multinomial
(
rng_R
)
post_r
,
out
=
multinomial
(
rng_R
)
g
=
compile
.
function
([
rng_R
],
[
post_r
,
out
],
accept_inplace
=
True
)
g
=
compile
.
function
([
rng_R
],
[
post_r
,
out
],
accept_inplace
=
True
)
post2
,
val2
=
g
(
post1
)
post2
,
val2
=
g
(
post1
)
numpy_val2
=
n
umpy
.
asarray
(
numpy_rng
.
multinomial
(
n
=
1
,
pvals
=
[
.
5
,
.
5
]),
numpy_val2
=
n
p
.
asarray
(
numpy_rng
.
multinomial
(
n
=
1
,
pvals
=
[
.
5
,
.
5
]),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
assert
n
umpy
.
all
(
val2
==
numpy_val2
)
assert
n
p
.
all
(
val2
==
numpy_val2
)
def
test_vector_arguments
(
self
):
def
test_vector_arguments
(
self
):
rng_R
=
random_state_type
()
rng_R
=
random_state_type
()
...
@@ -645,17 +645,17 @@ class T_random_function(utt.InferShapeTester):
...
@@ -645,17 +645,17 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
([
rng_R
,
low
],
[
post_r
,
out
],
accept_inplace
=
True
)
f
=
compile
.
function
([
rng_R
,
low
],
[
post_r
,
out
],
accept_inplace
=
True
)
def
as_floatX
(
thing
):
def
as_floatX
(
thing
):
return
n
umpy
.
asarray
(
thing
,
dtype
=
theano
.
config
.
floatX
)
return
n
p
.
asarray
(
thing
,
dtype
=
theano
.
config
.
floatX
)
rng_state0
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_state0
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
post0
,
val0
=
f
(
rng_state0
,
[
-
5
,
.
5
,
0
,
1
])
post0
,
val0
=
f
(
rng_state0
,
[
-
5
,
.
5
,
0
,
1
])
post1
,
val1
=
f
(
post0
,
as_floatX
([
.
9
]))
post1
,
val1
=
f
(
post0
,
as_floatX
([
.
9
]))
numpy_val0
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
5
,
.
5
,
0
,
1
],
high
=
1
))
numpy_val0
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
5
,
.
5
,
0
,
1
],
high
=
1
))
numpy_val1
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
as_floatX
([
.
9
]),
high
=
1
))
numpy_val1
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
as_floatX
([
.
9
]),
high
=
1
))
assert
n
umpy
.
all
(
val0
==
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
)
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
high
=
tensor
.
vector
()
high
=
tensor
.
vector
()
post_rb
,
outb
=
uniform
(
rng_R
,
low
=
low
,
high
=
high
)
post_rb
,
outb
=
uniform
(
rng_R
,
low
=
low
,
high
=
high
)
...
@@ -667,8 +667,8 @@ class T_random_function(utt.InferShapeTester):
...
@@ -667,8 +667,8 @@ class T_random_function(utt.InferShapeTester):
post1b
,
val1b
=
fb
(
post0b
,
[
-
4.
],
[
-
1
])
post1b
,
val1b
=
fb
(
post0b
,
[
-
4.
],
[
-
1
])
numpy_val0b
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
,
-
2
],
high
=
[
-
1
,
0
]))
numpy_val0b
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
,
-
2
],
high
=
[
-
1
,
0
]))
numpy_val1b
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
],
high
=
[
-
1
]))
numpy_val1b
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
],
high
=
[
-
1
]))
assert
n
umpy
.
all
(
val0b
==
numpy_val0b
)
assert
n
p
.
all
(
val0b
==
numpy_val0b
)
assert
n
umpy
.
all
(
val1b
==
numpy_val1b
)
assert
n
p
.
all
(
val1b
==
numpy_val1b
)
self
.
assertRaises
(
ValueError
,
fb
,
post1b
,
[
-
4.
,
-
2
],
[
-
1
,
0
,
1
])
self
.
assertRaises
(
ValueError
,
fb
,
post1b
,
[
-
4.
,
-
2
],
[
-
1
,
0
,
1
])
# TODO: do we want that?
# TODO: do we want that?
#self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1])
#self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1])
...
@@ -681,8 +681,8 @@ class T_random_function(utt.InferShapeTester):
...
@@ -681,8 +681,8 @@ class T_random_function(utt.InferShapeTester):
post1c
,
val1c
=
fc
(
post0c
,
[
-
4.
],
[
-
1
],
[
1
])
post1c
,
val1c
=
fc
(
post0c
,
[
-
4.
],
[
-
1
],
[
1
])
numpy_val0c
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
,
-
2
],
high
=
[
-
1
,
0
]))
numpy_val0c
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
,
-
2
],
high
=
[
-
1
,
0
]))
numpy_val1c
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
],
high
=
[
-
1
]))
numpy_val1c
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
[
-
4.
],
high
=
[
-
1
]))
assert
n
umpy
.
all
(
val0c
==
numpy_val0c
)
assert
n
p
.
all
(
val0c
==
numpy_val0c
)
assert
n
umpy
.
all
(
val1c
==
numpy_val1c
)
assert
n
p
.
all
(
val1c
==
numpy_val1c
)
self
.
assertRaises
(
ValueError
,
fc
,
post1c
,
[
-
4.
,
-
2
],
[
-
1
,
0
],
[
1
])
self
.
assertRaises
(
ValueError
,
fc
,
post1c
,
[
-
4.
,
-
2
],
[
-
1
,
0
],
[
1
])
self
.
assertRaises
(
ValueError
,
fc
,
post1c
,
[
-
4.
,
-
2
],
[
-
1
,
0
],
[
1
,
2
])
self
.
assertRaises
(
ValueError
,
fc
,
post1c
,
[
-
4.
,
-
2
],
[
-
1
,
0
],
[
1
,
2
])
self
.
assertRaises
(
ValueError
,
fc
,
post1c
,
[
-
4.
,
-
2
],
[
-
1
,
0
],
[
2
,
1
])
self
.
assertRaises
(
ValueError
,
fc
,
post1c
,
[
-
4.
,
-
2
],
[
-
1
,
0
],
[
2
,
1
])
...
@@ -699,8 +699,8 @@ class T_random_function(utt.InferShapeTester):
...
@@ -699,8 +699,8 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
([
rng_R
,
low
,
high
],
[
post_r
,
out
],
f
=
compile
.
function
([
rng_R
,
low
,
high
],
[
post_r
,
out
],
accept_inplace
=
True
)
accept_inplace
=
True
)
rng_state0
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_state0
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
post0
,
val0
=
f
(
rng_state0
,
[
-
5
,
.
5
,
0
,
1
],
[[
1.
]])
post0
,
val0
=
f
(
rng_state0
,
[
-
5
,
.
5
,
0
,
1
],
[[
1.
]])
post1
,
val1
=
f
(
post0
,
[
.
9
],
[[
1.
],
[
1.1
],
[
1.5
]])
post1
,
val1
=
f
(
post0
,
[
.
9
],
[[
1.
],
[
1.1
],
[
1.5
]])
post2
,
val2
=
f
(
post1
,
[
-
5
,
.
5
,
0
,
1
],
[[
1.
],
[
1.1
],
[
1.5
]])
post2
,
val2
=
f
(
post1
,
[
-
5
,
.
5
,
0
,
1
],
[[
1.
],
[
1.1
],
[
1.5
]])
...
@@ -710,9 +710,9 @@ class T_random_function(utt.InferShapeTester):
...
@@ -710,9 +710,9 @@ class T_random_function(utt.InferShapeTester):
numpy_val2
=
numpy_rng
.
uniform
(
low
=
[
-
5
,
.
5
,
0
,
1
],
numpy_val2
=
numpy_rng
.
uniform
(
low
=
[
-
5
,
.
5
,
0
,
1
],
high
=
[[
1.
],
[
1.1
],
[
1.5
]])
high
=
[[
1.
],
[
1.1
],
[
1.5
]])
assert
n
umpy
.
all
(
val0
==
numpy_val0
),
(
val0
,
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
),
(
val0
,
numpy_val0
)
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
assert
n
umpy
.
all
(
val2
==
numpy_val2
)
assert
n
p
.
all
(
val2
==
numpy_val2
)
def
test_uniform_vector
(
self
):
def
test_uniform_vector
(
self
):
rng_R
=
random_state_type
()
rng_R
=
random_state_type
()
...
@@ -724,22 +724,22 @@ class T_random_function(utt.InferShapeTester):
...
@@ -724,22 +724,22 @@ class T_random_function(utt.InferShapeTester):
accept_inplace
=
True
)
accept_inplace
=
True
)
def
as_floatX
(
thing
):
def
as_floatX
(
thing
):
return
n
umpy
.
asarray
(
thing
,
dtype
=
theano
.
config
.
floatX
)
return
n
p
.
asarray
(
thing
,
dtype
=
theano
.
config
.
floatX
)
low_val
=
as_floatX
([
.
1
,
.
2
,
.
3
])
low_val
=
as_floatX
([
.
1
,
.
2
,
.
3
])
high_val
=
as_floatX
([
1.1
,
2.2
,
3.3
])
high_val
=
as_floatX
([
1.1
,
2.2
,
3.3
])
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Arguments of size (3,)
# Arguments of size (3,)
rng0
,
val0
=
f
(
rng
,
low_val
,
high_val
)
rng0
,
val0
=
f
(
rng
,
low_val
,
high_val
)
numpy_val0
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
low_val
,
high
=
high_val
))
numpy_val0
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
low_val
,
high
=
high_val
))
assert
n
umpy
.
all
(
val0
==
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
)
# arguments of size (2,)
# arguments of size (2,)
rng1
,
val1
=
f
(
rng0
,
low_val
[:
-
1
],
high_val
[:
-
1
])
rng1
,
val1
=
f
(
rng0
,
low_val
[:
-
1
],
high_val
[:
-
1
])
numpy_val1
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
low_val
[:
-
1
],
numpy_val1
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
low_val
[:
-
1
],
high
=
high_val
[:
-
1
]))
high
=
high_val
[:
-
1
]))
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
# Specifying the size explicitly
# Specifying the size explicitly
g
=
compile
.
function
([
rng_R
,
low
,
high
],
g
=
compile
.
function
([
rng_R
,
low
,
high
],
...
@@ -748,7 +748,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -748,7 +748,7 @@ class T_random_function(utt.InferShapeTester):
rng2
,
val2
=
g
(
rng1
,
low_val
,
high_val
)
rng2
,
val2
=
g
(
rng1
,
low_val
,
high_val
)
numpy_val2
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
low_val
,
high
=
high_val
,
numpy_val2
=
as_floatX
(
numpy_rng
.
uniform
(
low
=
low_val
,
high
=
high_val
,
size
=
(
3
,)))
size
=
(
3
,)))
assert
n
umpy
.
all
(
val2
==
numpy_val2
)
assert
n
p
.
all
(
val2
==
numpy_val2
)
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
low_val
[:
-
1
],
high_val
[:
-
1
])
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
low_val
[:
-
1
],
high_val
[:
-
1
])
def
test_binomial_vector
(
self
):
def
test_binomial_vector
(
self
):
...
@@ -761,19 +761,19 @@ class T_random_function(utt.InferShapeTester):
...
@@ -761,19 +761,19 @@ class T_random_function(utt.InferShapeTester):
accept_inplace
=
True
)
accept_inplace
=
True
)
n_val
=
[
1
,
2
,
3
]
n_val
=
[
1
,
2
,
3
]
prob_val
=
n
umpy
.
asarray
([
.
1
,
.
2
,
.
3
],
dtype
=
config
.
floatX
)
prob_val
=
n
p
.
asarray
([
.
1
,
.
2
,
.
3
],
dtype
=
config
.
floatX
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Arguments of size (3,)
# Arguments of size (3,)
rng0
,
val0
=
f
(
rng
,
n_val
,
prob_val
)
rng0
,
val0
=
f
(
rng
,
n_val
,
prob_val
)
numpy_val0
=
numpy_rng
.
binomial
(
n
=
n_val
,
p
=
prob_val
)
numpy_val0
=
numpy_rng
.
binomial
(
n
=
n_val
,
p
=
prob_val
)
assert
n
umpy
.
all
(
val0
==
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
)
# arguments of size (2,)
# arguments of size (2,)
rng1
,
val1
=
f
(
rng0
,
n_val
[:
-
1
],
prob_val
[:
-
1
])
rng1
,
val1
=
f
(
rng0
,
n_val
[:
-
1
],
prob_val
[:
-
1
])
numpy_val1
=
numpy_rng
.
binomial
(
n
=
n_val
[:
-
1
],
p
=
prob_val
[:
-
1
])
numpy_val1
=
numpy_rng
.
binomial
(
n
=
n_val
[:
-
1
],
p
=
prob_val
[:
-
1
])
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
# Specifying the size explicitly
# Specifying the size explicitly
g
=
compile
.
function
([
rng_R
,
n
,
prob
],
g
=
compile
.
function
([
rng_R
,
n
,
prob
],
...
@@ -781,7 +781,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -781,7 +781,7 @@ class T_random_function(utt.InferShapeTester):
accept_inplace
=
True
)
accept_inplace
=
True
)
rng2
,
val2
=
g
(
rng1
,
n_val
,
prob_val
)
rng2
,
val2
=
g
(
rng1
,
n_val
,
prob_val
)
numpy_val2
=
numpy_rng
.
binomial
(
n
=
n_val
,
p
=
prob_val
,
size
=
(
3
,))
numpy_val2
=
numpy_rng
.
binomial
(
n
=
n_val
,
p
=
prob_val
,
size
=
(
3
,))
assert
n
umpy
.
all
(
val2
==
numpy_val2
)
assert
n
p
.
all
(
val2
==
numpy_val2
)
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
n_val
[:
-
1
],
prob_val
[:
-
1
])
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
n_val
[:
-
1
],
prob_val
[:
-
1
])
def
test_normal_vector
(
self
):
def
test_normal_vector
(
self
):
...
@@ -794,35 +794,35 @@ class T_random_function(utt.InferShapeTester):
...
@@ -794,35 +794,35 @@ class T_random_function(utt.InferShapeTester):
accept_inplace
=
True
)
accept_inplace
=
True
)
def
as_floatX
(
thing
):
def
as_floatX
(
thing
):
return
n
umpy
.
asarray
(
thing
,
dtype
=
theano
.
config
.
floatX
)
return
n
p
.
asarray
(
thing
,
dtype
=
theano
.
config
.
floatX
)
avg_val
=
[
1
,
2
,
3
]
avg_val
=
[
1
,
2
,
3
]
std_val
=
as_floatX
([
.
1
,
.
2
,
.
3
])
std_val
=
as_floatX
([
.
1
,
.
2
,
.
3
])
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Arguments of size (3,)
# Arguments of size (3,)
rng0
,
val0
=
f
(
rng
,
avg_val
,
std_val
)
rng0
,
val0
=
f
(
rng
,
avg_val
,
std_val
)
numpy_val0
=
as_floatX
(
numpy_rng
.
normal
(
loc
=
as_floatX
(
avg_val
),
numpy_val0
=
as_floatX
(
numpy_rng
.
normal
(
loc
=
as_floatX
(
avg_val
),
scale
=
as_floatX
(
std_val
)))
scale
=
as_floatX
(
std_val
)))
assert
n
umpy
.
all
(
val0
==
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
)
# arguments of size (2,)
# arguments of size (2,)
rng1
,
val1
=
f
(
rng0
,
avg_val
[:
-
1
],
std_val
[:
-
1
])
rng1
,
val1
=
f
(
rng0
,
avg_val
[:
-
1
],
std_val
[:
-
1
])
numpy_val1
=
n
umpy
.
asarray
(
numpy_rng
.
normal
(
loc
=
avg_val
[:
-
1
],
numpy_val1
=
n
p
.
asarray
(
numpy_rng
.
normal
(
loc
=
avg_val
[:
-
1
],
scale
=
std_val
[:
-
1
]),
scale
=
std_val
[:
-
1
]),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
# Specifying the size explicitly
# Specifying the size explicitly
g
=
compile
.
function
([
rng_R
,
avg
,
std
],
g
=
compile
.
function
([
rng_R
,
avg
,
std
],
normal
(
rng_R
,
avg
=
avg
,
std
=
std
,
size
=
(
3
,)),
normal
(
rng_R
,
avg
=
avg
,
std
=
std
,
size
=
(
3
,)),
accept_inplace
=
True
)
accept_inplace
=
True
)
rng2
,
val2
=
g
(
rng1
,
avg_val
,
std_val
)
rng2
,
val2
=
g
(
rng1
,
avg_val
,
std_val
)
numpy_val2
=
n
umpy
.
asarray
(
numpy_rng
.
normal
(
loc
=
avg_val
,
scale
=
std_val
,
numpy_val2
=
n
p
.
asarray
(
numpy_rng
.
normal
(
loc
=
avg_val
,
scale
=
std_val
,
size
=
(
3
,)),
size
=
(
3
,)),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
assert
n
umpy
.
all
(
val2
==
numpy_val2
)
assert
n
p
.
all
(
val2
==
numpy_val2
)
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
avg_val
[:
-
1
],
std_val
[:
-
1
])
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
avg_val
[:
-
1
],
std_val
[:
-
1
])
def
test_random_integers_vector
(
self
):
def
test_random_integers_vector
(
self
):
...
@@ -836,29 +836,29 @@ class T_random_function(utt.InferShapeTester):
...
@@ -836,29 +836,29 @@ class T_random_function(utt.InferShapeTester):
low_val
=
[
100
,
200
,
300
]
low_val
=
[
100
,
200
,
300
]
high_val
=
[
110
,
220
,
330
]
high_val
=
[
110
,
220
,
330
]
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Arguments of size (3,)
# Arguments of size (3,)
rng0
,
val0
=
f
(
rng
,
low_val
,
high_val
)
rng0
,
val0
=
f
(
rng
,
low_val
,
high_val
)
numpy_val0
=
n
umpy
.
asarray
([
numpy_rng
.
randint
(
low
=
lv
,
high
=
hv
+
1
)
numpy_val0
=
n
p
.
asarray
([
numpy_rng
.
randint
(
low
=
lv
,
high
=
hv
+
1
)
for
lv
,
hv
in
zip
(
low_val
,
high_val
)])
for
lv
,
hv
in
zip
(
low_val
,
high_val
)])
assert
n
umpy
.
all
(
val0
==
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
)
# arguments of size (2,)
# arguments of size (2,)
rng1
,
val1
=
f
(
rng0
,
low_val
[:
-
1
],
high_val
[:
-
1
])
rng1
,
val1
=
f
(
rng0
,
low_val
[:
-
1
],
high_val
[:
-
1
])
numpy_val1
=
n
umpy
.
asarray
([
numpy_rng
.
randint
(
low
=
lv
,
high
=
hv
+
1
)
numpy_val1
=
n
p
.
asarray
([
numpy_rng
.
randint
(
low
=
lv
,
high
=
hv
+
1
)
for
lv
,
hv
in
zip
(
low_val
[:
-
1
],
high_val
[:
-
1
])])
for
lv
,
hv
in
zip
(
low_val
[:
-
1
],
high_val
[:
-
1
])])
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
# Specifying the size explicitly
# Specifying the size explicitly
g
=
compile
.
function
([
rng_R
,
low
,
high
],
g
=
compile
.
function
([
rng_R
,
low
,
high
],
random_integers
(
rng_R
,
low
=
low
,
high
=
high
,
size
=
(
3
,)),
random_integers
(
rng_R
,
low
=
low
,
high
=
high
,
size
=
(
3
,)),
accept_inplace
=
True
)
accept_inplace
=
True
)
rng2
,
val2
=
g
(
rng1
,
low_val
,
high_val
)
rng2
,
val2
=
g
(
rng1
,
low_val
,
high_val
)
numpy_val2
=
n
umpy
.
asarray
([
numpy_rng
.
randint
(
low
=
lv
,
high
=
hv
+
1
)
numpy_val2
=
n
p
.
asarray
([
numpy_rng
.
randint
(
low
=
lv
,
high
=
hv
+
1
)
for
lv
,
hv
in
zip
(
low_val
,
high_val
)])
for
lv
,
hv
in
zip
(
low_val
,
high_val
)])
assert
n
umpy
.
all
(
val2
==
numpy_val2
)
assert
n
p
.
all
(
val2
==
numpy_val2
)
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
low_val
[:
-
1
],
high_val
[:
-
1
])
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
low_val
[:
-
1
],
high_val
[:
-
1
])
# Vectorized permutation don't make sense: the only parameter, n,
# Vectorized permutation don't make sense: the only parameter, n,
...
@@ -875,30 +875,30 @@ class T_random_function(utt.InferShapeTester):
...
@@ -875,30 +875,30 @@ class T_random_function(utt.InferShapeTester):
n_val
=
[
1
,
2
,
3
]
n_val
=
[
1
,
2
,
3
]
pvals_val
=
[[
.
1
,
.
9
],
[
.
2
,
.
8
],
[
.
3
,
.
7
]]
pvals_val
=
[[
.
1
,
.
9
],
[
.
2
,
.
8
],
[
.
3
,
.
7
]]
pvals_val
=
n
umpy
.
asarray
(
pvals_val
,
dtype
=
config
.
floatX
)
pvals_val
=
n
p
.
asarray
(
pvals_val
,
dtype
=
config
.
floatX
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Arguments of size (3,)
# Arguments of size (3,)
rng0
,
val0
=
f
(
rng
,
n_val
,
pvals_val
)
rng0
,
val0
=
f
(
rng
,
n_val
,
pvals_val
)
numpy_val0
=
n
umpy
.
asarray
([
numpy_rng
.
multinomial
(
n
=
nv
,
pvals
=
pv
)
numpy_val0
=
n
p
.
asarray
([
numpy_rng
.
multinomial
(
n
=
nv
,
pvals
=
pv
)
for
nv
,
pv
in
zip
(
n_val
,
pvals_val
)])
for
nv
,
pv
in
zip
(
n_val
,
pvals_val
)])
assert
n
umpy
.
all
(
val0
==
numpy_val0
)
assert
n
p
.
all
(
val0
==
numpy_val0
)
# arguments of size (2,)
# arguments of size (2,)
rng1
,
val1
=
f
(
rng0
,
n_val
[:
-
1
],
pvals_val
[:
-
1
])
rng1
,
val1
=
f
(
rng0
,
n_val
[:
-
1
],
pvals_val
[:
-
1
])
numpy_val1
=
n
umpy
.
asarray
([
numpy_rng
.
multinomial
(
n
=
nv
,
pvals
=
pv
)
numpy_val1
=
n
p
.
asarray
([
numpy_rng
.
multinomial
(
n
=
nv
,
pvals
=
pv
)
for
nv
,
pv
in
zip
(
n_val
[:
-
1
],
pvals_val
[:
-
1
])])
for
nv
,
pv
in
zip
(
n_val
[:
-
1
],
pvals_val
[:
-
1
])])
assert
n
umpy
.
all
(
val1
==
numpy_val1
)
assert
n
p
.
all
(
val1
==
numpy_val1
)
# Specifying the size explicitly
# Specifying the size explicitly
g
=
compile
.
function
([
rng_R
,
n
,
pvals
],
g
=
compile
.
function
([
rng_R
,
n
,
pvals
],
multinomial
(
rng_R
,
n
=
n
,
pvals
=
pvals
,
size
=
(
3
,)),
multinomial
(
rng_R
,
n
=
n
,
pvals
=
pvals
,
size
=
(
3
,)),
accept_inplace
=
True
)
accept_inplace
=
True
)
rng2
,
val2
=
g
(
rng1
,
n_val
,
pvals_val
)
rng2
,
val2
=
g
(
rng1
,
n_val
,
pvals_val
)
numpy_val2
=
n
umpy
.
asarray
([
numpy_rng
.
multinomial
(
n
=
nv
,
pvals
=
pv
)
numpy_val2
=
n
p
.
asarray
([
numpy_rng
.
multinomial
(
n
=
nv
,
pvals
=
pv
)
for
nv
,
pv
in
zip
(
n_val
,
pvals_val
)])
for
nv
,
pv
in
zip
(
n_val
,
pvals_val
)])
assert
n
umpy
.
all
(
val2
==
numpy_val2
)
assert
n
p
.
all
(
val2
==
numpy_val2
)
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
n_val
[:
-
1
],
pvals_val
[:
-
1
])
self
.
assertRaises
(
ValueError
,
g
,
rng2
,
n_val
[:
-
1
],
pvals_val
[:
-
1
])
def
test_multinomial_tensor3_a
(
self
):
def
test_multinomial_tensor3_a
(
self
):
...
@@ -914,15 +914,15 @@ class T_random_function(utt.InferShapeTester):
...
@@ -914,15 +914,15 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
([
rng_R
,
pvals
],
[
post_r
,
out
],
f
=
compile
.
function
([
rng_R
,
pvals
],
[
post_r
,
out
],
accept_inplace
=
True
)
accept_inplace
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
pvals_val
=
n
umpy
.
asarray
([[[
.
1
,
.
9
],
[
.
2
,
.
8
],
[
.
3
,
.
7
]]])
pvals_val
=
n
p
.
asarray
([[[
.
1
,
.
9
],
[
.
2
,
.
8
],
[
.
3
,
.
7
]]])
assert
pvals_val
.
shape
==
(
1
,
3
,
2
)
assert
pvals_val
.
shape
==
(
1
,
3
,
2
)
new_rng
,
draw
=
f
(
rng
,
pvals_val
)
new_rng
,
draw
=
f
(
rng
,
pvals_val
)
assert
draw
.
shape
==
(
1
,
3
,
2
)
assert
draw
.
shape
==
(
1
,
3
,
2
)
assert
n
umpy
.
allclose
(
draw
.
sum
(
axis
=
2
),
9
)
assert
n
p
.
allclose
(
draw
.
sum
(
axis
=
2
),
9
)
def
test_multinomial_tensor3_b
(
self
):
def
test_multinomial_tensor3_b
(
self
):
# Test the examples given in the multinomial documentation regarding
# Test the examples given in the multinomial documentation regarding
...
@@ -937,15 +937,15 @@ class T_random_function(utt.InferShapeTester):
...
@@ -937,15 +937,15 @@ class T_random_function(utt.InferShapeTester):
f
=
compile
.
function
([
rng_R
,
pvals
],
[
post_r
,
out
],
f
=
compile
.
function
([
rng_R
,
pvals
],
[
post_r
,
out
],
accept_inplace
=
True
)
accept_inplace
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
numpy_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
pvals_val
=
n
umpy
.
asarray
([[[
.
1
,
.
9
],
[
.
2
,
.
8
],
[
.
3
,
.
7
]]])
pvals_val
=
n
p
.
asarray
([[[
.
1
,
.
9
],
[
.
2
,
.
8
],
[
.
3
,
.
7
]]])
assert
pvals_val
.
shape
==
(
1
,
3
,
2
)
assert
pvals_val
.
shape
==
(
1
,
3
,
2
)
out_rng
,
draw
=
f
(
rng
,
pvals_val
)
out_rng
,
draw
=
f
(
rng
,
pvals_val
)
assert
draw
.
shape
==
(
10
,
1
,
3
,
2
)
assert
draw
.
shape
==
(
10
,
1
,
3
,
2
)
assert
n
umpy
.
allclose
(
draw
.
sum
(
axis
=
3
),
9
)
assert
n
p
.
allclose
(
draw
.
sum
(
axis
=
3
),
9
)
def
test_dtype
(
self
):
def
test_dtype
(
self
):
rng_R
=
random_state_type
()
rng_R
=
random_state_type
()
...
@@ -956,13 +956,13 @@ class T_random_function(utt.InferShapeTester):
...
@@ -956,13 +956,13 @@ class T_random_function(utt.InferShapeTester):
assert
out
.
dtype
==
'int8'
assert
out
.
dtype
==
'int8'
f
=
compile
.
function
([
rng_R
,
low
,
high
],
[
post_r
,
out
])
f
=
compile
.
function
([
rng_R
,
low
,
high
],
[
post_r
,
out
])
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng0
,
val0
=
f
(
rng
,
0
,
9
)
rng0
,
val0
=
f
(
rng
,
0
,
9
)
assert
val0
.
dtype
==
'int8'
assert
val0
.
dtype
==
'int8'
rng1
,
val1
=
f
(
rng0
,
255
,
257
)
rng1
,
val1
=
f
(
rng0
,
255
,
257
)
assert
val1
.
dtype
==
'int8'
assert
val1
.
dtype
==
'int8'
assert
n
umpy
.
all
(
abs
(
val1
)
<=
1
)
assert
n
p
.
all
(
abs
(
val1
)
<=
1
)
def
test_dtype_normal_uniform_687
(
self
):
def
test_dtype_normal_uniform_687
(
self
):
# Regression test for #687.
# Regression test for #687.
...
@@ -978,7 +978,7 @@ class T_random_function(utt.InferShapeTester):
...
@@ -978,7 +978,7 @@ class T_random_function(utt.InferShapeTester):
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
rng_R
=
random_state_type
()
rng_R
=
random_state_type
()
rng_R_val
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_R_val
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# no shape specified, default args
# no shape specified, default args
post_r
,
out
=
uniform
(
rng_R
)
post_r
,
out
=
uniform
(
rng_R
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论