Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5f0aec41
提交
5f0aec41
authored
4月 03, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
f8bdb7d7
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
111 行增加
和
111 行删除
+111
-111
test_rng_mrg.py
theano/sandbox/tests/test_rng_mrg.py
+111
-111
没有找到文件。
theano/sandbox/tests/test_rng_mrg.py
浏览文件 @
5f0aec41
...
...
@@ -8,7 +8,7 @@ import functools
from
nose.plugins.skip
import
SkipTest
from
nose.tools
import
assert_raises
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
import
theano
...
...
@@ -44,7 +44,7 @@ utt.seed_rng()
# 12 streams
# 7 substreams for each stream
# 5 samples drawn from each substream
java_samples
=
n
umpy
.
loadtxt
(
os
.
path
.
join
(
os
.
path
.
split
(
theano
.
__file__
)[
0
],
java_samples
=
n
p
.
loadtxt
(
os
.
path
.
join
(
os
.
path
.
split
(
theano
.
__file__
)[
0
],
'sandbox'
,
'samples_MRG31k3p_12_7_5.txt'
))
...
...
@@ -65,15 +65,15 @@ def test_deterministic():
fsample1
=
f
()
fsample2
=
f
()
assert
not
n
umpy
.
allclose
(
fsample1
,
fsample2
)
assert
not
n
p
.
allclose
(
fsample1
,
fsample2
)
R2
=
MRG_RandomStreams
(
seed
=
seed
,
use_cuda
=
use_cuda
)
u2
=
R2
.
uniform
(
size
=
sample_size
)
g
=
theano
.
function
([],
u2
)
gsample1
=
g
()
gsample2
=
g
()
assert
n
umpy
.
allclose
(
fsample1
,
gsample1
)
assert
n
umpy
.
allclose
(
fsample2
,
gsample2
)
assert
n
p
.
allclose
(
fsample1
,
gsample1
)
assert
n
p
.
allclose
(
fsample2
,
gsample2
)
def
test_consistency_randomstreams
():
...
...
@@ -102,12 +102,12 @@ def test_consistency_randomstreams():
for
j
in
range
(
n_samples
):
s
=
f
()
stream_samples
.
append
(
s
)
stream_samples
=
n
umpy
.
array
(
stream_samples
)
stream_samples
=
n
p
.
array
(
stream_samples
)
stream_samples
=
stream_samples
.
T
.
flatten
()
samples
.
append
(
stream_samples
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_get_substream_rstates
():
...
...
@@ -117,7 +117,7 @@ def test_get_substream_rstates():
n_streams
=
100
dtype
=
'float32'
rng
=
MRG_RandomStreams
(
n
umpy
.
random
.
randint
(
2147462579
))
rng
=
MRG_RandomStreams
(
n
p
.
random
.
randint
(
2147462579
))
rng
.
get_substream_rstates
(
n_streams
,
dtype
)
...
...
@@ -137,12 +137,12 @@ def test_consistency_cpu_serial():
n_substreams
=
7
samples
=
[]
curr_rstate
=
n
umpy
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
curr_rstate
=
n
p
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
for
i
in
range
(
n_streams
):
stream_rstate
=
curr_rstate
.
copy
()
for
j
in
range
(
n_substreams
):
rstate
=
theano
.
shared
(
n
umpy
.
array
([
stream_rstate
.
copy
()],
rstate
=
theano
.
shared
(
n
p
.
array
([
stream_rstate
.
copy
()],
dtype
=
'int32'
))
new_rstate
,
sample
=
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
dtype
=
config
.
floatX
,
...
...
@@ -164,8 +164,8 @@ def test_consistency_cpu_serial():
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_cpu_parallel
():
...
...
@@ -180,14 +180,14 @@ def test_consistency_cpu_parallel():
n_substreams
=
7
# 7 samples will be drawn in parallel
samples
=
[]
curr_rstate
=
n
umpy
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
curr_rstate
=
n
p
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
for
i
in
range
(
n_streams
):
stream_samples
=
[]
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
=
n
umpy
.
asarray
(
rstate
)
rstate
=
n
p
.
asarray
(
rstate
)
rstate
=
theano
.
shared
(
rstate
)
new_rstate
,
sample
=
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
...
...
@@ -205,13 +205,13 @@ def test_consistency_cpu_parallel():
s
=
f
()
stream_samples
.
append
(
s
)
samples
.
append
(
n
umpy
.
array
(
stream_samples
)
.
T
.
flatten
())
samples
.
append
(
n
p
.
array
(
stream_samples
)
.
T
.
flatten
())
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_GPU_serial
():
...
...
@@ -233,15 +233,15 @@ def test_consistency_GPU_serial():
n_substreams
=
7
samples
=
[]
curr_rstate
=
n
umpy
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
curr_rstate
=
n
p
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
for
i
in
range
(
n_streams
):
stream_rstate
=
curr_rstate
.
copy
()
for
j
in
range
(
n_substreams
):
substream_rstate
=
n
umpy
.
array
(
stream_rstate
.
copy
(),
dtype
=
'int32'
)
substream_rstate
=
n
p
.
array
(
stream_rstate
.
copy
(),
dtype
=
'int32'
)
# HACK - we transfer these int32 to the GPU memory as float32
# (reinterpret_cast)
tmp_float_buf
=
n
umpy
.
frombuffer
(
substream_rstate
.
data
,
tmp_float_buf
=
n
p
.
frombuffer
(
substream_rstate
.
data
,
dtype
=
'float32'
)
# Transfer to device
rstate
=
float32_shared_constructor
(
tmp_float_buf
)
...
...
@@ -269,8 +269,8 @@ def test_consistency_GPU_serial():
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_GPU_parallel
():
...
...
@@ -293,17 +293,17 @@ def test_consistency_GPU_parallel():
n_substreams
=
7
# 7 samples will be drawn in parallel
samples
=
[]
curr_rstate
=
n
umpy
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
curr_rstate
=
n
p
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
for
i
in
range
(
n_streams
):
stream_samples
=
[]
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
=
n
umpy
.
asarray
(
rstate
)
.
flatten
()
rstate
=
n
p
.
asarray
(
rstate
)
.
flatten
()
# HACK - transfer these int32 to the GPU memory as float32
# (reinterpret_cast)
tmp_float_buf
=
n
umpy
.
frombuffer
(
rstate
.
data
,
dtype
=
'float32'
)
tmp_float_buf
=
n
p
.
frombuffer
(
rstate
.
data
,
dtype
=
'float32'
)
# Transfer to device
rstate
=
float32_shared_constructor
(
tmp_float_buf
)
...
...
@@ -325,13 +325,13 @@ def test_consistency_GPU_parallel():
s
=
f
()
stream_samples
.
append
(
s
)
samples
.
append
(
n
umpy
.
array
(
stream_samples
)
.
T
.
flatten
())
samples
.
append
(
n
p
.
array
(
stream_samples
)
.
T
.
flatten
())
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_GPU_nstreams_limit
():
...
...
@@ -373,12 +373,12 @@ def test_consistency_GPUA_serial():
n_substreams
=
7
samples
=
[]
curr_rstate
=
n
umpy
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
curr_rstate
=
n
p
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
for
i
in
range
(
n_streams
):
stream_rstate
=
curr_rstate
.
copy
()
for
j
in
range
(
n_substreams
):
substream_rstate
=
n
umpy
.
array
([
stream_rstate
.
copy
()],
substream_rstate
=
n
p
.
array
([
stream_rstate
.
copy
()],
dtype
=
'int32'
)
# Transfer to device
rstate
=
gpuarray_shared_constructor
(
substream_rstate
)
...
...
@@ -407,8 +407,8 @@ def test_consistency_GPUA_serial():
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_GPUA_parallel
():
...
...
@@ -424,14 +424,14 @@ def test_consistency_GPUA_parallel():
n_substreams
=
7
# 7 samples will be drawn in parallel
samples
=
[]
curr_rstate
=
n
umpy
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
curr_rstate
=
n
p
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
for
i
in
range
(
n_streams
):
stream_samples
=
[]
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
=
n
umpy
.
asarray
(
rstate
)
rstate
=
n
p
.
asarray
(
rstate
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
new_rstate
,
sample
=
rng_mrg
.
GPUA_mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
...
...
@@ -452,13 +452,13 @@ def test_consistency_GPUA_parallel():
s
=
f
()
stream_samples
.
append
(
s
)
samples
.
append
(
n
umpy
.
array
(
stream_samples
)
.
T
.
flatten
())
samples
.
append
(
n
p
.
array
(
stream_samples
)
.
T
.
flatten
())
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_GPUA_full_fill
():
...
...
@@ -496,16 +496,16 @@ def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=None,
ival
=
f
(
*
inputs
)
assert
ival
.
shape
==
sample_size
dt
+=
time
.
time
()
-
t0
ival
=
n
umpy
.
asarray
(
ival
)
ival
=
n
p
.
asarray
(
ival
)
if
i
==
0
:
mean
=
n
umpy
.
array
(
ival
,
copy
=
True
)
avg_var
=
n
umpy
.
mean
((
ival
-
target_avg
)
**
2
)
mean
=
n
p
.
array
(
ival
,
copy
=
True
)
avg_var
=
n
p
.
mean
((
ival
-
target_avg
)
**
2
)
min_
=
ival
.
min
()
max_
=
ival
.
max
()
else
:
alpha
=
1.0
/
(
1
+
i
)
mean
=
alpha
*
ival
+
(
1
-
alpha
)
*
mean
avg_var
=
(
alpha
*
n
umpy
.
mean
((
ival
-
target_avg
)
**
2
)
+
avg_var
=
(
alpha
*
n
p
.
mean
((
ival
-
target_avg
)
**
2
)
+
(
1
-
alpha
)
*
avg_var
)
min_
=
min
(
min_
,
ival
.
min
())
max_
=
max
(
max_
,
ival
.
max
())
...
...
@@ -514,19 +514,19 @@ def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=None,
assert
max_
<
1
if
hasattr
(
target_avg
,
'shape'
):
# looks if target_avg is an array
diff
=
n
umpy
.
mean
(
abs
(
mean
-
target_avg
))
diff
=
n
p
.
mean
(
abs
(
mean
-
target_avg
))
# print prefix, 'mean diff with mean', diff
assert
n
umpy
.
all
(
diff
<
mean_rtol
*
(
1
+
abs
(
target_avg
))),
(
assert
n
p
.
all
(
diff
<
mean_rtol
*
(
1
+
abs
(
target_avg
))),
(
'bad mean?
%
s
%
s'
%
(
mean
,
target_avg
))
else
:
# if target_avg is a scalar, then we can do the mean of
# `mean` to get something more precise
mean
=
n
umpy
.
mean
(
mean
)
mean
=
n
p
.
mean
(
mean
)
# print prefix, 'mean', mean
assert
abs
(
mean
-
target_avg
)
<
mean_rtol
*
(
1
+
abs
(
target_avg
)),
(
'bad mean?
%
f
%
f'
%
(
mean
,
target_avg
))
std
=
n
umpy
.
sqrt
(
avg_var
)
std
=
n
p
.
sqrt
(
avg_var
)
# print prefix, 'var', avg_var
# print prefix, 'std', std
if
target_std
is
not
None
:
...
...
@@ -556,9 +556,9 @@ def test_uniform():
for
size
,
const_size
,
var_input
,
input
in
[
(
sample_size
,
sample_size
,
[],
[]),
(
x
.
shape
,
sample_size
,
[
x
],
[
n
umpy
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
[
n
p
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
((
x
.
shape
[
0
],
sample_size
[
1
]),
sample_size
,
[
x
],
[
n
umpy
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
[
n
p
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
# test empty size (scalar)
((),
(),
[],
[]),
]:
...
...
@@ -586,7 +586,7 @@ def test_uniform():
# print cpu_out[-1, -10:]
# Increase the number of steps if sizes implies only a few samples
if
n
umpy
.
prod
(
const_size
)
<
10
:
if
n
p
.
prod
(
const_size
)
<
10
:
steps_
=
steps
*
100
else
:
steps_
=
steps
...
...
@@ -607,14 +607,14 @@ def test_uniform():
theano
.
sandbox
.
rng_mrg
.
GPU_mrg_uniform
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
# theano.printing.debugprint(f)
gpu_out
=
n
umpy
.
asarray
(
f
(
*
input
))
gpu_out
=
n
p
.
asarray
(
f
(
*
input
))
# print 'GPU: random?[:10], random?[-10:]'
# print gpu_out[0, 0:10]
# print gpu_out[-1, -10:]
basictest
(
f
,
steps_
,
const_size
,
prefix
=
'mrg gpu'
,
inputs
=
input
)
n
umpy
.
testing
.
assert_array_almost_equal
(
cpu_out
,
gpu_out
,
n
p
.
testing
.
assert_array_almost_equal
(
cpu_out
,
gpu_out
,
decimal
=
6
)
# print ''
...
...
@@ -633,7 +633,7 @@ def test_broadcastable():
x
=
tensor
.
matrix
()
size1
=
(
10
,
1
)
size2
=
(
x
.
shape
[
0
],
1
)
pvals_1
=
n
umpy
.
random
.
uniform
(
0
,
1
,
size
=
size1
)
pvals_1
=
n
p
.
random
.
uniform
(
0
,
1
,
size
=
size1
)
pvals_1
=
pvals_1
/
sum
(
pvals_1
)
pvals_2
=
R
.
uniform
(
size
=
size2
)
pvals_2
=
pvals_2
/
tensor
.
sum
(
pvals_2
)
...
...
@@ -684,9 +684,9 @@ def test_binomial():
for
size
,
const_size
,
var_input
,
input
in
[
(
sample_size
,
sample_size
,
[],
[]),
(
x
.
shape
,
sample_size
,
[
x
],
[
n
umpy
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
[
n
p
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
((
x
.
shape
[
0
],
sample_size
[
1
]),
sample_size
,
[
x
],
[
n
umpy
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
[
n
p
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)]),
# test empty size (scalar)
((),
(),
[],
[]),
]:
...
...
@@ -701,7 +701,7 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
out
=
f
(
*
input
)
# Increase the number of steps if sizes implies only a few samples
if
n
umpy
.
prod
(
const_size
)
<
10
:
if
n
p
.
prod
(
const_size
)
<
10
:
steps_
=
steps
*
100
else
:
steps_
=
steps
...
...
@@ -717,12 +717,12 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
f
=
theano
.
function
(
var_input
,
theano
.
Out
(
theano
.
sandbox
.
cuda
.
basic_ops
.
gpu_from_host
(
u
),
borrow
=
True
),
mode
=
mode_with_gpu
)
gpu_out
=
n
umpy
.
asarray
(
f
(
*
input
))
gpu_out
=
n
p
.
asarray
(
f
(
*
input
))
basictest
(
f
,
steps_
,
const_size
,
prefix
=
'mrg gpu'
,
inputs
=
input
,
allow_01
=
True
,
target_avg
=
mean
,
mean_rtol
=
rtol
)
n
umpy
.
testing
.
assert_array_almost_equal
(
out
,
gpu_out
,
n
p
.
testing
.
assert_array_almost_equal
(
out
,
gpu_out
,
decimal
=
6
)
RR
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
234
)
...
...
@@ -752,22 +752,22 @@ def test_normal0():
for
size
,
const_size
,
var_input
,
input
,
avg
,
rtol
,
std_tol
in
[
(
sample_size
,
sample_size
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
(
x
.
shape
,
sample_size
,
[
x
],
[
n
umpy
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
[
n
p
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
((
x
.
shape
[
0
],
sample_size
[
1
]),
sample_size
,
[
x
],
[
n
umpy
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
[
n
p
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
# test odd value
(
sample_size_odd
,
sample_size_odd
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
# test odd value
(
x
.
shape
,
sample_size_odd
,
[
x
],
[
n
umpy
.
zeros
(
sample_size_odd
,
dtype
=
config
.
floatX
)],
[
n
p
.
zeros
(
sample_size_odd
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
(
sample_size
,
sample_size
,
[],
[],
n
umpy
.
arange
(
numpy
.
prod
(
sample_size
),
n
p
.
arange
(
np
.
prod
(
sample_size
),
dtype
=
'float32'
)
.
reshape
(
sample_size
),
10.
*
std
/
n
umpy
.
sqrt
(
steps
),
default_rtol
),
10.
*
std
/
n
p
.
sqrt
(
steps
),
default_rtol
),
# test empty size (scalar)
((),
(),
[],
[],
-
5.
,
default_rtol
,
0.02
),
# test with few samples at the same time
...
...
@@ -788,7 +788,7 @@ def test_normal0():
# print 'random?[:10]\n', out[0, 0:10]
# Increase the number of steps if size implies only a few samples
if
n
umpy
.
prod
(
const_size
)
<
10
:
if
n
p
.
prod
(
const_size
)
<
10
:
steps_
=
steps
*
50
else
:
steps_
=
steps
...
...
@@ -812,7 +812,7 @@ def test_normal0():
# theano.printing.debugprint(f)
sys
.
stdout
.
flush
()
gpu_out
=
n
umpy
.
asarray
(
f
(
*
input
))
gpu_out
=
n
p
.
asarray
(
f
(
*
input
))
# print 'random?[:10]\n', gpu_out[0, 0:10]
# print '----'
sys
.
stdout
.
flush
()
...
...
@@ -821,7 +821,7 @@ def test_normal0():
mean_rtol
=
rtol
,
std_tol
=
std_tol
)
# Need to allow some rounding error as their is float
# computation that are done on the gpu vs cpu
assert
n
umpy
.
allclose
(
out
,
gpu_out
,
rtol
=
5e-6
,
atol
=
5e-6
)
assert
n
p
.
allclose
(
out
,
gpu_out
,
rtol
=
5e-6
,
atol
=
5e-6
)
# print ''
# print 'ON CPU w NUMPY:'
...
...
@@ -838,26 +838,26 @@ def basic_multinomialtest(f, steps, sample_size, target_pvals, n_samples,
prefix
=
""
,
mean_rtol
=
0.04
):
dt
=
0.0
avg_pvals
=
n
umpy
.
zeros
(
target_pvals
.
shape
,
dtype
=
config
.
floatX
)
avg_pvals
=
n
p
.
zeros
(
target_pvals
.
shape
,
dtype
=
config
.
floatX
)
for
i
in
xrange
(
steps
):
t0
=
time
.
time
()
ival
=
f
()
assert
ival
.
shape
==
sample_size
assert
n
umpy
.
all
(
numpy
.
sum
(
ival
,
axis
=
1
)
==
n_samples
)
assert
n
p
.
all
(
np
.
sum
(
ival
,
axis
=
1
)
==
n_samples
)
dt
+=
time
.
time
()
-
t0
avg_pvals
+=
ival
avg_pvals
/=
(
steps
*
n_samples
)
assert
n
umpy
.
mean
(
abs
(
avg_pvals
-
target_pvals
))
<
mean_rtol
assert
n
p
.
mean
(
abs
(
avg_pvals
-
target_pvals
))
<
mean_rtol
print
(
'random?[:10]
\n
'
,
n
umpy
.
asarray
(
f
()[:
10
]))
print
(
'random?[:10]
\n
'
,
n
p
.
asarray
(
f
()[:
10
]))
print
(
prefix
,
'mean'
,
avg_pvals
)
# < mean_rtol, 'bad mean? %s %s' % (str(avg_pvals), str(target_pvals))
print
(
n
umpy
.
mean
(
abs
(
avg_pvals
-
target_pvals
)))
print
(
n
p
.
mean
(
abs
(
avg_pvals
-
target_pvals
)))
print
(
prefix
,
'time'
,
dt
)
print
(
prefix
,
'elements'
,
steps
*
n
umpy
.
prod
(
target_pvals
.
shape
))
print
(
prefix
,
'samples/sec'
,
steps
*
n
umpy
.
prod
(
target_pvals
.
shape
)
/
dt
)
print
(
prefix
,
'elements'
,
steps
*
n
p
.
prod
(
target_pvals
.
shape
))
print
(
prefix
,
'samples/sec'
,
steps
*
n
p
.
prod
(
target_pvals
.
shape
)
/
dt
)
def
test_multinomial
():
...
...
@@ -875,8 +875,8 @@ def test_multinomial():
# print ''
# print 'ON CPU:'
pvals
=
n
umpy
.
asarray
(
numpy
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
umpy
.
apply_along_axis
(
lambda
row
:
row
/
numpy
.
sum
(
row
),
1
,
pvals
)
pvals
=
n
p
.
asarray
(
np
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
p
.
apply_along_axis
(
lambda
row
:
row
/
np
.
sum
(
row
),
1
,
pvals
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
# Note: we specify `nstreams` to avoid a warning.
m
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
config
.
floatX
,
nstreams
=
30
*
256
)
...
...
@@ -892,7 +892,7 @@ def test_multinomial():
# print ''
# print 'ON GPU:'
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
pvals
=
n
umpy
.
asarray
(
pvals
,
dtype
=
'float32'
)
pvals
=
n
p
.
asarray
(
pvals
,
dtype
=
'float32'
)
# We give the number of streams to avoid a warning.
n
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
'float32'
,
nstreams
=
30
*
256
)
# well, it's really that this test w GPU doesn't make sense otw
...
...
@@ -907,7 +907,7 @@ def test_multinomial():
sys
.
stdout
.
flush
()
basic_multinomialtest
(
f
,
steps
,
sample_size
,
pvals
,
n_samples
=
1
,
prefix
=
'gpu mrg '
)
n
umpy
.
testing
.
assert_array_almost_equal
(
out
,
gpu_out
,
decimal
=
6
)
n
p
.
testing
.
assert_array_almost_equal
(
out
,
gpu_out
,
decimal
=
6
)
def
test_multinomial_n_samples
():
...
...
@@ -922,8 +922,8 @@ def test_multinomial_n_samples():
sample_size
=
(
450
,
6
)
mode_
=
theano
.
compile
.
mode
.
get_mode
(
mode_
)
pvals
=
n
umpy
.
asarray
(
numpy
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
umpy
.
apply_along_axis
(
lambda
row
:
row
/
numpy
.
sum
(
row
),
1
,
pvals
)
pvals
=
n
p
.
asarray
(
np
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
p
.
apply_along_axis
(
lambda
row
:
row
/
np
.
sum
(
row
),
1
,
pvals
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
for
n_samples
,
steps
in
zip
([
5
,
10
,
100
,
1000
],
[
20
,
10
,
1
,
1
]):
...
...
@@ -936,7 +936,7 @@ def test_multinomial_n_samples():
if
mode
!=
'FAST_COMPILE'
and
cuda_available
:
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
pvals
=
n
umpy
.
asarray
(
pvals
,
dtype
=
'float32'
)
pvals
=
n
p
.
asarray
(
pvals
,
dtype
=
'float32'
)
n
=
R
.
multinomial
(
pvals
=
pvals
,
n
=
n_samples
,
dtype
=
'float32'
,
nstreams
=
30
*
256
)
assert
n
.
dtype
==
'float32'
...
...
@@ -999,14 +999,14 @@ def test_random_state_transfer():
for
(
su1
,
su2
)
in
zip
(
g1
.
rng
.
state_updates
,
g2
.
rng
.
state_updates
):
su2
[
0
]
.
set_value
(
su1
[
0
]
.
get_value
())
n
umpy
.
testing
.
assert_array_almost_equal
(
f1
(),
f2
(),
decimal
=
6
)
n
p
.
testing
.
assert_array_almost_equal
(
f1
(),
f2
(),
decimal
=
6
)
def
test_gradient_scan
():
# Test for a crash when using MRG inside scan and taking the gradient
# See https://groups.google.com/d/msg/theano-dev/UbcYyU5m-M8/UO9UgXqnQP0J
theano_rng
=
MRG_RandomStreams
(
10
)
w
=
theano
.
shared
(
n
umpy
.
ones
(
1
,
dtype
=
'float32'
))
w
=
theano
.
shared
(
n
p
.
ones
(
1
,
dtype
=
'float32'
))
def
one_step
(
x
):
return
x
+
theano_rng
.
uniform
((
1
,),
dtype
=
'float32'
)
*
w
...
...
@@ -1015,7 +1015,7 @@ def test_gradient_scan():
values
,
updates
=
theano
.
scan
(
one_step
,
outputs_info
=
x
,
n_steps
=
10
)
gw
=
theano
.
grad
(
tensor
.
sum
(
values
[
-
1
]),
w
)
f
=
theano
.
function
([
x
],
gw
)
f
(
n
umpy
.
arange
(
1
,
dtype
=
'float32'
))
f
(
n
p
.
arange
(
1
,
dtype
=
'float32'
))
def
test_multMatVect
():
...
...
@@ -1029,14 +1029,14 @@ def test_multMatVect():
g0
=
rng_mrg
.
DotModulo
()(
A1
,
s1
,
m1
,
A2
,
s2
,
m2
)
f0
=
theano
.
function
([
A1
,
s1
,
m1
,
A2
,
s2
,
m2
],
g0
)
i32max
=
n
umpy
.
iinfo
(
numpy
.
int32
)
.
max
i32max
=
n
p
.
iinfo
(
np
.
int32
)
.
max
A1
=
n
umpy
.
random
.
randint
(
0
,
i32max
,
(
3
,
3
))
.
astype
(
'int64'
)
s1
=
n
umpy
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
m1
=
n
umpy
.
asarray
(
numpy
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
A2
=
n
umpy
.
random
.
randint
(
0
,
i32max
,
(
3
,
3
))
.
astype
(
'int64'
)
s2
=
n
umpy
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
m2
=
n
umpy
.
asarray
(
numpy
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
A1
=
n
p
.
random
.
randint
(
0
,
i32max
,
(
3
,
3
))
.
astype
(
'int64'
)
s1
=
n
p
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
m1
=
n
p
.
asarray
(
np
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
A2
=
n
p
.
random
.
randint
(
0
,
i32max
,
(
3
,
3
))
.
astype
(
'int64'
)
s2
=
n
p
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
m2
=
n
p
.
asarray
(
np
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
f0
.
input_storage
[
0
]
.
storage
[
0
]
=
A1
f0
.
input_storage
[
1
]
.
storage
[
0
]
=
s1
...
...
@@ -1050,8 +1050,8 @@ def test_multMatVect():
f0
.
fn
()
r_b
=
f0
.
output_storage
[
0
]
.
value
assert
n
umpy
.
allclose
(
r_a1
,
r_b
[:
3
])
assert
n
umpy
.
allclose
(
r_a2
,
r_b
[
3
:])
assert
n
p
.
allclose
(
r_a1
,
r_b
[:
3
])
assert
n
p
.
allclose
(
r_a2
,
r_b
[
3
:])
def
test_seed_fn
():
...
...
@@ -1079,13 +1079,13 @@ def test_seed_fn():
fn1_val0
=
fn1
()
fn1_val1
=
fn1
()
assert
not
n
umpy
.
allclose
(
fn1_val0
,
fn1_val1
)
assert
not
n
p
.
allclose
(
fn1_val0
,
fn1_val1
)
fn2_val0
=
fn2
()
fn2_val1
=
fn2
()
assert
not
n
umpy
.
allclose
(
fn2_val0
,
fn2_val1
)
assert
not
n
p
.
allclose
(
fn2_val0
,
fn2_val1
)
fn3_val0
=
fn3
([
4
])
fn3_val1
=
fn3
([
4
])
assert
not
n
umpy
.
allclose
(
fn3_val0
,
fn3_val1
)
assert
not
n
p
.
allclose
(
fn3_val0
,
fn3_val1
)
assert
fn1_val0
.
size
==
4
assert
fn2_val0
.
size
==
9
...
...
@@ -1097,12 +1097,12 @@ def test_seed_fn():
fn2_val3
=
fn2
()
fn3_val2
=
fn3
([
4
])
fn3_val3
=
fn3
([
4
])
assert
n
umpy
.
allclose
(
fn1_val0
,
fn1_val2
)
==
same
assert
n
umpy
.
allclose
(
fn1_val1
,
fn1_val3
)
==
same
assert
n
umpy
.
allclose
(
fn2_val0
,
fn2_val2
)
==
same
assert
n
umpy
.
allclose
(
fn2_val1
,
fn2_val3
)
==
same
assert
n
umpy
.
allclose
(
fn3_val0
,
fn3_val2
)
==
same
assert
n
umpy
.
allclose
(
fn3_val1
,
fn3_val3
)
==
same
assert
n
p
.
allclose
(
fn1_val0
,
fn1_val2
)
==
same
assert
n
p
.
allclose
(
fn1_val1
,
fn1_val3
)
==
same
assert
n
p
.
allclose
(
fn2_val0
,
fn2_val2
)
==
same
assert
n
p
.
allclose
(
fn2_val1
,
fn2_val3
)
==
same
assert
n
p
.
allclose
(
fn3_val0
,
fn3_val2
)
==
same
assert
n
p
.
allclose
(
fn3_val1
,
fn3_val3
)
==
same
def
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
):
...
...
@@ -1118,7 +1118,7 @@ def rng_mrg_overflow(sizes, fct, mode, should_raise_error):
def
test_overflow_cpu
():
# run with THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32
rng
=
MRG_RandomStreams
(
n
umpy
.
random
.
randint
(
1234
))
rng
=
MRG_RandomStreams
(
n
p
.
random
.
randint
(
1234
))
fct
=
rng
.
uniform
# should raise error as the size overflows
sizes
=
[(
2
**
31
,
),
(
2
**
32
,
),
(
2
**
15
,
2
**
16
,),
(
2
,
2
**
15
,
2
**
15
)]
...
...
@@ -1127,8 +1127,8 @@ def test_overflow_cpu():
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
rng_mrg_overflow
(
sizes
,
fct
,
config
.
mode
,
should_raise_error
=
False
)
# should support int32 sizes
sizes
=
[(
n
umpy
.
int32
(
2
**
10
),
),
(
n
umpy
.
int32
(
2
),
numpy
.
int32
(
2
**
10
),
numpy
.
int32
(
2
**
10
))]
sizes
=
[(
n
p
.
int32
(
2
**
10
),
),
(
n
p
.
int32
(
2
),
np
.
int32
(
2
**
10
),
np
.
int32
(
2
**
10
))]
rng_mrg_overflow
(
sizes
,
fct
,
config
.
mode
,
should_raise_error
=
False
)
...
...
@@ -1147,8 +1147,8 @@ def test_overflow_gpu_old_backend():
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
# should support int32 sizes
sizes
=
[(
n
umpy
.
int32
(
2
**
10
),
),
(
n
umpy
.
int32
(
2
),
numpy
.
int32
(
2
**
10
),
numpy
.
int32
(
2
**
10
))]
sizes
=
[(
n
p
.
int32
(
2
**
10
),
),
(
n
p
.
int32
(
2
),
np
.
int32
(
2
**
10
),
np
.
int32
(
2
**
10
))]
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
...
...
@@ -1159,11 +1159,11 @@ def test_overflow_gpu_new_backend():
from
theano.gpuarray.type
import
gpuarray_shared_constructor
seed
=
12345
n_substreams
=
7
curr_rstate
=
n
umpy
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
curr_rstate
=
n
p
.
array
([
seed
]
*
6
,
dtype
=
'int32'
)
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
=
n
umpy
.
asarray
(
rstate
)
rstate
=
n
p
.
asarray
(
rstate
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
fct
=
functools
.
partial
(
rng_mrg
.
GPUA_mrg_uniform
.
new
,
rstate
,
ndim
=
None
,
dtype
=
'float32'
)
...
...
@@ -1174,8 +1174,8 @@ def test_overflow_gpu_new_backend():
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
# should support int32 sizes
sizes
=
[(
n
umpy
.
int32
(
2
**
10
),
),
(
n
umpy
.
int32
(
2
),
numpy
.
int32
(
2
**
10
),
numpy
.
int32
(
2
**
10
))]
sizes
=
[(
n
p
.
int32
(
2
**
10
),
),
(
n
p
.
int32
(
2
),
np
.
int32
(
2
**
10
),
np
.
int32
(
2
**
10
))]
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
...
...
@@ -1185,12 +1185,12 @@ def test_validate_input_types_gpuarray_backend():
from
theano.configparser
import
change_flags
with
change_flags
(
compute_test_value
=
"raise"
):
rstate
=
n
umpy
.
zeros
((
7
,
6
),
dtype
=
"int32"
)
rstate
=
n
p
.
zeros
((
7
,
6
),
dtype
=
"int32"
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
dtype
=
"float32"
,
size
=
(
3
,))
if
__name__
==
"__main__"
:
rng
=
MRG_RandomStreams
(
n
umpy
.
random
.
randint
(
2147462579
))
rng
=
MRG_RandomStreams
(
n
p
.
random
.
randint
(
2147462579
))
print
(
theano
.
__file__
)
pvals
=
theano
.
tensor
.
fmatrix
()
for
i
in
range
(
10
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论