Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bea31470
提交
bea31470
authored
4月 03, 2017
作者:
Frédéric Bastien
提交者:
GitHub
4月 03, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5782 from Amrithasuresh/master
Updated numpy as np #4218
上级
56da8ca8
65a48d98
隐藏空白字符变更
内嵌
并排
正在显示
18 个修改的文件
包含
719 行增加
和
719 行删除
+719
-719
fourier.py
theano/sandbox/fourier.py
+7
-7
test_linalg.py
theano/sandbox/linalg/tests/test_linalg.py
+9
-9
minimal.py
theano/sandbox/minimal.py
+5
-5
multinomial.py
theano/sandbox/multinomial.py
+5
-5
rng_mrg.py
theano/sandbox/rng_mrg.py
+55
-55
test_multinomial.py
theano/sandbox/tests/test_multinomial.py
+17
-17
test_multinomial_wo_replacement.py
theano/sandbox/tests/test_multinomial_wo_replacement.py
+31
-31
test_rng_mrg.py
theano/sandbox/tests/test_rng_mrg.py
+119
-119
scan.py
theano/scan_module/scan.py
+10
-10
scan_op.py
theano/scan_module/scan_op.py
+35
-35
scan_opt.py
theano/scan_module/scan_opt.py
+3
-3
scan_perform_ext.py
theano/scan_module/scan_perform_ext.py
+2
-2
scan_utils.py
theano/scan_module/scan_utils.py
+6
-6
test_scan.py
theano/scan_module/tests/test_scan.py
+352
-352
test_scan_checkpoints.py
theano/scan_module/tests/test_scan_checkpoints.py
+4
-4
test_scan_opt.py
theano/scan_module/tests/test_scan_opt.py
+19
-19
test_scan_utils.py
theano/scan_module/tests/test_scan_utils.py
+10
-10
blas.py
theano/tensor/blas.py
+30
-30
没有找到文件。
theano/sandbox/fourier.py
浏览文件 @
bea31470
...
@@ -4,7 +4,7 @@ Provides Ops for FFT and DCT.
...
@@ -4,7 +4,7 @@ Provides Ops for FFT and DCT.
"""
"""
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
numpy.fft
import
numpy.fft
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -126,13 +126,13 @@ def dct_matrix(rows, cols, unitary=True):
...
@@ -126,13 +126,13 @@ def dct_matrix(rows, cols, unitary=True):
This algorithm is adapted from Dan Ellis' Rastmat spec2cep.m, lines 15-20.
This algorithm is adapted from Dan Ellis' Rastmat spec2cep.m, lines 15-20.
"""
"""
rval
=
n
umpy
.
zeros
((
rows
,
cols
))
rval
=
n
p
.
zeros
((
rows
,
cols
))
col_range
=
n
umpy
.
arange
(
cols
)
col_range
=
n
p
.
arange
(
cols
)
scale
=
n
umpy
.
sqrt
(
2.0
/
cols
)
scale
=
n
p
.
sqrt
(
2.0
/
cols
)
for
i
in
xrange
(
rows
):
for
i
in
xrange
(
rows
):
rval
[
i
]
=
n
umpy
.
cos
(
rval
[
i
]
=
n
p
.
cos
(
i
*
(
col_range
*
2
+
1
)
/
(
2.0
*
cols
)
*
n
umpy
.
pi
)
*
scale
i
*
(
col_range
*
2
+
1
)
/
(
2.0
*
cols
)
*
n
p
.
pi
)
*
scale
if
unitary
:
if
unitary
:
rval
[
0
]
*=
n
umpy
.
sqrt
(
0.5
)
rval
[
0
]
*=
n
p
.
sqrt
(
0.5
)
return
rval
return
rval
theano/sandbox/linalg/tests/test_linalg.py
浏览文件 @
bea31470
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
numpy.linalg
import
numpy.linalg
import
theano
import
theano
...
@@ -39,9 +39,9 @@ def test_rop_lop():
...
@@ -39,9 +39,9 @@ def test_rop_lop():
non_sequences
=
[
y
,
mx
,
mv
])
non_sequences
=
[
y
,
mx
,
mv
])
scan_f
=
function
([
mx
,
mv
],
sy
)
scan_f
=
function
([
mx
,
mv
],
sy
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vx
=
n
umpy
.
asarray
(
rng
.
randn
(
4
,
4
),
theano
.
config
.
floatX
)
vx
=
n
p
.
asarray
(
rng
.
randn
(
4
,
4
),
theano
.
config
.
floatX
)
vv
=
n
umpy
.
asarray
(
rng
.
randn
(
4
,
4
),
theano
.
config
.
floatX
)
vv
=
n
p
.
asarray
(
rng
.
randn
(
4
,
4
),
theano
.
config
.
floatX
)
v1
=
rop_f
(
vx
,
vv
)
v1
=
rop_f
(
vx
,
vv
)
v2
=
scan_f
(
vx
,
vv
)
v2
=
scan_f
(
vx
,
vv
)
...
@@ -61,7 +61,7 @@ def test_rop_lop():
...
@@ -61,7 +61,7 @@ def test_rop_lop():
'Op did not raised an error even though the function'
'Op did not raised an error even though the function'
' is not differentiable'
))
' is not differentiable'
))
vv
=
n
umpy
.
asarray
(
rng
.
uniform
(
size
=
(
4
,)),
theano
.
config
.
floatX
)
vv
=
n
p
.
asarray
(
rng
.
uniform
(
size
=
(
4
,)),
theano
.
config
.
floatX
)
yv
=
tensor
.
Lop
(
y
,
mx
,
v
)
yv
=
tensor
.
Lop
(
y
,
mx
,
v
)
lop_f
=
function
([
mx
,
v
],
yv
)
lop_f
=
function
([
mx
,
v
],
yv
)
...
@@ -75,21 +75,21 @@ def test_rop_lop():
...
@@ -75,21 +75,21 @@ def test_rop_lop():
def
test_spectral_radius_bound
():
def
test_spectral_radius_bound
():
tol
=
10
**
(
-
6
)
tol
=
10
**
(
-
6
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
theano
.
tensor
.
matrix
()
x
=
theano
.
tensor
.
matrix
()
radius_bound
=
spectral_radius_bound
(
x
,
5
)
radius_bound
=
spectral_radius_bound
(
x
,
5
)
f
=
theano
.
function
([
x
],
radius_bound
)
f
=
theano
.
function
([
x
],
radius_bound
)
shp
=
(
3
,
4
)
shp
=
(
3
,
4
)
m
=
rng
.
rand
(
*
shp
)
m
=
rng
.
rand
(
*
shp
)
m
=
n
umpy
.
cov
(
m
)
.
astype
(
config
.
floatX
)
m
=
n
p
.
cov
(
m
)
.
astype
(
config
.
floatX
)
radius_bound_theano
=
f
(
m
)
radius_bound_theano
=
f
(
m
)
# test the approximation
# test the approximation
mm
=
m
mm
=
m
for
i
in
range
(
5
):
for
i
in
range
(
5
):
mm
=
n
umpy
.
dot
(
mm
,
mm
)
mm
=
n
p
.
dot
(
mm
,
mm
)
radius_bound_numpy
=
n
umpy
.
trace
(
mm
)
**
(
2
**
(
-
5
))
radius_bound_numpy
=
n
p
.
trace
(
mm
)
**
(
2
**
(
-
5
))
assert
abs
(
radius_bound_numpy
-
radius_bound_theano
)
<
tol
assert
abs
(
radius_bound_numpy
-
radius_bound_theano
)
<
tol
# test the bound
# test the bound
...
...
theano/sandbox/minimal.py
浏览文件 @
bea31470
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
import
unittest
import
numpy
import
numpy
as
np
from
theano
import
gof
,
tensor
,
function
from
theano
import
gof
,
tensor
,
function
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
...
@@ -39,11 +39,11 @@ class Minimal(gof.Op):
...
@@ -39,11 +39,11 @@ class Minimal(gof.Op):
# but do not modify any of the arguments [inplace].
# but do not modify any of the arguments [inplace].
print
(
"perform got
%
i arguments"
%
len
(
inputs
))
print
(
"perform got
%
i arguments"
%
len
(
inputs
))
print
(
"Max of input[0] is "
,
n
umpy
.
max
(
inputs
[
0
]))
print
(
"Max of input[0] is "
,
n
p
.
max
(
inputs
[
0
]))
# return some computed value.
# return some computed value.
# do not return something that is aliased to one of the inputs.
# do not return something that is aliased to one of the inputs.
output
[
0
]
=
n
umpy
.
asarray
(
0
,
dtype
=
'int64'
)
output
[
0
]
=
n
p
.
asarray
(
0
,
dtype
=
'int64'
)
minimal
=
Minimal
()
minimal
=
Minimal
()
...
@@ -55,7 +55,7 @@ minimal = Minimal()
...
@@ -55,7 +55,7 @@ minimal = Minimal()
class
T_minimal
(
unittest
.
TestCase
):
class
T_minimal
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
(
666
))
self
.
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
(
666
))
def
test0
(
self
):
def
test0
(
self
):
A
=
tensor
.
matrix
()
A
=
tensor
.
matrix
()
...
@@ -66,6 +66,6 @@ class T_minimal(unittest.TestCase):
...
@@ -66,6 +66,6 @@ class T_minimal(unittest.TestCase):
print
(
'built'
)
print
(
'built'
)
Aval
=
self
.
rng
.
randn
(
5
,
5
)
Aval
=
self
.
rng
.
randn
(
5
,
5
)
bval
=
n
umpy
.
arange
(
5
,
dtype
=
float
)
bval
=
n
p
.
arange
(
5
,
dtype
=
float
)
f
(
Aval
,
bval
)
f
(
Aval
,
bval
)
print
(
'done'
)
print
(
'done'
)
theano/sandbox/multinomial.py
浏览文件 @
bea31470
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
warnings
import
warnings
import
theano
import
theano
...
@@ -172,7 +172,7 @@ class MultinomialFromUniform(Op):
...
@@ -172,7 +172,7 @@ class MultinomialFromUniform(Op):
raise
ValueError
(
"unis.shape[0] != pvals.shape[0] * n_samples"
,
raise
ValueError
(
"unis.shape[0] != pvals.shape[0] * n_samples"
,
unis
.
shape
[
0
],
pvals
.
shape
[
0
],
n_samples
)
unis
.
shape
[
0
],
pvals
.
shape
[
0
],
n_samples
)
if
z
[
0
]
is
None
or
z
[
0
]
.
shape
!=
pvals
.
shape
:
if
z
[
0
]
is
None
or
z
[
0
]
.
shape
!=
pvals
.
shape
:
z
[
0
]
=
n
umpy
.
zeros
(
pvals
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
z
[
0
]
=
n
p
.
zeros
(
pvals
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
else
:
else
:
z
[
0
]
.
fill
(
0
)
z
[
0
]
.
fill
(
0
)
...
@@ -209,7 +209,7 @@ class MultinomialFromUniform(Op):
...
@@ -209,7 +209,7 @@ class MultinomialFromUniform(Op):
# have the same answer as the c code as in the c code
# have the same answer as the c code as in the c code
# the cumul is in double precission.
# the cumul is in double precission.
cumsum
=
pvals
[
n
]
.
cumsum
(
dtype
=
'float64'
)
cumsum
=
pvals
[
n
]
.
cumsum
(
dtype
=
'float64'
)
z
[
0
][
n
,
n
umpy
.
searchsorted
(
cumsum
,
unis_n
)]
+=
1
z
[
0
][
n
,
n
p
.
searchsorted
(
cumsum
,
unis_n
)]
+=
1
class
ChoiceFromUniform
(
MultinomialFromUniform
):
class
ChoiceFromUniform
(
MultinomialFromUniform
):
...
@@ -380,8 +380,8 @@ class ChoiceFromUniform(MultinomialFromUniform):
...
@@ -380,8 +380,8 @@ class ChoiceFromUniform(MultinomialFromUniform):
else
:
else
:
odtype
=
self
.
odtype
odtype
=
self
.
odtype
if
(
z
[
0
]
is
None
or
if
(
z
[
0
]
is
None
or
not
n
umpy
.
all
(
z
[
0
]
.
shape
==
[
pvals
.
shape
[
0
],
n_samples
])):
not
n
p
.
all
(
z
[
0
]
.
shape
==
[
pvals
.
shape
[
0
],
n_samples
])):
z
[
0
]
=
-
1
*
n
umpy
.
ones
((
pvals
.
shape
[
0
],
n_samples
),
dtype
=
odtype
)
z
[
0
]
=
-
1
*
n
p
.
ones
((
pvals
.
shape
[
0
],
n_samples
),
dtype
=
odtype
)
nb_multi
=
pvals
.
shape
[
0
]
nb_multi
=
pvals
.
shape
[
0
]
nb_outcomes
=
pvals
.
shape
[
1
]
nb_outcomes
=
pvals
.
shape
[
1
]
...
...
theano/sandbox/rng_mrg.py
浏览文件 @
bea31470
...
@@ -8,7 +8,7 @@ http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
...
@@ -8,7 +8,7 @@ http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
warnings
import
warnings
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six
import
integer_types
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -38,7 +38,7 @@ if theano.sandbox.cuda.cuda_available:
...
@@ -38,7 +38,7 @@ if theano.sandbox.cuda.cuda_available:
def
matVecModM
(
A
,
s
,
m
):
def
matVecModM
(
A
,
s
,
m
):
# TODO : need description for method, parameter and return
# TODO : need description for method, parameter and return
assert
A
.
dtype
==
'int64'
assert
A
.
dtype
==
'int64'
return
n
umpy
.
int32
(
numpy
.
sum
((
A
*
s
)
%
m
,
1
)
%
m
)
return
n
p
.
int32
(
np
.
sum
((
A
*
s
)
%
m
,
1
)
%
m
)
def
multMatVect
(
v
,
A
,
m1
,
B
,
m2
):
def
multMatVect
(
v
,
A
,
m1
,
B
,
m2
):
...
@@ -97,7 +97,7 @@ class DotModulo(Op):
...
@@ -97,7 +97,7 @@ class DotModulo(Op):
(
out
,)
=
outputs
(
out
,)
=
outputs
o1
=
matVecModM
(
A
,
s
,
m
)
o1
=
matVecModM
(
A
,
s
,
m
)
o2
=
matVecModM
(
A2
,
s2
,
m2
)
o2
=
matVecModM
(
A2
,
s2
,
m2
)
out
[
0
]
=
n
umpy
.
concatenate
((
o1
,
o2
))
out
[
0
]
=
n
p
.
concatenate
((
o1
,
o2
))
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
6
,)
return
(
6
,)
...
@@ -198,39 +198,39 @@ class DotModulo(Op):
...
@@ -198,39 +198,39 @@ class DotModulo(Op):
# MRG31k3p
# MRG31k3p
# generator constants :
# generator constants :
M1
=
n
umpy
.
asarray
(
numpy
.
int32
(
2147483647
))
# 2^31 - 1
M1
=
n
p
.
asarray
(
np
.
int32
(
2147483647
))
# 2^31 - 1
M2
=
n
umpy
.
asarray
(
numpy
.
int32
(
2147462579
))
# 2^31 - 21069
M2
=
n
p
.
asarray
(
np
.
int32
(
2147462579
))
# 2^31 - 21069
MASK12
=
n
umpy
.
int32
(
511
)
# 2^9 - 1
MASK12
=
n
p
.
int32
(
511
)
# 2^9 - 1
MASK13
=
n
umpy
.
int32
(
16777215
)
# 2^24 - 1
MASK13
=
n
p
.
int32
(
16777215
)
# 2^24 - 1
MASK2
=
n
umpy
.
int32
(
65535
)
# 2^16 - 1
MASK2
=
n
p
.
int32
(
65535
)
# 2^16 - 1
MULT2
=
n
umpy
.
int32
(
21069
)
MULT2
=
n
p
.
int32
(
21069
)
NORM
=
4.656612873077392578125e-10
# 1./2^31
NORM
=
4.656612873077392578125e-10
# 1./2^31
# A1p0 = n
umpy
.asarray([[0, 4194304, 129], [1, 0, 0], [0, 1, 0]],
# A1p0 = n
p
.asarray([[0, 4194304, 129], [1, 0, 0], [0, 1, 0]],
# dtype='int64')
# dtype='int64')
# A2p0 = n
umpy
.asarray([[32768, 0, 32769], [1, 0, 0], [0, 1, 0]],
# A2p0 = n
p
.asarray([[32768, 0, 32769], [1, 0, 0], [0, 1, 0]],
# dtype='int64')
# dtype='int64')
A1p72
=
n
umpy
.
asarray
([[
1516919229
,
758510237
,
499121365
],
A1p72
=
n
p
.
asarray
([[
1516919229
,
758510237
,
499121365
],
[
1884998244
,
1516919229
,
335398200
],
[
1884998244
,
1516919229
,
335398200
],
[
601897748
,
1884998244
,
358115744
]],
[
601897748
,
1884998244
,
358115744
]],
dtype
=
'int64'
)
dtype
=
'int64'
)
A2p72
=
n
umpy
.
asarray
([[
1228857673
,
1496414766
,
954677935
],
A2p72
=
n
p
.
asarray
([[
1228857673
,
1496414766
,
954677935
],
[
1133297478
,
1407477216
,
1496414766
],
[
1133297478
,
1407477216
,
1496414766
],
[
2002613992
,
1639496704
,
1407477216
]],
[
2002613992
,
1639496704
,
1407477216
]],
dtype
=
'int64'
)
dtype
=
'int64'
)
A1p134
=
n
umpy
.
asarray
(
A1p134
=
n
p
.
asarray
(
[[
1702500920
,
1849582496
,
1656874625
],
[[
1702500920
,
1849582496
,
1656874625
],
[
828554832
,
1702500920
,
1512419905
],
[
828554832
,
1702500920
,
1512419905
],
[
1143731069
,
828554832
,
102237247
]],
[
1143731069
,
828554832
,
102237247
]],
dtype
=
'int64'
)
dtype
=
'int64'
)
A2p134
=
n
umpy
.
asarray
(
A2p134
=
n
p
.
asarray
(
[[
796789021
,
1464208080
,
607337906
],
[[
796789021
,
1464208080
,
607337906
],
[
1241679051
,
1431130166
,
1464208080
],
[
1241679051
,
1431130166
,
1464208080
],
[
1401213391
,
1178684362
,
1431130166
]],
[
1401213391
,
1178684362
,
1431130166
]],
dtype
=
'int64'
)
dtype
=
'int64'
)
np_int32_vals
=
[
n
umpy
.
int32
(
i
)
for
i
in
(
0
,
7
,
9
,
15
,
16
,
22
,
24
)]
np_int32_vals
=
[
n
p
.
int32
(
i
)
for
i
in
(
0
,
7
,
9
,
15
,
16
,
22
,
24
)]
def
ff_2p134
(
rstate
):
def
ff_2p134
(
rstate
):
...
@@ -246,14 +246,14 @@ def ff_2p72(rstate):
...
@@ -246,14 +246,14 @@ def ff_2p72(rstate):
def
mrg_next_value
(
rstate
,
new_rstate
):
def
mrg_next_value
(
rstate
,
new_rstate
):
# TODO : need description for method, parameter and return
# TODO : need description for method, parameter and return
x11
,
x12
,
x13
,
x21
,
x22
,
x23
=
rstate
x11
,
x12
,
x13
,
x21
,
x22
,
x23
=
rstate
assert
type
(
x11
)
==
n
umpy
.
int32
assert
type
(
x11
)
==
n
p
.
int32
i0
,
i7
,
i9
,
i15
,
i16
,
i22
,
i24
=
np_int32_vals
i0
,
i7
,
i9
,
i15
,
i16
,
i22
,
i24
=
np_int32_vals
# first component
# first component
y1
=
(((
x12
&
MASK12
)
<<
i22
)
+
(
x12
>>
i9
)
+
y1
=
(((
x12
&
MASK12
)
<<
i22
)
+
(
x12
>>
i9
)
+
((
x13
&
MASK13
)
<<
i7
)
+
(
x13
>>
i24
))
((
x13
&
MASK13
)
<<
i7
)
+
(
x13
>>
i24
))
assert
type
(
y1
)
==
n
umpy
.
int32
assert
type
(
y1
)
==
n
p
.
int32
if
(
y1
<
0
or
y1
>=
M1
):
# must also check overflow
if
(
y1
<
0
or
y1
>=
M1
):
# must also check overflow
y1
-=
M1
y1
-=
M1
y1
+=
x13
y1
+=
x13
...
@@ -266,11 +266,11 @@ def mrg_next_value(rstate, new_rstate):
...
@@ -266,11 +266,11 @@ def mrg_next_value(rstate, new_rstate):
# second component
# second component
y1
=
((
x21
&
MASK2
)
<<
i15
)
+
(
MULT2
*
(
x21
>>
i16
))
y1
=
((
x21
&
MASK2
)
<<
i15
)
+
(
MULT2
*
(
x21
>>
i16
))
assert
type
(
y1
)
==
n
umpy
.
int32
assert
type
(
y1
)
==
n
p
.
int32
if
(
y1
<
0
or
y1
>=
M2
):
if
(
y1
<
0
or
y1
>=
M2
):
y1
-=
M2
y1
-=
M2
y2
=
((
x23
&
MASK2
)
<<
i15
)
+
(
MULT2
*
(
x23
>>
i16
))
y2
=
((
x23
&
MASK2
)
<<
i15
)
+
(
MULT2
*
(
x23
>>
i16
))
assert
type
(
y2
)
==
n
umpy
.
int32
assert
type
(
y2
)
==
n
p
.
int32
if
(
y2
<
0
or
y2
>=
M2
):
if
(
y2
<
0
or
y2
>=
M2
):
y2
-=
M2
y2
-=
M2
y2
+=
x23
y2
+=
x23
...
@@ -286,7 +286,7 @@ def mrg_next_value(rstate, new_rstate):
...
@@ -286,7 +286,7 @@ def mrg_next_value(rstate, new_rstate):
# Must never return either 0 or M1+1
# Must never return either 0 or M1+1
new_rstate
[
...
]
=
[
x11
,
x12
,
x13
,
x21
,
x22
,
x23
]
new_rstate
[
...
]
=
[
x11
,
x12
,
x13
,
x21
,
x22
,
x23
]
assert
new_rstate
.
dtype
==
n
umpy
.
int32
assert
new_rstate
.
dtype
==
n
p
.
int32
if
(
x11
<=
x21
):
if
(
x11
<=
x21
):
return
(
x11
-
x21
+
M1
)
*
NORM
return
(
x11
-
x21
+
M1
)
*
NORM
else
:
else
:
...
@@ -360,22 +360,22 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -360,22 +360,22 @@ class mrg_uniform(mrg_uniform_base):
# some rng_mrg tests) I also add this limit here.
# some rng_mrg tests) I also add this limit here.
raise
ValueError
(
"rng_mrg does not support more then (2**31 -1) samples"
)
raise
ValueError
(
"rng_mrg does not support more then (2**31 -1) samples"
)
rstate
=
n
umpy
.
asarray
(
rstate
)
# bring state from GPU if necessary
rstate
=
n
p
.
asarray
(
rstate
)
# bring state from GPU if necessary
if
not
self
.
inplace
:
if
not
self
.
inplace
:
rstate
=
rstate
.
copy
()
rstate
=
rstate
.
copy
()
n_streams
,
_
=
rstate
.
shape
n_streams
,
_
=
rstate
.
shape
rval
=
n
umpy
.
zeros
(
n_elements
,
dtype
=
self
.
output_type
.
dtype
)
rval
=
n
p
.
zeros
(
n_elements
,
dtype
=
self
.
output_type
.
dtype
)
err_orig
=
n
umpy
.
seterr
(
over
=
'ignore'
)
err_orig
=
n
p
.
seterr
(
over
=
'ignore'
)
try
:
try
:
for
i
in
xrange
(
n_elements
):
for
i
in
xrange
(
n_elements
):
sample
=
mrg_next_value
(
rstate
[
i
%
n_streams
],
sample
=
mrg_next_value
(
rstate
[
i
%
n_streams
],
rstate
[
i
%
n_streams
])
rstate
[
i
%
n_streams
])
rval
[
i
]
=
sample
rval
[
i
]
=
sample
finally
:
finally
:
n
umpy
.
seterr
(
**
err_orig
)
n
p
.
seterr
(
**
err_orig
)
# send to GPU if necessary
# send to GPU if necessary
o_rstate
[
0
]
=
node
.
outputs
[
0
]
.
type
.
filter
(
rstate
)
o_rstate
[
0
]
=
node
.
outputs
[
0
]
.
type
.
filter
(
rstate
)
...
@@ -396,13 +396,13 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -396,13 +396,13 @@ class mrg_uniform(mrg_uniform_base):
'NPY_ARRAY_ENSURECOPY|NPY_ARRAY_C_CONTIGUOUS|'
'NPY_ARRAY_ENSURECOPY|NPY_ARRAY_C_CONTIGUOUS|'
'NPY_ARRAY_ALIGNED'
)
'NPY_ARRAY_ALIGNED'
)
ndim
=
self
.
output_type
.
ndim
ndim
=
self
.
output_type
.
ndim
o_type_num
=
n
umpy
.
asarray
(
0
,
dtype
=
self
.
output_type
.
dtype
)
.
dtype
.
num
o_type_num
=
n
p
.
asarray
(
0
,
dtype
=
self
.
output_type
.
dtype
)
.
dtype
.
num
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
if
self
.
output_type
.
dtype
==
'float32'
:
if
self
.
output_type
.
dtype
==
'float32'
:
otype
=
'float'
otype
=
'float'
NORM
=
'4.6566126e-10f'
# n
umpy
.float32(1.0/(2**31+65))
NORM
=
'4.6566126e-10f'
# n
p
.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# n
umpy
.float32(number * M1) < 1.0
# n
p
.float32(number * M1) < 1.0
else
:
else
:
otype
=
'double'
otype
=
'double'
NORM
=
'4.656612873077392578125e-10'
NORM
=
'4.656612873077392578125e-10'
...
@@ -590,9 +590,9 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
...
@@ -590,9 +590,9 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
def
c_support_code_apply
(
self
,
node
,
nodename
):
def
c_support_code_apply
(
self
,
node
,
nodename
):
if
self
.
output_type
.
dtype
==
'float32'
:
if
self
.
output_type
.
dtype
==
'float32'
:
otype
=
'float'
otype
=
'float'
NORM
=
'4.6566126e-10f'
# n
umpy
.float32(1.0/(2**31+65))
NORM
=
'4.6566126e-10f'
# n
p
.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# n
umpy
.float32(number * M1) < 1.0
# n
p
.float32(number * M1) < 1.0
else
:
else
:
otype
=
'double'
otype
=
'double'
NORM
=
'4.656612873077392578125e-10'
NORM
=
'4.656612873077392578125e-10'
...
@@ -686,7 +686,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
...
@@ -686,7 +686,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
o_rstate
,
o_sample
=
out
o_rstate
,
o_sample
=
out
inplace
=
int
(
self
.
inplace
)
inplace
=
int
(
self
.
inplace
)
ndim
=
self
.
output_type
.
ndim
ndim
=
self
.
output_type
.
ndim
o_type_num
=
n
umpy
.
asarray
(
0
,
dtype
=
self
.
output_type
.
dtype
)
.
dtype
.
num
o_type_num
=
n
p
.
asarray
(
0
,
dtype
=
self
.
output_type
.
dtype
)
.
dtype
.
num
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
if
self
.
output_type
.
dtype
==
'float32'
:
if
self
.
output_type
.
dtype
==
'float32'
:
...
@@ -858,15 +858,15 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
...
@@ -858,15 +858,15 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
otype
=
'ga_half'
otype
=
'ga_half'
# limit the values of the state that we use.
# limit the values of the state that we use.
mask
=
'& 0x7fff'
mask
=
'& 0x7fff'
NORM
=
'3.0518e-05f'
# n
umpy
.float16(1.0/(2**15+8))
NORM
=
'3.0518e-05f'
# n
p
.float16(1.0/(2**15+8))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# n
umpy
.float16(number * (M1 & 0x7fff)) < 1.0
# n
p
.float16(number * (M1 & 0x7fff)) < 1.0
elif
self
.
output_type
.
dtype
==
'float32'
:
elif
self
.
output_type
.
dtype
==
'float32'
:
otype
=
'float'
otype
=
'float'
mask
=
''
mask
=
''
NORM
=
'4.6566126e-10f'
# n
umpy
.float32(1.0/(2**31+65))
NORM
=
'4.6566126e-10f'
# n
p
.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# n
umpy
.float32(number * M1) < 1.0
# n
p
.float32(number * M1) < 1.0
elif
self
.
output_type
.
dtype
==
'float64'
:
elif
self
.
output_type
.
dtype
==
'float64'
:
otype
=
'double'
otype
=
'double'
mask
=
''
mask
=
''
...
@@ -969,7 +969,7 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
...
@@ -969,7 +969,7 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
o_rstate
,
o_sample
=
out
o_rstate
,
o_sample
=
out
inplace
=
int
(
self
.
inplace
)
inplace
=
int
(
self
.
inplace
)
ndim
=
self
.
output_type
.
ndim
ndim
=
self
.
output_type
.
ndim
o_type_num
=
n
umpy
.
asarray
(
0
,
dtype
=
self
.
output_type
.
dtype
)
.
dtype
.
num
o_type_num
=
n
p
.
asarray
(
0
,
dtype
=
self
.
output_type
.
dtype
)
.
dtype
.
num
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
ctx
=
sub
[
'params'
]
ctx
=
sub
[
'params'
]
kname
=
self
.
gpu_kernels
(
node
,
nodename
)[
0
]
.
objvar
kname
=
self
.
gpu_kernels
(
node
,
nodename
)[
0
]
.
objvar
...
@@ -1176,7 +1176,7 @@ class MRG_RandomStreams(object):
...
@@ -1176,7 +1176,7 @@ class MRG_RandomStreams(object):
raise
ValueError
(
'seed should not be 0'
,
seed
)
raise
ValueError
(
'seed should not be 0'
,
seed
)
elif
seed
>=
M2
:
elif
seed
>=
M2
:
raise
ValueError
(
'seed should be less than
%
i'
%
M2
,
seed
)
raise
ValueError
(
'seed should be less than
%
i'
%
M2
,
seed
)
self
.
rstate
=
n
umpy
.
asarray
([
seed
]
*
6
,
dtype
=
'int32'
)
self
.
rstate
=
n
p
.
asarray
([
seed
]
*
6
,
dtype
=
'int32'
)
elif
len
(
seed
)
==
6
:
elif
len
(
seed
)
==
6
:
if
seed
[
0
]
==
0
and
seed
[
1
]
==
0
and
seed
[
2
]
==
0
:
if
seed
[
0
]
==
0
and
seed
[
1
]
==
0
and
seed
[
2
]
==
0
:
raise
ValueError
(
raise
ValueError
(
...
@@ -1192,7 +1192,7 @@ class MRG_RandomStreams(object):
...
@@ -1192,7 +1192,7 @@ class MRG_RandomStreams(object):
raise
ValueError
(
raise
ValueError
(
'The last 3 values of seed should be less than
%
i'
%
M2
,
'The last 3 values of seed should be less than
%
i'
%
M2
,
seed
)
seed
)
self
.
rstate
=
n
umpy
.
asarray
(
seed
,
dtype
=
'int32'
)
self
.
rstate
=
n
p
.
asarray
(
seed
,
dtype
=
'int32'
)
else
:
else
:
raise
TypeError
(
"seed should be 1 integer or 6 integers"
)
raise
TypeError
(
"seed should be 1 integer or 6 integers"
)
...
@@ -1234,7 +1234,7 @@ class MRG_RandomStreams(object):
...
@@ -1234,7 +1234,7 @@ class MRG_RandomStreams(object):
"""
"""
# self.rstate = ff_2p134(self.rstate)
# self.rstate = ff_2p134(self.rstate)
self
.
rstate
=
multMatVect
(
self
.
rstate
,
A1p134
,
M1
,
A2p134
,
M2
)
self
.
rstate
=
multMatVect
(
self
.
rstate
,
A1p134
,
M1
,
A2p134
,
M2
)
assert
self
.
rstate
.
dtype
==
n
umpy
.
int32
assert
self
.
rstate
.
dtype
==
n
p
.
int32
@theano.configparser.change_flags
(
compute_test_value
=
'off'
)
@theano.configparser.change_flags
(
compute_test_value
=
'off'
)
def
get_substream_rstates
(
self
,
n_streams
,
dtype
,
inc_rstate
=
True
):
def
get_substream_rstates
(
self
,
n_streams
,
dtype
,
inc_rstate
=
True
):
...
@@ -1247,7 +1247,7 @@ class MRG_RandomStreams(object):
...
@@ -1247,7 +1247,7 @@ class MRG_RandomStreams(object):
assert
isinstance
(
dtype
,
str
)
assert
isinstance
(
dtype
,
str
)
assert
n_streams
<
2
**
72
assert
n_streams
<
2
**
72
assert
n_streams
>
0
assert
n_streams
>
0
rval
=
n
umpy
.
zeros
((
n_streams
,
6
),
dtype
=
'int32'
)
rval
=
n
p
.
zeros
((
n_streams
,
6
),
dtype
=
'int32'
)
rval
[
0
]
=
self
.
rstate
rval
[
0
]
=
self
.
rstate
# If multMatVect.dot_modulo isn't compiled, compile it.
# If multMatVect.dot_modulo isn't compiled, compile it.
...
@@ -1276,7 +1276,7 @@ class MRG_RandomStreams(object):
...
@@ -1276,7 +1276,7 @@ class MRG_RandomStreams(object):
# HACK - we use fact that int32 and float32 have same size to
# HACK - we use fact that int32 and float32 have same size to
# sneak ints into the CudaNdarray type.
# sneak ints into the CudaNdarray type.
# these *SHOULD NEVER BE USED AS FLOATS*
# these *SHOULD NEVER BE USED AS FLOATS*
tmp_float_buf
=
n
umpy
.
frombuffer
(
rval
.
data
,
dtype
=
'float32'
)
tmp_float_buf
=
n
p
.
frombuffer
(
rval
.
data
,
dtype
=
'float32'
)
assert
tmp_float_buf
.
shape
==
rval
.
shape
assert
tmp_float_buf
.
shape
==
rval
.
shape
assert
(
tmp_float_buf
.
view
(
'int32'
)
==
rval
)
.
all
()
assert
(
tmp_float_buf
.
view
(
'int32'
)
==
rval
)
.
all
()
rval
=
tmp_float_buf
rval
=
tmp_float_buf
...
@@ -1334,9 +1334,9 @@ class MRG_RandomStreams(object):
...
@@ -1334,9 +1334,9 @@ class MRG_RandomStreams(object):
if
isinstance
(
size
,
tuple
):
if
isinstance
(
size
,
tuple
):
msg
=
"size must be a tuple of int or a Theano variable"
msg
=
"size must be a tuple of int or a Theano variable"
assert
all
([
isinstance
(
i
,
(
n
umpy
.
integer
,
integer_types
,
Variable
))
assert
all
([
isinstance
(
i
,
(
n
p
.
integer
,
integer_types
,
Variable
))
for
i
in
size
]),
msg
for
i
in
size
]),
msg
if
any
([
isinstance
(
i
,
(
n
umpy
.
integer
,
integer_types
))
and
i
<=
0
if
any
([
isinstance
(
i
,
(
n
p
.
integer
,
integer_types
))
and
i
<=
0
for
i
in
size
]):
for
i
in
size
]):
raise
ValueError
(
raise
ValueError
(
"The specified size contains a dimension with value <= 0"
,
"The specified size contains a dimension with value <= 0"
,
...
@@ -1558,10 +1558,10 @@ class MRG_RandomStreams(object):
...
@@ -1558,10 +1558,10 @@ class MRG_RandomStreams(object):
evened
=
False
evened
=
False
constant
=
False
constant
=
False
if
(
isinstance
(
size
,
tuple
)
and
if
(
isinstance
(
size
,
tuple
)
and
all
([
isinstance
(
i
,
(
n
umpy
.
integer
,
integer_types
))
for
i
in
size
])):
all
([
isinstance
(
i
,
(
n
p
.
integer
,
integer_types
))
for
i
in
size
])):
constant
=
True
constant
=
True
# Force dtype because it defaults to float when size is empty
# Force dtype because it defaults to float when size is empty
n_samples
=
n
umpy
.
prod
(
size
,
dtype
=
'int64'
)
n_samples
=
n
p
.
prod
(
size
,
dtype
=
'int64'
)
if
n_samples
%
2
==
1
:
if
n_samples
%
2
==
1
:
n_samples
+=
1
n_samples
+=
1
...
@@ -1583,14 +1583,14 @@ class MRG_RandomStreams(object):
...
@@ -1583,14 +1583,14 @@ class MRG_RandomStreams(object):
sqrt_ln_U1
=
sqrt
(
-
2.0
*
log
(
U1
))
sqrt_ln_U1
=
sqrt
(
-
2.0
*
log
(
U1
))
# TypeError: 'TensorVariable' object does not support item assignment
# TypeError: 'TensorVariable' object does not support item assignment
# so this doesn't work...
# so this doesn't work...
# normal_samples[:n_samples/2] = sqrt_ln_U1 * cos(2.0*n
umpy
.pi*U2)
# normal_samples[:n_samples/2] = sqrt_ln_U1 * cos(2.0*n
p
.pi*U2)
# normal_samples[n_samples/2:] = sqrt_ln_U1 * sin(2.0*n
umpy
.pi*U2)
# normal_samples[n_samples/2:] = sqrt_ln_U1 * sin(2.0*n
p
.pi*U2)
# so trying this instead
# so trying this instead
first_half
=
sqrt_ln_U1
*
cos
(
first_half
=
sqrt_ln_U1
*
cos
(
n
umpy
.
array
(
2.0
*
numpy
.
pi
,
dtype
=
dtype
)
*
U2
)
n
p
.
array
(
2.0
*
np
.
pi
,
dtype
=
dtype
)
*
U2
)
second_half
=
sqrt_ln_U1
*
sin
(
second_half
=
sqrt_ln_U1
*
sin
(
n
umpy
.
array
(
2.0
*
numpy
.
pi
,
dtype
=
dtype
)
*
U2
)
n
p
.
array
(
2.0
*
np
.
pi
,
dtype
=
dtype
)
*
U2
)
normal_samples
=
join
(
0
,
first_half
,
second_half
)
normal_samples
=
join
(
0
,
first_half
,
second_half
)
final_samples
=
None
final_samples
=
None
...
...
theano/sandbox/tests/test_multinomial.py
浏览文件 @
bea31470
...
@@ -4,7 +4,7 @@ import sys
...
@@ -4,7 +4,7 @@ import sys
from
six
import
reraise
from
six
import
reraise
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
config
,
function
,
tensor
from
theano
import
config
,
function
,
tensor
...
@@ -40,9 +40,9 @@ def test_n_samples_1():
...
@@ -40,9 +40,9 @@ def test_n_samples_1():
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
for
i
in
[
1
,
5
,
10
,
100
,
1000
,
10000
]:
for
i
in
[
1
,
5
,
10
,
100
,
1000
,
10000
]:
uni
=
n
umpy
.
random
.
rand
(
2
*
i
)
.
astype
(
config
.
floatX
)
uni
=
n
p
.
random
.
rand
(
2
*
i
)
.
astype
(
config
.
floatX
)
res
=
f
([[
1.0
,
0.0
],
[
0.0
,
1.0
]],
uni
,
i
)
res
=
f
([[
1.0
,
0.0
],
[
0.0
,
1.0
]],
uni
,
i
)
utt
.
assert_allclose
(
res
,
[[
i
*
1.0
,
0.0
],
[
0.0
,
i
*
1.0
]])
utt
.
assert_allclose
(
res
,
[[
i
*
1.0
,
0.0
],
[
0.0
,
i
*
1.0
]])
...
@@ -55,17 +55,17 @@ def test_n_samples_2():
...
@@ -55,17 +55,17 @@ def test_n_samples_2():
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
f
=
function
([
p
,
u
,
n
],
m
,
allow_input_downcast
=
True
)
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
for
i
in
[
1
,
5
,
10
,
100
,
1000
]:
for
i
in
[
1
,
5
,
10
,
100
,
1000
]:
uni
=
n
umpy
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
uni
=
n
p
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
pvals
=
n
umpy
.
random
.
randint
(
1
,
1000
,
(
1
,
1000
))
.
astype
(
config
.
floatX
)
pvals
=
n
p
.
random
.
randint
(
1
,
1000
,
(
1
,
1000
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
res
=
f
(
pvals
,
uni
,
i
)
res
=
f
(
pvals
,
uni
,
i
)
assert
res
.
sum
()
==
i
assert
res
.
sum
()
==
i
for
i
in
[
1
,
5
,
10
,
100
,
1000
]:
for
i
in
[
1
,
5
,
10
,
100
,
1000
]:
uni
=
n
umpy
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
uni
=
n
p
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
pvals
=
n
umpy
.
random
.
randint
(
pvals
=
n
p
.
random
.
randint
(
1
,
1000000
,
(
1
,
1000000
))
.
astype
(
config
.
floatX
)
1
,
1000000
,
(
1
,
1000000
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
res
=
f
(
pvals
,
uni
,
i
)
res
=
f
(
pvals
,
uni
,
i
)
...
@@ -104,8 +104,8 @@ def test_n_samples_compatibility():
...
@@ -104,8 +104,8 @@ def test_n_samples_compatibility():
raise
raise
f
=
theano
.
function
([
X
],
samples
)
f
=
theano
.
function
([
X
],
samples
)
res
=
f
(
n
umpy
.
random
.
randn
(
20
,
10
))
res
=
f
(
n
p
.
random
.
randn
(
20
,
10
))
assert
n
umpy
.
all
(
res
.
sum
(
axis
=
1
)
==
1
)
assert
n
p
.
all
(
res
.
sum
(
axis
=
1
)
==
1
)
def
test_multinomial_0
():
def
test_multinomial_0
():
...
@@ -160,9 +160,9 @@ def test_multinomial_large():
...
@@ -160,9 +160,9 @@ def test_multinomial_large():
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
n
umpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
n
p
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
n
umpy
.
ones_like
(
pval
[:,
0
])
*
0.5
uval
=
n
p
.
ones_like
(
pval
[:,
0
])
*
0.5
mval
=
f
(
pval
,
uval
)
mval
=
f
(
pval
,
uval
)
assert
mval
.
shape
==
pval
.
shape
assert
mval
.
shape
==
pval
.
shape
...
@@ -175,7 +175,7 @@ def test_multinomial_large():
...
@@ -175,7 +175,7 @@ def test_multinomial_large():
else
:
else
:
raise
NotImplementedError
(
config
.
cast_policy
)
raise
NotImplementedError
(
config
.
cast_policy
)
utt
.
assert_allclose
(
mval
.
sum
(
axis
=
1
),
2
)
utt
.
assert_allclose
(
mval
.
sum
(
axis
=
1
),
2
)
asdf
=
n
umpy
.
asarray
([
0
,
0
,
2
,
0
])
+
0
*
pval
asdf
=
n
p
.
asarray
([
0
,
0
,
2
,
0
])
+
0
*
pval
utt
.
assert_allclose
(
mval
,
asdf
)
# broadcast over all rows
utt
.
assert_allclose
(
mval
,
asdf
)
# broadcast over all rows
run_with_c
(
body
)
run_with_c
(
body
)
if
cuda
.
cuda_available
:
if
cuda
.
cuda_available
:
...
@@ -216,9 +216,9 @@ def test_gpu_opt():
...
@@ -216,9 +216,9 @@ def test_gpu_opt():
f
=
function
([
p
,
u
],
m_gpu
,
allow_input_downcast
=
True
,
mode
=
get_mode
(
True
))
f
=
function
([
p
,
u
],
m_gpu
,
allow_input_downcast
=
True
,
mode
=
get_mode
(
True
))
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
n
umpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
n
p
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
n
umpy
.
ones_like
(
pval
[:,
0
])
*
0.5
uval
=
n
p
.
ones_like
(
pval
[:,
0
])
*
0.5
f
(
pval
,
uval
)
f
(
pval
,
uval
)
# Test with a row, it was failing in the past.
# Test with a row, it was failing in the past.
...
@@ -230,7 +230,7 @@ def test_gpu_opt():
...
@@ -230,7 +230,7 @@ def test_gpu_opt():
f
=
function
([
r
,
u
],
m_gpu
,
allow_input_downcast
=
True
,
mode
=
get_mode
(
True
))
f
=
function
([
r
,
u
],
m_gpu
,
allow_input_downcast
=
True
,
mode
=
get_mode
(
True
))
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
assert
any
([
type
(
node
.
op
)
is
multinomial
.
GpuMultinomialFromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
n
umpy
.
arange
(
1
*
4
,
dtype
=
'float32'
)
.
reshape
((
1
,
4
))
+
0.1
pval
=
n
p
.
arange
(
1
*
4
,
dtype
=
'float32'
)
.
reshape
((
1
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
n
umpy
.
ones_like
(
pval
[:,
0
])
*
0.5
uval
=
n
p
.
ones_like
(
pval
[:,
0
])
*
0.5
f
(
pval
,
uval
)
f
(
pval
,
uval
)
theano/sandbox/tests/test_multinomial_wo_replacement.py
浏览文件 @
bea31470
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
os
import
os
from
theano
import
config
,
function
,
tensor
from
theano
import
config
,
function
,
tensor
from
theano.compat
import
PY3
from
theano.compat
import
PY3
...
@@ -24,25 +24,25 @@ class test_OP(unittest.TestCase):
...
@@ -24,25 +24,25 @@ class test_OP(unittest.TestCase):
n_elements
=
1000
n_elements
=
1000
all_indices
=
range
(
n_elements
)
all_indices
=
range
(
n_elements
)
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
expected
=
[
expected
=
[
n
umpy
.
asarray
([[
931
,
318
,
185
,
209
,
559
]]),
n
p
.
asarray
([[
931
,
318
,
185
,
209
,
559
]]),
n
umpy
.
asarray
([[
477
,
887
,
2
,
717
,
333
,
665
,
159
,
559
,
348
,
136
]]),
n
p
.
asarray
([[
477
,
887
,
2
,
717
,
333
,
665
,
159
,
559
,
348
,
136
]]),
n
umpy
.
asarray
([[
546
,
28
,
79
,
665
,
295
,
779
,
433
,
531
,
411
,
716
,
244
,
234
,
70
,
88
,
612
,
639
,
383
,
335
,
n
p
.
asarray
([[
546
,
28
,
79
,
665
,
295
,
779
,
433
,
531
,
411
,
716
,
244
,
234
,
70
,
88
,
612
,
639
,
383
,
335
,
451
,
100
,
175
,
492
,
848
,
771
,
559
,
214
,
568
,
596
,
370
,
486
,
855
,
925
,
138
,
300
,
528
,
507
,
451
,
100
,
175
,
492
,
848
,
771
,
559
,
214
,
568
,
596
,
370
,
486
,
855
,
925
,
138
,
300
,
528
,
507
,
730
,
199
,
882
,
357
,
58
,
195
,
705
,
900
,
66
,
468
,
513
,
410
,
816
,
672
]])]
730
,
199
,
882
,
357
,
58
,
195
,
705
,
900
,
66
,
468
,
513
,
410
,
816
,
672
]])]
for
i
in
[
5
,
10
,
50
,
100
,
500
,
n_elements
]:
for
i
in
[
5
,
10
,
50
,
100
,
500
,
n_elements
]:
uni
=
n
umpy
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
uni
=
n
p
.
random
.
rand
(
i
)
.
astype
(
config
.
floatX
)
pvals
=
n
umpy
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
=
n
p
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
res
=
f
(
pvals
,
uni
,
i
)
res
=
f
(
pvals
,
uni
,
i
)
for
ii
in
range
(
len
(
expected
)):
for
ii
in
range
(
len
(
expected
)):
if
expected
[
ii
]
.
shape
==
res
.
shape
:
if
expected
[
ii
]
.
shape
==
res
.
shape
:
assert
(
expected
[
ii
]
==
res
)
.
all
()
assert
(
expected
[
ii
]
==
res
)
.
all
()
res
=
n
umpy
.
squeeze
(
res
)
res
=
n
p
.
squeeze
(
res
)
assert
len
(
res
)
==
i
assert
len
(
res
)
==
i
assert
n
umpy
.
all
(
numpy
.
in1d
(
numpy
.
unique
(
res
),
all_indices
)),
res
assert
n
p
.
all
(
np
.
in1d
(
np
.
unique
(
res
),
all_indices
)),
res
def
test_fail_select_alot
(
self
):
def
test_fail_select_alot
(
self
):
"""
"""
...
@@ -58,9 +58,9 @@ class test_OP(unittest.TestCase):
...
@@ -58,9 +58,9 @@ class test_OP(unittest.TestCase):
n_elements
=
100
n_elements
=
100
n_selected
=
200
n_selected
=
200
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
uni
=
n
umpy
.
random
.
rand
(
n_selected
)
.
astype
(
config
.
floatX
)
uni
=
n
p
.
random
.
rand
(
n_selected
)
.
astype
(
config
.
floatX
)
pvals
=
n
umpy
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
=
n
p
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
self
.
assertRaises
(
ValueError
,
f
,
pvals
,
uni
,
n_selected
)
self
.
assertRaises
(
ValueError
,
f
,
pvals
,
uni
,
n_selected
)
...
@@ -79,18 +79,18 @@ class test_OP(unittest.TestCase):
...
@@ -79,18 +79,18 @@ class test_OP(unittest.TestCase):
n_elements
=
100
n_elements
=
100
n_selected
=
10
n_selected
=
10
mean_rtol
=
0.0005
mean_rtol
=
0.0005
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
pvals
=
n
umpy
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
=
n
p
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
avg_pvals
=
n
umpy
.
zeros
((
n_elements
,),
dtype
=
config
.
floatX
)
avg_pvals
=
n
p
.
zeros
((
n_elements
,),
dtype
=
config
.
floatX
)
for
rep
in
range
(
10000
):
for
rep
in
range
(
10000
):
uni
=
n
umpy
.
random
.
rand
(
n_selected
)
.
astype
(
config
.
floatX
)
uni
=
n
p
.
random
.
rand
(
n_selected
)
.
astype
(
config
.
floatX
)
res
=
f
(
pvals
,
uni
,
n_selected
)
res
=
f
(
pvals
,
uni
,
n_selected
)
res
=
n
umpy
.
squeeze
(
res
)
res
=
n
p
.
squeeze
(
res
)
avg_pvals
[
res
]
+=
1
avg_pvals
[
res
]
+=
1
avg_pvals
/=
avg_pvals
.
sum
()
avg_pvals
/=
avg_pvals
.
sum
()
avg_diff
=
n
umpy
.
mean
(
abs
(
avg_pvals
-
pvals
))
avg_diff
=
n
p
.
mean
(
abs
(
avg_pvals
-
pvals
))
assert
avg_diff
<
mean_rtol
,
avg_diff
assert
avg_diff
<
mean_rtol
,
avg_diff
...
@@ -110,14 +110,14 @@ class test_function(unittest.TestCase):
...
@@ -110,14 +110,14 @@ class test_function(unittest.TestCase):
n_elements
=
1000
n_elements
=
1000
all_indices
=
range
(
n_elements
)
all_indices
=
range
(
n_elements
)
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
for
i
in
[
5
,
10
,
50
,
100
,
500
,
n_elements
]:
for
i
in
[
5
,
10
,
50
,
100
,
500
,
n_elements
]:
pvals
=
n
umpy
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
=
n
p
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
res
=
f
(
pvals
,
i
)
res
=
f
(
pvals
,
i
)
res
=
n
umpy
.
squeeze
(
res
)
res
=
n
p
.
squeeze
(
res
)
assert
len
(
res
)
==
i
assert
len
(
res
)
==
i
assert
n
umpy
.
all
(
numpy
.
in1d
(
numpy
.
unique
(
res
),
all_indices
)),
res
assert
n
p
.
all
(
np
.
in1d
(
np
.
unique
(
res
),
all_indices
)),
res
def
test_fail_select_alot
(
self
):
def
test_fail_select_alot
(
self
):
"""
"""
...
@@ -134,8 +134,8 @@ class test_function(unittest.TestCase):
...
@@ -134,8 +134,8 @@ class test_function(unittest.TestCase):
n_elements
=
100
n_elements
=
100
n_selected
=
200
n_selected
=
200
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
pvals
=
n
umpy
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
=
n
p
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
self
.
assertRaises
(
ValueError
,
f
,
pvals
,
n_selected
)
self
.
assertRaises
(
ValueError
,
f
,
pvals
,
n_selected
)
...
@@ -155,17 +155,17 @@ class test_function(unittest.TestCase):
...
@@ -155,17 +155,17 @@ class test_function(unittest.TestCase):
n_elements
=
100
n_elements
=
100
n_selected
=
10
n_selected
=
10
mean_rtol
=
0.0005
mean_rtol
=
0.0005
n
umpy
.
random
.
seed
(
12345
)
n
p
.
random
.
seed
(
12345
)
pvals
=
n
umpy
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
=
n
p
.
random
.
randint
(
1
,
100
,
(
1
,
n_elements
))
.
astype
(
config
.
floatX
)
pvals
/=
pvals
.
sum
(
1
)
pvals
/=
pvals
.
sum
(
1
)
avg_pvals
=
n
umpy
.
zeros
((
n_elements
,),
dtype
=
config
.
floatX
)
avg_pvals
=
n
p
.
zeros
((
n_elements
,),
dtype
=
config
.
floatX
)
for
rep
in
range
(
10000
):
for
rep
in
range
(
10000
):
res
=
f
(
pvals
,
n_selected
)
res
=
f
(
pvals
,
n_selected
)
res
=
n
umpy
.
squeeze
(
res
)
res
=
n
p
.
squeeze
(
res
)
avg_pvals
[
res
]
+=
1
avg_pvals
[
res
]
+=
1
avg_pvals
/=
avg_pvals
.
sum
()
avg_pvals
/=
avg_pvals
.
sum
()
avg_diff
=
n
umpy
.
mean
(
abs
(
avg_pvals
-
pvals
))
avg_diff
=
n
p
.
mean
(
abs
(
avg_pvals
-
pvals
))
assert
avg_diff
<
mean_rtol
assert
avg_diff
<
mean_rtol
def
test_unpickle_legacy_op
(
self
):
def
test_unpickle_legacy_op
(
self
):
...
...
theano/sandbox/tests/test_rng_mrg.py
浏览文件 @
bea31470
...
@@ -8,7 +8,7 @@ import functools
...
@@ -8,7 +8,7 @@ import functools
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
nose.tools
import
assert_raises
from
nose.tools
import
assert_raises
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
...
@@ -44,9 +44,9 @@ utt.seed_rng()
...
@@ -44,9 +44,9 @@ utt.seed_rng()
# 12 streams
# 12 streams
# 7 substreams for each stream
# 7 substreams for each stream
# 5 samples drawn from each substream
# 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'
,
'sandbox'
,
'samples_MRG31k3p_12_7_5.txt'
))
'samples_MRG31k3p_12_7_5.txt'
))
def
test_deterministic
():
def
test_deterministic
():
...
@@ -65,15 +65,15 @@ def test_deterministic():
...
@@ -65,15 +65,15 @@ def test_deterministic():
fsample1
=
f
()
fsample1
=
f
()
fsample2
=
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
)
R2
=
MRG_RandomStreams
(
seed
=
seed
,
use_cuda
=
use_cuda
)
u2
=
R2
.
uniform
(
size
=
sample_size
)
u2
=
R2
.
uniform
(
size
=
sample_size
)
g
=
theano
.
function
([],
u2
)
g
=
theano
.
function
([],
u2
)
gsample1
=
g
()
gsample1
=
g
()
gsample2
=
g
()
gsample2
=
g
()
assert
n
umpy
.
allclose
(
fsample1
,
gsample1
)
assert
n
p
.
allclose
(
fsample1
,
gsample1
)
assert
n
umpy
.
allclose
(
fsample2
,
gsample2
)
assert
n
p
.
allclose
(
fsample2
,
gsample2
)
def
test_consistency_randomstreams
():
def
test_consistency_randomstreams
():
...
@@ -102,12 +102,12 @@ def test_consistency_randomstreams():
...
@@ -102,12 +102,12 @@ def test_consistency_randomstreams():
for
j
in
range
(
n_samples
):
for
j
in
range
(
n_samples
):
s
=
f
()
s
=
f
()
stream_samples
.
append
(
s
)
stream_samples
.
append
(
s
)
stream_samples
=
n
umpy
.
array
(
stream_samples
)
stream_samples
=
n
p
.
array
(
stream_samples
)
stream_samples
=
stream_samples
.
T
.
flatten
()
stream_samples
=
stream_samples
.
T
.
flatten
()
samples
.
append
(
stream_samples
)
samples
.
append
(
stream_samples
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_get_substream_rstates
():
def
test_get_substream_rstates
():
...
@@ -117,7 +117,7 @@ def test_get_substream_rstates():
...
@@ -117,7 +117,7 @@ def test_get_substream_rstates():
n_streams
=
100
n_streams
=
100
dtype
=
'float32'
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
)
rng
.
get_substream_rstates
(
n_streams
,
dtype
)
...
@@ -137,13 +137,13 @@ def test_consistency_cpu_serial():
...
@@ -137,13 +137,13 @@ def test_consistency_cpu_serial():
n_substreams
=
7
n_substreams
=
7
samples
=
[]
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
):
for
i
in
range
(
n_streams
):
stream_rstate
=
curr_rstate
.
copy
()
stream_rstate
=
curr_rstate
.
copy
()
for
j
in
range
(
n_substreams
):
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'
))
dtype
=
'int32'
))
new_rstate
,
sample
=
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
new_rstate
,
sample
=
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
dtype
=
config
.
floatX
,
dtype
=
config
.
floatX
,
size
=
(
1
,))
size
=
(
1
,))
...
@@ -164,8 +164,8 @@ def test_consistency_cpu_serial():
...
@@ -164,8 +164,8 @@ def test_consistency_cpu_serial():
# next stream
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_cpu_parallel
():
def
test_consistency_cpu_parallel
():
...
@@ -180,14 +180,14 @@ def test_consistency_cpu_parallel():
...
@@ -180,14 +180,14 @@ def test_consistency_cpu_parallel():
n_substreams
=
7
# 7 samples will be drawn in parallel
n_substreams
=
7
# 7 samples will be drawn in parallel
samples
=
[]
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
):
for
i
in
range
(
n_streams
):
stream_samples
=
[]
stream_samples
=
[]
rstate
=
[
curr_rstate
.
copy
()]
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
=
n
umpy
.
asarray
(
rstate
)
rstate
=
n
p
.
asarray
(
rstate
)
rstate
=
theano
.
shared
(
rstate
)
rstate
=
theano
.
shared
(
rstate
)
new_rstate
,
sample
=
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
new_rstate
,
sample
=
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
...
@@ -205,13 +205,13 @@ def test_consistency_cpu_parallel():
...
@@ -205,13 +205,13 @@ def test_consistency_cpu_parallel():
s
=
f
()
s
=
f
()
stream_samples
.
append
(
s
)
stream_samples
.
append
(
s
)
samples
.
append
(
n
umpy
.
array
(
stream_samples
)
.
T
.
flatten
())
samples
.
append
(
n
p
.
array
(
stream_samples
)
.
T
.
flatten
())
# next stream
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_GPU_serial
():
def
test_consistency_GPU_serial
():
...
@@ -233,16 +233,16 @@ def test_consistency_GPU_serial():
...
@@ -233,16 +233,16 @@ def test_consistency_GPU_serial():
n_substreams
=
7
n_substreams
=
7
samples
=
[]
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
):
for
i
in
range
(
n_streams
):
stream_rstate
=
curr_rstate
.
copy
()
stream_rstate
=
curr_rstate
.
copy
()
for
j
in
range
(
n_substreams
):
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
# HACK - we transfer these int32 to the GPU memory as float32
# (reinterpret_cast)
# (reinterpret_cast)
tmp_float_buf
=
n
umpy
.
frombuffer
(
substream_rstate
.
data
,
tmp_float_buf
=
n
p
.
frombuffer
(
substream_rstate
.
data
,
dtype
=
'float32'
)
dtype
=
'float32'
)
# Transfer to device
# Transfer to device
rstate
=
float32_shared_constructor
(
tmp_float_buf
)
rstate
=
float32_shared_constructor
(
tmp_float_buf
)
...
@@ -269,8 +269,8 @@ def test_consistency_GPU_serial():
...
@@ -269,8 +269,8 @@ def test_consistency_GPU_serial():
# next stream
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_GPU_parallel
():
def
test_consistency_GPU_parallel
():
...
@@ -293,17 +293,17 @@ def test_consistency_GPU_parallel():
...
@@ -293,17 +293,17 @@ def test_consistency_GPU_parallel():
n_substreams
=
7
# 7 samples will be drawn in parallel
n_substreams
=
7
# 7 samples will be drawn in parallel
samples
=
[]
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
):
for
i
in
range
(
n_streams
):
stream_samples
=
[]
stream_samples
=
[]
rstate
=
[
curr_rstate
.
copy
()]
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
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
# HACK - transfer these int32 to the GPU memory as float32
# (reinterpret_cast)
# (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
# Transfer to device
rstate
=
float32_shared_constructor
(
tmp_float_buf
)
rstate
=
float32_shared_constructor
(
tmp_float_buf
)
...
@@ -325,13 +325,13 @@ def test_consistency_GPU_parallel():
...
@@ -325,13 +325,13 @@ def test_consistency_GPU_parallel():
s
=
f
()
s
=
f
()
stream_samples
.
append
(
s
)
stream_samples
.
append
(
s
)
samples
.
append
(
n
umpy
.
array
(
stream_samples
)
.
T
.
flatten
())
samples
.
append
(
n
p
.
array
(
stream_samples
)
.
T
.
flatten
())
# next stream
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_GPU_nstreams_limit
():
def
test_GPU_nstreams_limit
():
...
@@ -373,13 +373,13 @@ def test_consistency_GPUA_serial():
...
@@ -373,13 +373,13 @@ def test_consistency_GPUA_serial():
n_substreams
=
7
n_substreams
=
7
samples
=
[]
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
):
for
i
in
range
(
n_streams
):
stream_rstate
=
curr_rstate
.
copy
()
stream_rstate
=
curr_rstate
.
copy
()
for
j
in
range
(
n_substreams
):
for
j
in
range
(
n_substreams
):
substream_rstate
=
n
umpy
.
array
([
stream_rstate
.
copy
()],
substream_rstate
=
n
p
.
array
([
stream_rstate
.
copy
()],
dtype
=
'int32'
)
dtype
=
'int32'
)
# Transfer to device
# Transfer to device
rstate
=
gpuarray_shared_constructor
(
substream_rstate
)
rstate
=
gpuarray_shared_constructor
(
substream_rstate
)
...
@@ -407,8 +407,8 @@ def test_consistency_GPUA_serial():
...
@@ -407,8 +407,8 @@ def test_consistency_GPUA_serial():
# next stream
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_consistency_GPUA_parallel
():
def
test_consistency_GPUA_parallel
():
...
@@ -424,14 +424,14 @@ def test_consistency_GPUA_parallel():
...
@@ -424,14 +424,14 @@ def test_consistency_GPUA_parallel():
n_substreams
=
7
# 7 samples will be drawn in parallel
n_substreams
=
7
# 7 samples will be drawn in parallel
samples
=
[]
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
):
for
i
in
range
(
n_streams
):
stream_samples
=
[]
stream_samples
=
[]
rstate
=
[
curr_rstate
.
copy
()]
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
=
n
umpy
.
asarray
(
rstate
)
rstate
=
n
p
.
asarray
(
rstate
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
new_rstate
,
sample
=
rng_mrg
.
GPUA_mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
new_rstate
,
sample
=
rng_mrg
.
GPUA_mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
...
@@ -452,13 +452,13 @@ def test_consistency_GPUA_parallel():
...
@@ -452,13 +452,13 @@ def test_consistency_GPUA_parallel():
s
=
f
()
s
=
f
()
stream_samples
.
append
(
s
)
stream_samples
.
append
(
s
)
samples
.
append
(
n
umpy
.
array
(
stream_samples
)
.
T
.
flatten
())
samples
.
append
(
n
p
.
array
(
stream_samples
)
.
T
.
flatten
())
# next stream
# next stream
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
curr_rstate
=
rng_mrg
.
ff_2p134
(
curr_rstate
)
samples
=
n
umpy
.
array
(
samples
)
.
flatten
()
samples
=
n
p
.
array
(
samples
)
.
flatten
()
assert
(
n
umpy
.
allclose
(
samples
,
java_samples
))
assert
(
n
p
.
allclose
(
samples
,
java_samples
))
def
test_GPUA_full_fill
():
def
test_GPUA_full_fill
():
...
@@ -496,16 +496,16 @@ def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=None,
...
@@ -496,16 +496,16 @@ def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=None,
ival
=
f
(
*
inputs
)
ival
=
f
(
*
inputs
)
assert
ival
.
shape
==
sample_size
assert
ival
.
shape
==
sample_size
dt
+=
time
.
time
()
-
t0
dt
+=
time
.
time
()
-
t0
ival
=
n
umpy
.
asarray
(
ival
)
ival
=
n
p
.
asarray
(
ival
)
if
i
==
0
:
if
i
==
0
:
mean
=
n
umpy
.
array
(
ival
,
copy
=
True
)
mean
=
n
p
.
array
(
ival
,
copy
=
True
)
avg_var
=
n
umpy
.
mean
((
ival
-
target_avg
)
**
2
)
avg_var
=
n
p
.
mean
((
ival
-
target_avg
)
**
2
)
min_
=
ival
.
min
()
min_
=
ival
.
min
()
max_
=
ival
.
max
()
max_
=
ival
.
max
()
else
:
else
:
alpha
=
1.0
/
(
1
+
i
)
alpha
=
1.0
/
(
1
+
i
)
mean
=
alpha
*
ival
+
(
1
-
alpha
)
*
mean
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
)
(
1
-
alpha
)
*
avg_var
)
min_
=
min
(
min_
,
ival
.
min
())
min_
=
min
(
min_
,
ival
.
min
())
max_
=
max
(
max_
,
ival
.
max
())
max_
=
max
(
max_
,
ival
.
max
())
...
@@ -514,19 +514,19 @@ def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=None,
...
@@ -514,19 +514,19 @@ def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=None,
assert
max_
<
1
assert
max_
<
1
if
hasattr
(
target_avg
,
'shape'
):
# looks if target_avg is an array
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
# 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
))
'bad mean?
%
s
%
s'
%
(
mean
,
target_avg
))
else
:
else
:
# if target_avg is a scalar, then we can do the mean of
# if target_avg is a scalar, then we can do the mean of
# `mean` to get something more precise
# `mean` to get something more precise
mean
=
n
umpy
.
mean
(
mean
)
mean
=
n
p
.
mean
(
mean
)
# print prefix, 'mean', mean
# print prefix, 'mean', mean
assert
abs
(
mean
-
target_avg
)
<
mean_rtol
*
(
1
+
abs
(
target_avg
)),
(
assert
abs
(
mean
-
target_avg
)
<
mean_rtol
*
(
1
+
abs
(
target_avg
)),
(
'bad mean?
%
f
%
f'
%
(
mean
,
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, 'var', avg_var
# print prefix, 'std', std
# print prefix, 'std', std
if
target_std
is
not
None
:
if
target_std
is
not
None
:
...
@@ -556,9 +556,9 @@ def test_uniform():
...
@@ -556,9 +556,9 @@ def test_uniform():
for
size
,
const_size
,
var_input
,
input
in
[
for
size
,
const_size
,
var_input
,
input
in
[
(
sample_size
,
sample_size
,
[],
[]),
(
sample_size
,
sample_size
,
[],
[]),
(
x
.
shape
,
sample_size
,
[
x
],
(
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
],
((
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)
# test empty size (scalar)
((),
(),
[],
[]),
((),
(),
[],
[]),
]:
]:
...
@@ -586,7 +586,7 @@ def test_uniform():
...
@@ -586,7 +586,7 @@ def test_uniform():
# print cpu_out[-1, -10:]
# print cpu_out[-1, -10:]
# Increase the number of steps if sizes implies only a few samples
# 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
steps_
=
steps
*
100
else
:
else
:
steps_
=
steps
steps_
=
steps
...
@@ -607,15 +607,15 @@ def test_uniform():
...
@@ -607,15 +607,15 @@ def test_uniform():
theano
.
sandbox
.
rng_mrg
.
GPU_mrg_uniform
)
theano
.
sandbox
.
rng_mrg
.
GPU_mrg_uniform
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
# theano.printing.debugprint(f)
# 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: random?[:10], random?[-10:]'
# print gpu_out[0, 0:10]
# print gpu_out[0, 0:10]
# print gpu_out[-1, -10:]
# print gpu_out[-1, -10:]
basictest
(
f
,
steps_
,
const_size
,
prefix
=
'mrg gpu'
,
inputs
=
input
)
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
)
decimal
=
6
)
# print ''
# print ''
# print 'ON CPU w Numpy with size=(%s):' % str(size)
# print 'ON CPU w Numpy with size=(%s):' % str(size)
...
@@ -633,7 +633,7 @@ def test_broadcastable():
...
@@ -633,7 +633,7 @@ def test_broadcastable():
x
=
tensor
.
matrix
()
x
=
tensor
.
matrix
()
size1
=
(
10
,
1
)
size1
=
(
10
,
1
)
size2
=
(
x
.
shape
[
0
],
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_1
=
pvals_1
/
sum
(
pvals_1
)
pvals_2
=
R
.
uniform
(
size
=
size2
)
pvals_2
=
R
.
uniform
(
size
=
size2
)
pvals_2
=
pvals_2
/
tensor
.
sum
(
pvals_2
)
pvals_2
=
pvals_2
/
tensor
.
sum
(
pvals_2
)
...
@@ -684,9 +684,9 @@ def test_binomial():
...
@@ -684,9 +684,9 @@ def test_binomial():
for
size
,
const_size
,
var_input
,
input
in
[
for
size
,
const_size
,
var_input
,
input
in
[
(
sample_size
,
sample_size
,
[],
[]),
(
sample_size
,
sample_size
,
[],
[]),
(
x
.
shape
,
sample_size
,
[
x
],
(
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
],
((
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)
# test empty size (scalar)
((),
(),
[],
[]),
((),
(),
[],
[]),
]:
]:
...
@@ -701,7 +701,7 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
...
@@ -701,7 +701,7 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
out
=
f
(
*
input
)
out
=
f
(
*
input
)
# Increase the number of steps if sizes implies only a few samples
# 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
steps_
=
steps
*
100
else
:
else
:
steps_
=
steps
steps_
=
steps
...
@@ -717,13 +717,13 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
...
@@ -717,13 +717,13 @@ def t_binomial(mean, size, const_size, var_input, input, steps, rtol):
f
=
theano
.
function
(
var_input
,
theano
.
Out
(
f
=
theano
.
function
(
var_input
,
theano
.
Out
(
theano
.
sandbox
.
cuda
.
basic_ops
.
gpu_from_host
(
u
),
theano
.
sandbox
.
cuda
.
basic_ops
.
gpu_from_host
(
u
),
borrow
=
True
),
mode
=
mode_with_gpu
)
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'
,
basictest
(
f
,
steps_
,
const_size
,
prefix
=
'mrg gpu'
,
inputs
=
input
,
allow_01
=
True
,
inputs
=
input
,
allow_01
=
True
,
target_avg
=
mean
,
mean_rtol
=
rtol
)
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
)
decimal
=
6
)
RR
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
234
)
RR
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
234
)
...
@@ -752,22 +752,22 @@ def test_normal0():
...
@@ -752,22 +752,22 @@ def test_normal0():
for
size
,
const_size
,
var_input
,
input
,
avg
,
rtol
,
std_tol
in
[
for
size
,
const_size
,
var_input
,
input
,
avg
,
rtol
,
std_tol
in
[
(
sample_size
,
sample_size
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
(
sample_size
,
sample_size
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
(
x
.
shape
,
sample_size
,
[
x
],
(
x
.
shape
,
sample_size
,
[
x
],
[
n
umpy
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
[
n
p
.
zeros
(
sample_size
,
dtype
=
config
.
floatX
)],
-
5.
,
default_rtol
,
default_rtol
),
-
5.
,
default_rtol
,
default_rtol
),
((
x
.
shape
[
0
],
sample_size
[
1
]),
sample_size
,
[
x
],
((
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
),
-
5.
,
default_rtol
,
default_rtol
),
# test odd value
# test odd value
(
sample_size_odd
,
sample_size_odd
,
[],
[],
-
5.
,
(
sample_size_odd
,
sample_size_odd
,
[],
[],
-
5.
,
default_rtol
,
default_rtol
),
default_rtol
,
default_rtol
),
# test odd value
# test odd value
(
x
.
shape
,
sample_size_odd
,
[
x
],
(
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
),
-
5.
,
default_rtol
,
default_rtol
),
(
sample_size
,
sample_size
,
[],
[],
(
sample_size
,
sample_size
,
[],
[],
n
umpy
.
arange
(
numpy
.
prod
(
sample_size
),
n
p
.
arange
(
np
.
prod
(
sample_size
),
dtype
=
'float32'
)
.
reshape
(
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)
# test empty size (scalar)
((),
(),
[],
[],
-
5.
,
default_rtol
,
0.02
),
((),
(),
[],
[],
-
5.
,
default_rtol
,
0.02
),
# test with few samples at the same time
# test with few samples at the same time
...
@@ -788,7 +788,7 @@ def test_normal0():
...
@@ -788,7 +788,7 @@ def test_normal0():
# print 'random?[:10]\n', out[0, 0:10]
# print 'random?[:10]\n', out[0, 0:10]
# Increase the number of steps if size implies only a few samples
# 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
steps_
=
steps
*
50
else
:
else
:
steps_
=
steps
steps_
=
steps
...
@@ -812,7 +812,7 @@ def test_normal0():
...
@@ -812,7 +812,7 @@ def test_normal0():
# theano.printing.debugprint(f)
# theano.printing.debugprint(f)
sys
.
stdout
.
flush
()
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 'random?[:10]\n', gpu_out[0, 0:10]
# print '----'
# print '----'
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
...
@@ -821,7 +821,7 @@ def test_normal0():
...
@@ -821,7 +821,7 @@ def test_normal0():
mean_rtol
=
rtol
,
std_tol
=
std_tol
)
mean_rtol
=
rtol
,
std_tol
=
std_tol
)
# Need to allow some rounding error as their is float
# Need to allow some rounding error as their is float
# computation that are done on the gpu vs cpu
# 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 ''
# print 'ON CPU w NUMPY:'
# print 'ON CPU w NUMPY:'
...
@@ -838,26 +838,26 @@ def basic_multinomialtest(f, steps, sample_size, target_pvals, n_samples,
...
@@ -838,26 +838,26 @@ def basic_multinomialtest(f, steps, sample_size, target_pvals, n_samples,
prefix
=
""
,
mean_rtol
=
0.04
):
prefix
=
""
,
mean_rtol
=
0.04
):
dt
=
0.0
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
):
for
i
in
xrange
(
steps
):
t0
=
time
.
time
()
t0
=
time
.
time
()
ival
=
f
()
ival
=
f
()
assert
ival
.
shape
==
sample_size
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
dt
+=
time
.
time
()
-
t0
avg_pvals
+=
ival
avg_pvals
+=
ival
avg_pvals
/=
(
steps
*
n_samples
)
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
)
print
(
prefix
,
'mean'
,
avg_pvals
)
# < mean_rtol, 'bad mean? %s %s' % (str(avg_pvals), str(target_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
,
'time'
,
dt
)
print
(
prefix
,
'elements'
,
steps
*
n
umpy
.
prod
(
target_pvals
.
shape
))
print
(
prefix
,
'elements'
,
steps
*
n
p
.
prod
(
target_pvals
.
shape
))
print
(
prefix
,
'samples/sec'
,
steps
*
n
umpy
.
prod
(
target_pvals
.
shape
)
/
dt
)
print
(
prefix
,
'samples/sec'
,
steps
*
n
p
.
prod
(
target_pvals
.
shape
)
/
dt
)
def
test_multinomial
():
def
test_multinomial
():
...
@@ -875,8 +875,8 @@ def test_multinomial():
...
@@ -875,8 +875,8 @@ def test_multinomial():
# print ''
# print ''
# print 'ON CPU:'
# print 'ON CPU:'
pvals
=
n
umpy
.
asarray
(
numpy
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
p
.
asarray
(
np
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
umpy
.
apply_along_axis
(
lambda
row
:
row
/
numpy
.
sum
(
row
),
1
,
pvals
)
pvals
=
n
p
.
apply_along_axis
(
lambda
row
:
row
/
np
.
sum
(
row
),
1
,
pvals
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
# Note: we specify `nstreams` to avoid a warning.
# Note: we specify `nstreams` to avoid a warning.
m
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
config
.
floatX
,
nstreams
=
30
*
256
)
m
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
config
.
floatX
,
nstreams
=
30
*
256
)
...
@@ -892,7 +892,7 @@ def test_multinomial():
...
@@ -892,7 +892,7 @@ def test_multinomial():
# print ''
# print ''
# print 'ON GPU:'
# print 'ON GPU:'
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
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.
# We give the number of streams to avoid a warning.
n
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
'float32'
,
nstreams
=
30
*
256
)
n
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
'float32'
,
nstreams
=
30
*
256
)
# well, it's really that this test w GPU doesn't make sense otw
# well, it's really that this test w GPU doesn't make sense otw
...
@@ -907,7 +907,7 @@ def test_multinomial():
...
@@ -907,7 +907,7 @@ def test_multinomial():
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
basic_multinomialtest
(
f
,
steps
,
sample_size
,
pvals
,
n_samples
=
1
,
basic_multinomialtest
(
f
,
steps
,
sample_size
,
pvals
,
n_samples
=
1
,
prefix
=
'gpu mrg '
)
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
():
def
test_multinomial_n_samples
():
...
@@ -922,8 +922,8 @@ def test_multinomial_n_samples():
...
@@ -922,8 +922,8 @@ def test_multinomial_n_samples():
sample_size
=
(
450
,
6
)
sample_size
=
(
450
,
6
)
mode_
=
theano
.
compile
.
mode
.
get_mode
(
mode_
)
mode_
=
theano
.
compile
.
mode
.
get_mode
(
mode_
)
pvals
=
n
umpy
.
asarray
(
numpy
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
p
.
asarray
(
np
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
n
umpy
.
apply_along_axis
(
lambda
row
:
row
/
numpy
.
sum
(
row
),
1
,
pvals
)
pvals
=
n
p
.
apply_along_axis
(
lambda
row
:
row
/
np
.
sum
(
row
),
1
,
pvals
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
for
n_samples
,
steps
in
zip
([
5
,
10
,
100
,
1000
],
[
20
,
10
,
1
,
1
]):
for
n_samples
,
steps
in
zip
([
5
,
10
,
100
,
1000
],
[
20
,
10
,
1
,
1
]):
...
@@ -936,7 +936,7 @@ def test_multinomial_n_samples():
...
@@ -936,7 +936,7 @@ def test_multinomial_n_samples():
if
mode
!=
'FAST_COMPILE'
and
cuda_available
:
if
mode
!=
'FAST_COMPILE'
and
cuda_available
:
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
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
,
n
=
R
.
multinomial
(
pvals
=
pvals
,
n
=
n_samples
,
dtype
=
'float32'
,
nstreams
=
30
*
256
)
dtype
=
'float32'
,
nstreams
=
30
*
256
)
assert
n
.
dtype
==
'float32'
assert
n
.
dtype
==
'float32'
...
@@ -999,14 +999,14 @@ def test_random_state_transfer():
...
@@ -999,14 +999,14 @@ def test_random_state_transfer():
for
(
su1
,
su2
)
in
zip
(
g1
.
rng
.
state_updates
,
g2
.
rng
.
state_updates
):
for
(
su1
,
su2
)
in
zip
(
g1
.
rng
.
state_updates
,
g2
.
rng
.
state_updates
):
su2
[
0
]
.
set_value
(
su1
[
0
]
.
get_value
())
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
():
def
test_gradient_scan
():
# Test for a crash when using MRG inside scan and taking the gradient
# 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
# See https://groups.google.com/d/msg/theano-dev/UbcYyU5m-M8/UO9UgXqnQP0J
theano_rng
=
MRG_RandomStreams
(
10
)
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
):
def
one_step
(
x
):
return
x
+
theano_rng
.
uniform
((
1
,),
dtype
=
'float32'
)
*
w
return
x
+
theano_rng
.
uniform
((
1
,),
dtype
=
'float32'
)
*
w
...
@@ -1015,7 +1015,7 @@ def test_gradient_scan():
...
@@ -1015,7 +1015,7 @@ def test_gradient_scan():
values
,
updates
=
theano
.
scan
(
one_step
,
outputs_info
=
x
,
n_steps
=
10
)
values
,
updates
=
theano
.
scan
(
one_step
,
outputs_info
=
x
,
n_steps
=
10
)
gw
=
theano
.
grad
(
tensor
.
sum
(
values
[
-
1
]),
w
)
gw
=
theano
.
grad
(
tensor
.
sum
(
values
[
-
1
]),
w
)
f
=
theano
.
function
([
x
],
gw
)
f
=
theano
.
function
([
x
],
gw
)
f
(
n
umpy
.
arange
(
1
,
dtype
=
'float32'
))
f
(
n
p
.
arange
(
1
,
dtype
=
'float32'
))
def
test_multMatVect
():
def
test_multMatVect
():
...
@@ -1029,14 +1029,14 @@ def test_multMatVect():
...
@@ -1029,14 +1029,14 @@ def test_multMatVect():
g0
=
rng_mrg
.
DotModulo
()(
A1
,
s1
,
m1
,
A2
,
s2
,
m2
)
g0
=
rng_mrg
.
DotModulo
()(
A1
,
s1
,
m1
,
A2
,
s2
,
m2
)
f0
=
theano
.
function
([
A1
,
s1
,
m1
,
A2
,
s2
,
m2
],
g0
)
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'
)
A1
=
n
p
.
random
.
randint
(
0
,
i32max
,
(
3
,
3
))
.
astype
(
'int64'
)
s1
=
n
umpy
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
s1
=
n
p
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
m1
=
n
umpy
.
asarray
(
numpy
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
m1
=
n
p
.
asarray
(
np
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
A2
=
n
umpy
.
random
.
randint
(
0
,
i32max
,
(
3
,
3
))
.
astype
(
'int64'
)
A2
=
n
p
.
random
.
randint
(
0
,
i32max
,
(
3
,
3
))
.
astype
(
'int64'
)
s2
=
n
umpy
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
s2
=
n
p
.
random
.
randint
(
0
,
i32max
,
3
)
.
astype
(
'int32'
)
m2
=
n
umpy
.
asarray
(
numpy
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
m2
=
n
p
.
asarray
(
np
.
random
.
randint
(
i32max
),
dtype
=
"int32"
)
f0
.
input_storage
[
0
]
.
storage
[
0
]
=
A1
f0
.
input_storage
[
0
]
.
storage
[
0
]
=
A1
f0
.
input_storage
[
1
]
.
storage
[
0
]
=
s1
f0
.
input_storage
[
1
]
.
storage
[
0
]
=
s1
...
@@ -1050,8 +1050,8 @@ def test_multMatVect():
...
@@ -1050,8 +1050,8 @@ def test_multMatVect():
f0
.
fn
()
f0
.
fn
()
r_b
=
f0
.
output_storage
[
0
]
.
value
r_b
=
f0
.
output_storage
[
0
]
.
value
assert
n
umpy
.
allclose
(
r_a1
,
r_b
[:
3
])
assert
n
p
.
allclose
(
r_a1
,
r_b
[:
3
])
assert
n
umpy
.
allclose
(
r_a2
,
r_b
[
3
:])
assert
n
p
.
allclose
(
r_a2
,
r_b
[
3
:])
def
test_seed_fn
():
def
test_seed_fn
():
...
@@ -1079,13 +1079,13 @@ def test_seed_fn():
...
@@ -1079,13 +1079,13 @@ def test_seed_fn():
fn1_val0
=
fn1
()
fn1_val0
=
fn1
()
fn1_val1
=
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_val0
=
fn2
()
fn2_val1
=
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_val0
=
fn3
([
4
])
fn3_val1
=
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
fn1_val0
.
size
==
4
assert
fn2_val0
.
size
==
9
assert
fn2_val0
.
size
==
9
...
@@ -1097,12 +1097,12 @@ def test_seed_fn():
...
@@ -1097,12 +1097,12 @@ def test_seed_fn():
fn2_val3
=
fn2
()
fn2_val3
=
fn2
()
fn3_val2
=
fn3
([
4
])
fn3_val2
=
fn3
([
4
])
fn3_val3
=
fn3
([
4
])
fn3_val3
=
fn3
([
4
])
assert
n
umpy
.
allclose
(
fn1_val0
,
fn1_val2
)
==
same
assert
n
p
.
allclose
(
fn1_val0
,
fn1_val2
)
==
same
assert
n
umpy
.
allclose
(
fn1_val1
,
fn1_val3
)
==
same
assert
n
p
.
allclose
(
fn1_val1
,
fn1_val3
)
==
same
assert
n
umpy
.
allclose
(
fn2_val0
,
fn2_val2
)
==
same
assert
n
p
.
allclose
(
fn2_val0
,
fn2_val2
)
==
same
assert
n
umpy
.
allclose
(
fn2_val1
,
fn2_val3
)
==
same
assert
n
p
.
allclose
(
fn2_val1
,
fn2_val3
)
==
same
assert
n
umpy
.
allclose
(
fn3_val0
,
fn3_val2
)
==
same
assert
n
p
.
allclose
(
fn3_val0
,
fn3_val2
)
==
same
assert
n
umpy
.
allclose
(
fn3_val1
,
fn3_val3
)
==
same
assert
n
p
.
allclose
(
fn3_val1
,
fn3_val3
)
==
same
def
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
):
def
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
):
...
@@ -1118,7 +1118,7 @@ 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
():
def
test_overflow_cpu
():
# run with THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32
# 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
fct
=
rng
.
uniform
# should raise error as the size overflows
# should raise error as the size overflows
sizes
=
[(
2
**
31
,
),
(
2
**
32
,
),
(
2
**
15
,
2
**
16
,),
(
2
,
2
**
15
,
2
**
15
)]
sizes
=
[(
2
**
31
,
),
(
2
**
32
,
),
(
2
**
15
,
2
**
16
,),
(
2
,
2
**
15
,
2
**
15
)]
...
@@ -1127,8 +1127,8 @@ def test_overflow_cpu():
...
@@ -1127,8 +1127,8 @@ def test_overflow_cpu():
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
rng_mrg_overflow
(
sizes
,
fct
,
config
.
mode
,
should_raise_error
=
False
)
rng_mrg_overflow
(
sizes
,
fct
,
config
.
mode
,
should_raise_error
=
False
)
# should support int32 sizes
# should support int32 sizes
sizes
=
[(
n
umpy
.
int32
(
2
**
10
),
),
sizes
=
[(
n
p
.
int32
(
2
**
10
),
),
(
n
umpy
.
int32
(
2
),
numpy
.
int32
(
2
**
10
),
numpy
.
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
)
rng_mrg_overflow
(
sizes
,
fct
,
config
.
mode
,
should_raise_error
=
False
)
...
@@ -1147,8 +1147,8 @@ def test_overflow_gpu_old_backend():
...
@@ -1147,8 +1147,8 @@ def test_overflow_gpu_old_backend():
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
# should support int32 sizes
# should support int32 sizes
sizes
=
[(
n
umpy
.
int32
(
2
**
10
),
),
sizes
=
[(
n
p
.
int32
(
2
**
10
),
),
(
n
umpy
.
int32
(
2
),
numpy
.
int32
(
2
**
10
),
numpy
.
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
)
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
...
@@ -1159,11 +1159,11 @@ def test_overflow_gpu_new_backend():
...
@@ -1159,11 +1159,11 @@ def test_overflow_gpu_new_backend():
from
theano.gpuarray.type
import
gpuarray_shared_constructor
from
theano.gpuarray.type
import
gpuarray_shared_constructor
seed
=
12345
seed
=
12345
n_substreams
=
7
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
()]
rstate
=
[
curr_rstate
.
copy
()]
for
j
in
range
(
1
,
n_substreams
):
for
j
in
range
(
1
,
n_substreams
):
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
.
append
(
rng_mrg
.
ff_2p72
(
rstate
[
-
1
]))
rstate
=
n
umpy
.
asarray
(
rstate
)
rstate
=
n
p
.
asarray
(
rstate
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
fct
=
functools
.
partial
(
rng_mrg
.
GPUA_mrg_uniform
.
new
,
rstate
,
fct
=
functools
.
partial
(
rng_mrg
.
GPUA_mrg_uniform
.
new
,
rstate
,
ndim
=
None
,
dtype
=
'float32'
)
ndim
=
None
,
dtype
=
'float32'
)
...
@@ -1174,8 +1174,8 @@ def test_overflow_gpu_new_backend():
...
@@ -1174,8 +1174,8 @@ def test_overflow_gpu_new_backend():
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
sizes
=
[(
2
**
5
,
),
(
2
**
5
,
2
**
5
),
(
2
**
5
,
2
**
5
,
2
**
5
)]
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
# should support int32 sizes
# should support int32 sizes
sizes
=
[(
n
umpy
.
int32
(
2
**
10
),
),
sizes
=
[(
n
p
.
int32
(
2
**
10
),
),
(
n
umpy
.
int32
(
2
),
numpy
.
int32
(
2
**
10
),
numpy
.
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
)
rng_mrg_overflow
(
sizes
,
fct
,
mode
,
should_raise_error
=
False
)
...
@@ -1185,12 +1185,12 @@ def test_validate_input_types_gpuarray_backend():
...
@@ -1185,12 +1185,12 @@ def test_validate_input_types_gpuarray_backend():
from
theano.configparser
import
change_flags
from
theano.configparser
import
change_flags
with
change_flags
(
compute_test_value
=
"raise"
):
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
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
dtype
=
"float32"
,
size
=
(
3
,))
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
dtype
=
"float32"
,
size
=
(
3
,))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
rng
=
MRG_RandomStreams
(
n
umpy
.
random
.
randint
(
2147462579
))
rng
=
MRG_RandomStreams
(
n
p
.
random
.
randint
(
2147462579
))
print
(
theano
.
__file__
)
print
(
theano
.
__file__
)
pvals
=
theano
.
tensor
.
fmatrix
()
pvals
=
theano
.
tensor
.
fmatrix
()
for
i
in
range
(
10
):
for
i
in
range
(
10
):
...
...
theano/scan_module/scan.py
浏览文件 @
bea31470
...
@@ -45,7 +45,7 @@ __contact__ = "Razvan Pascanu <r.pascanu@gmail>"
...
@@ -45,7 +45,7 @@ __contact__ = "Razvan Pascanu <r.pascanu@gmail>"
import
logging
import
logging
import
numpy
import
numpy
as
np
import
warnings
import
warnings
from
collections
import
OrderedDict
from
collections
import
OrderedDict
...
@@ -488,8 +488,8 @@ def scan(fn,
...
@@ -488,8 +488,8 @@ def scan(fn,
# a sequence, though is highly unlikely in practice
# a sequence, though is highly unlikely in practice
if
'taps'
in
seq
:
if
'taps'
in
seq
:
# go through the indicated slice
# go through the indicated slice
mintap
=
n
umpy
.
min
(
seq
[
'taps'
])
mintap
=
n
p
.
min
(
seq
[
'taps'
])
maxtap
=
n
umpy
.
max
(
seq
[
'taps'
])
maxtap
=
n
p
.
max
(
seq
[
'taps'
])
for
k
in
seq
[
'taps'
]:
for
k
in
seq
[
'taps'
]:
# create one slice of the input
# create one slice of the input
# Later on, if we decide not to use scan because we are
# Later on, if we decide not to use scan because we are
...
@@ -670,15 +670,15 @@ def scan(fn,
...
@@ -670,15 +670,15 @@ def scan(fn,
elif
init_out
.
get
(
'taps'
,
None
):
elif
init_out
.
get
(
'taps'
,
None
):
if
n
umpy
.
any
(
numpy
.
array
(
init_out
.
get
(
'taps'
,
[]))
>
0
):
if
n
p
.
any
(
np
.
array
(
init_out
.
get
(
'taps'
,
[]))
>
0
):
# Make sure we do not have requests for future values of a
# Make sure we do not have requests for future values of a
# sequence we can not provide such values
# sequence we can not provide such values
raise
ValueError
(
'Can not use future taps of outputs'
,
raise
ValueError
(
'Can not use future taps of outputs'
,
init_out
)
init_out
)
# go through the taps
# go through the taps
mintap
=
abs
(
n
umpy
.
min
(
init_out
[
'taps'
]))
mintap
=
abs
(
n
p
.
min
(
init_out
[
'taps'
]))
mit_sot_tap_array
.
append
(
init_out
[
'taps'
])
mit_sot_tap_array
.
append
(
init_out
[
'taps'
])
idx_offset
=
abs
(
n
umpy
.
min
(
init_out
[
'taps'
]))
idx_offset
=
abs
(
n
p
.
min
(
init_out
[
'taps'
]))
# Sequence
# Sequence
mit_sot_scan_inputs
.
append
(
mit_sot_scan_inputs
.
append
(
scan_utils
.
expand_empty
(
init_out
[
'initial'
][:
mintap
],
scan_utils
.
expand_empty
(
init_out
[
'initial'
][:
mintap
],
...
@@ -725,9 +725,9 @@ def scan(fn,
...
@@ -725,9 +725,9 @@ def scan(fn,
# a map); in that case we do not have to do anything ..
# a map); in that case we do not have to do anything ..
# Re-order args
# Re-order args
max_mit_sot
=
n
umpy
.
max
([
-
1
]
+
mit_sot_rightOrder
)
+
1
max_mit_sot
=
n
p
.
max
([
-
1
]
+
mit_sot_rightOrder
)
+
1
max_sit_sot
=
n
umpy
.
max
([
-
1
]
+
sit_sot_rightOrder
)
+
1
max_sit_sot
=
n
p
.
max
([
-
1
]
+
sit_sot_rightOrder
)
+
1
n_elems
=
n
umpy
.
max
([
max_mit_sot
,
max_sit_sot
])
n_elems
=
n
p
.
max
([
max_mit_sot
,
max_sit_sot
])
_ordered_args
=
[[]
for
x
in
xrange
(
n_elems
)]
_ordered_args
=
[[]
for
x
in
xrange
(
n_elems
)]
offset
=
0
offset
=
0
for
idx
in
xrange
(
n_mit_sot
):
for
idx
in
xrange
(
n_mit_sot
):
...
@@ -1101,7 +1101,7 @@ def scan(fn,
...
@@ -1101,7 +1101,7 @@ def scan(fn,
return
out_ls
return
out_ls
offset
=
n_mit_mot
offset
=
n_mit_mot
offsets
=
[
abs
(
n
umpy
.
min
(
x
))
for
x
in
mit_sot_tap_array
]
offsets
=
[
abs
(
n
p
.
min
(
x
))
for
x
in
mit_sot_tap_array
]
mit_sot_outs
=
remove_dimensions
(
mit_sot_outs
=
remove_dimensions
(
scan_outs
[
offset
:
offset
+
n_mit_sot
],
scan_outs
[
offset
:
offset
+
n_mit_sot
],
mit_sot_return_steps
,
mit_sot_return_steps
,
...
...
theano/scan_module/scan_op.py
浏览文件 @
bea31470
...
@@ -54,7 +54,7 @@ import logging
...
@@ -54,7 +54,7 @@ import logging
import
time
import
time
from
collections
import
OrderedDict
from
collections
import
OrderedDict
import
numpy
import
numpy
as
np
from
six
import
iteritems
,
integer_types
,
raise_from
from
six
import
iteritems
,
integer_types
,
raise_from
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -193,7 +193,7 @@ class Scan(PureOp):
...
@@ -193,7 +193,7 @@ class Scan(PureOp):
self
.
info
[
'name'
]
=
self
.
name
self
.
info
[
'name'
]
=
self
.
name
# Pre-computing some values to speed up perform
# Pre-computing some values to speed up perform
self
.
mintaps
=
[
n
umpy
.
min
(
x
)
for
x
in
self
.
tap_array
]
self
.
mintaps
=
[
n
p
.
min
(
x
)
for
x
in
self
.
tap_array
]
self
.
mintaps
+=
[
0
for
x
in
xrange
(
self
.
n_nit_sot
)]
self
.
mintaps
+=
[
0
for
x
in
xrange
(
self
.
n_nit_sot
)]
self
.
seqs_arg_offset
=
1
+
self
.
n_seqs
self
.
seqs_arg_offset
=
1
+
self
.
n_seqs
self
.
shared_arg_offset
=
(
self
.
seqs_arg_offset
+
self
.
shared_arg_offset
=
(
self
.
seqs_arg_offset
+
...
@@ -336,7 +336,7 @@ class Scan(PureOp):
...
@@ -336,7 +336,7 @@ class Scan(PureOp):
the inner function)
the inner function)
"""
"""
assert
n
umpy
.
all
(
isinstance
(
i
,
gof
.
Variable
)
for
i
in
inputs
)
assert
n
p
.
all
(
isinstance
(
i
,
gof
.
Variable
)
for
i
in
inputs
)
# Check that the number of inputs to the Scan node corresponds to
# Check that the number of inputs to the Scan node corresponds to
# the number of inputs of the inner function of scan
# the number of inputs of the inner function of scan
n_outer_ins
=
len
(
inputs
)
-
len
(
self
.
outer_nitsot
(
inputs
))
-
1
n_outer_ins
=
len
(
inputs
)
-
len
(
self
.
outer_nitsot
(
inputs
))
-
1
...
@@ -901,53 +901,53 @@ class Scan(PureOp):
...
@@ -901,53 +901,53 @@ class Scan(PureOp):
try
:
try
:
if
impl
==
'py'
:
if
impl
==
'py'
:
raise
theano
.
gof
.
cmodule
.
MissingGXX
raise
theano
.
gof
.
cmodule
.
MissingGXX
cython_mintaps
=
n
umpy
.
asarray
(
self
.
mintaps
,
dtype
=
'int32'
)
cython_mintaps
=
n
p
.
asarray
(
self
.
mintaps
,
dtype
=
'int32'
)
cython_tap_array_len
=
\
cython_tap_array_len
=
\
n
umpy
.
asarray
([
len
(
x
)
for
x
in
self
.
tap_array
],
n
p
.
asarray
([
len
(
x
)
for
x
in
self
.
tap_array
],
dtype
=
'int32'
)
dtype
=
'int32'
)
if
len
(
self
.
tap_array
)
==
0
:
if
len
(
self
.
tap_array
)
==
0
:
d1
=
0
d1
=
0
else
:
else
:
d1
=
n
umpy
.
max
(
cython_tap_array_len
)
d1
=
n
p
.
max
(
cython_tap_array_len
)
d0
=
len
(
self
.
tap_array
)
d0
=
len
(
self
.
tap_array
)
cython_tap_array
=
n
umpy
.
zeros
((
d0
,
d1
),
dtype
=
'int32'
)
cython_tap_array
=
n
p
.
zeros
((
d0
,
d1
),
dtype
=
'int32'
)
for
_d0
in
xrange
(
d0
):
for
_d0
in
xrange
(
d0
):
for
_d1
in
xrange
(
cython_tap_array_len
[
_d0
]):
for
_d1
in
xrange
(
cython_tap_array_len
[
_d0
]):
cython_tap_array
[
_d0
,
_d1
]
=
self
.
tap_array
[
_d0
][
_d1
]
cython_tap_array
[
_d0
,
_d1
]
=
self
.
tap_array
[
_d0
][
_d1
]
cython_mit_mot_out_nslices
=
\
cython_mit_mot_out_nslices
=
\
n
umpy
.
asarray
([
len
(
x
)
for
x
in
self
.
mit_mot_out_slices
],
n
p
.
asarray
([
len
(
x
)
for
x
in
self
.
mit_mot_out_slices
],
dtype
=
'int32'
)
dtype
=
'int32'
)
if
len
(
self
.
mit_mot_out_slices
)
==
0
:
if
len
(
self
.
mit_mot_out_slices
)
==
0
:
d1
=
0
d1
=
0
else
:
else
:
d1
=
n
umpy
.
max
(
cython_mit_mot_out_nslices
)
d1
=
n
p
.
max
(
cython_mit_mot_out_nslices
)
d0
=
len
(
self
.
mit_mot_out_slices
)
d0
=
len
(
self
.
mit_mot_out_slices
)
cython_mit_mot_out_slices
=
n
umpy
.
zeros
((
d0
,
d1
),
cython_mit_mot_out_slices
=
n
p
.
zeros
((
d0
,
d1
),
dtype
=
'int32'
)
dtype
=
'int32'
)
for
_d0
in
xrange
(
d0
):
for
_d0
in
xrange
(
d0
):
for
_d1
in
xrange
(
cython_mit_mot_out_nslices
[
_d0
]):
for
_d1
in
xrange
(
cython_mit_mot_out_nslices
[
_d0
]):
cython_mit_mot_out_slices
[
_d0
,
_d1
]
=
\
cython_mit_mot_out_slices
[
_d0
,
_d1
]
=
\
self
.
mit_mot_out_slices
[
_d0
][
_d1
]
self
.
mit_mot_out_slices
[
_d0
][
_d1
]
cython_vector_seqs
=
numpy
.
asarray
(
self
.
vector_seqs
,
cython_vector_seqs
=
np
.
asarray
(
self
.
vector_seqs
,
dtype
=
'int32'
)
cython_vector_outs
=
np
.
asarray
(
self
.
vector_outs
,
dtype
=
'int32'
)
cython_mitmots_preallocated
=
np
.
asarray
(
self
.
mitmots_preallocated
,
dtype
=
'int32'
)
cython_inps_is_tensor
=
np
.
asarray
(
self
.
inps_is_tensor
,
dtype
=
'int32'
)
dtype
=
'int32'
)
cython_
vector_outs
=
numpy
.
asarray
(
self
.
vector_outs
,
cython_
outs_is_tensor
=
np
.
asarray
(
self
.
outs_is_tensor
,
dtype
=
'int32'
)
dtype
=
'int32'
)
cython_mitmots_preallocated
=
numpy
.
asarray
(
self
.
mitmots_preallocated
,
dtype
=
'int32'
)
cython_inps_is_tensor
=
numpy
.
asarray
(
self
.
inps_is_tensor
,
dtype
=
'int32'
)
cython_outs_is_tensor
=
numpy
.
asarray
(
self
.
outs_is_tensor
,
dtype
=
'int32'
)
if
hasattr
(
self
,
'destroy_map'
):
if
hasattr
(
self
,
'destroy_map'
):
cython_destroy_map
=
[
x
in
self
.
destroy_map
cython_destroy_map
=
[
x
in
self
.
destroy_map
for
x
in
xrange
(
len
(
node
.
outputs
))]
for
x
in
xrange
(
len
(
node
.
outputs
))]
else
:
else
:
cython_destroy_map
=
[
0
for
x
in
xrange
(
len
(
node
.
outputs
))]
cython_destroy_map
=
[
0
for
x
in
xrange
(
len
(
node
.
outputs
))]
cython_destroy_map
=
n
umpy
.
asarray
(
cython_destroy_map
,
cython_destroy_map
=
n
p
.
asarray
(
cython_destroy_map
,
dtype
=
'int32'
)
dtype
=
'int32'
)
from
.
import
scan_perform_ext
from
.
import
scan_perform_ext
def
p
(
node
,
args
,
outs
):
def
p
(
node
,
args
,
outs
):
...
@@ -2200,9 +2200,9 @@ class Scan(PureOp):
...
@@ -2200,9 +2200,9 @@ class Scan(PureOp):
# Seqs
# Seqs
outer_inp_seqs
=
[
x
[::
-
1
]
for
x
in
inputs
[
1
:
1
+
self
.
n_seqs
]]
outer_inp_seqs
=
[
x
[::
-
1
]
for
x
in
inputs
[
1
:
1
+
self
.
n_seqs
]]
for
idx
in
xrange
(
self
.
n_mit_mot
+
self
.
n_mit_sot
):
for
idx
in
xrange
(
self
.
n_mit_mot
+
self
.
n_mit_sot
):
mintap
=
n
umpy
.
min
(
self
.
tap_array
[
idx
])
mintap
=
n
p
.
min
(
self
.
tap_array
[
idx
])
if
idx
<
self
.
n_mit_mot
:
if
idx
<
self
.
n_mit_mot
:
outmaxtap
=
n
umpy
.
max
(
self
.
mitmot_out_taps
()[
idx
])
outmaxtap
=
n
p
.
max
(
self
.
mitmot_out_taps
()[
idx
])
else
:
else
:
outmaxtap
=
0
outmaxtap
=
0
seq
=
outs
[
idx
]
seq
=
outs
[
idx
]
...
@@ -2226,7 +2226,7 @@ class Scan(PureOp):
...
@@ -2226,7 +2226,7 @@ class Scan(PureOp):
# that.
# that.
for
taps
,
x
in
zip
(
self
.
mitsot_taps
(),
for
taps
,
x
in
zip
(
self
.
mitsot_taps
(),
self
.
outer_mitsot_outs
(
outs
)):
self
.
outer_mitsot_outs
(
outs
)):
mintap
=
n
umpy
.
min
(
taps
)
mintap
=
n
p
.
min
(
taps
)
if
hasattr
(
x
[::
-
1
][:
mintap
],
'test_value'
):
if
hasattr
(
x
[::
-
1
][:
mintap
],
'test_value'
):
assert
(
x
[::
-
1
][:
mintap
]
.
tag
.
test_value
.
shape
[
0
]
==
assert
(
x
[::
-
1
][:
mintap
]
.
tag
.
test_value
.
shape
[
0
]
==
inputs
[
0
]
.
tag
.
test_value
)
inputs
[
0
]
.
tag
.
test_value
)
...
@@ -2238,7 +2238,7 @@ class Scan(PureOp):
...
@@ -2238,7 +2238,7 @@ class Scan(PureOp):
if
hasattr
(
x
[::
-
1
]
.
tag
,
'test_value'
):
if
hasattr
(
x
[::
-
1
]
.
tag
,
'test_value'
):
assert
(
x
[::
-
1
]
.
tag
.
test_value
.
shape
[
0
]
==
assert
(
x
[::
-
1
]
.
tag
.
test_value
.
shape
[
0
]
==
inputs
[
0
]
.
tag
.
test_value
)
inputs
[
0
]
.
tag
.
test_value
)
outer_inp_seqs
+=
[
x
[::
-
1
][:
n
umpy
.
min
(
taps
)]
outer_inp_seqs
+=
[
x
[::
-
1
][:
n
p
.
min
(
taps
)]
for
taps
,
x
in
zip
(
self
.
mitsot_taps
(),
for
taps
,
x
in
zip
(
self
.
mitsot_taps
(),
self
.
outer_mitsot_outs
(
outs
))]
self
.
outer_mitsot_outs
(
outs
))]
outer_inp_seqs
+=
[
x
[::
-
1
][:
-
1
]
for
x
in
self
.
outer_sitsot_outs
(
outs
)]
outer_inp_seqs
+=
[
x
[::
-
1
][:
-
1
]
for
x
in
self
.
outer_sitsot_outs
(
outs
)]
...
@@ -2726,8 +2726,8 @@ class Scan(PureOp):
...
@@ -2726,8 +2726,8 @@ class Scan(PureOp):
b
=
e
b
=
e
e
=
e
+
self
.
n_mit_mot
e
=
e
+
self
.
n_mit_mot
ib
=
ie
ib
=
ie
ie
=
ie
+
int
(
n
umpy
.
sum
([
len
(
x
)
for
x
in
ie
=
ie
+
int
(
n
p
.
sum
([
len
(
x
)
for
x
in
self
.
tap_array
[:
self
.
n_mit_mot
]]))
self
.
tap_array
[:
self
.
n_mit_mot
]]))
clean_eval_points
=
[]
clean_eval_points
=
[]
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
if
evp
is
not
None
:
if
evp
is
not
None
:
...
@@ -2742,9 +2742,9 @@ class Scan(PureOp):
...
@@ -2742,9 +2742,9 @@ class Scan(PureOp):
b
=
e
b
=
e
e
=
e
+
self
.
n_mit_sot
e
=
e
+
self
.
n_mit_sot
ib
=
ie
ib
=
ie
ie
=
ie
+
int
(
n
umpy
.
sum
([
len
(
x
)
for
x
in
ie
=
ie
+
int
(
n
p
.
sum
([
len
(
x
)
for
x
in
self
.
tap_array
[
self
.
n_mit_mot
:
self
.
tap_array
[
self
.
n_mit_mot
:
self
.
n_mit_mot
+
self
.
n_mit_sot
]]))
self
.
n_mit_mot
+
self
.
n_mit_sot
]]))
clean_eval_points
=
[]
clean_eval_points
=
[]
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
if
evp
is
not
None
:
if
evp
is
not
None
:
...
@@ -2795,8 +2795,8 @@ class Scan(PureOp):
...
@@ -2795,8 +2795,8 @@ class Scan(PureOp):
inner_other
=
self_inputs
[
ie
:]
+
inner_eval_points
[
ib
:]
inner_other
=
self_inputs
[
ie
:]
+
inner_eval_points
[
ib
:]
# Outputs
# Outputs
n_mit_mot_outs
=
int
(
n
umpy
.
sum
([
len
(
x
)
for
x
in
n_mit_mot_outs
=
int
(
n
p
.
sum
([
len
(
x
)
for
x
in
self
.
mit_mot_out_slices
]))
self
.
mit_mot_out_slices
]))
info
[
'n_mit_mot_outs'
]
=
n_mit_mot_outs
*
2
info
[
'n_mit_mot_outs'
]
=
n_mit_mot_outs
*
2
b
=
0
b
=
0
e
=
n_mit_mot_outs
e
=
n_mit_mot_outs
...
...
theano/scan_module/scan_opt.py
浏览文件 @
bea31470
...
@@ -54,7 +54,7 @@ import logging
...
@@ -54,7 +54,7 @@ import logging
import
copy
import
copy
from
sys
import
maxsize
from
sys
import
maxsize
from
collections
import
OrderedDict
from
collections
import
OrderedDict
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
tensor
,
scalar
from
theano
import
tensor
,
scalar
...
@@ -636,7 +636,7 @@ class PushOutSeqScan(gof.Optimizer):
...
@@ -636,7 +636,7 @@ class PushOutSeqScan(gof.Optimizer):
if
out
in
op
.
inner_mitsot_outs
(
ls
):
if
out
in
op
.
inner_mitsot_outs
(
ls
):
odx
=
op
.
inner_mitsot_outs
(
ls
)
.
index
(
out
)
odx
=
op
.
inner_mitsot_outs
(
ls
)
.
index
(
out
)
inp
=
op
.
outer_mitsot
(
node
)[
odx
]
inp
=
op
.
outer_mitsot
(
node
)[
odx
]
st
=
abs
(
n
umpy
.
min
(
op
.
mitsot_taps
()))
st
=
abs
(
n
p
.
min
(
op
.
mitsot_taps
()))
y
=
tensor
.
set_subtensor
(
inp
[
st
:],
_y
)
y
=
tensor
.
set_subtensor
(
inp
[
st
:],
_y
)
elif
out
in
op
.
inner_sitsot_outs
(
ls
):
elif
out
in
op
.
inner_sitsot_outs
(
ls
):
odx
=
op
.
inner_sitsot_outs
(
ls
)
.
index
(
out
)
odx
=
op
.
inner_sitsot_outs
(
ls
)
.
index
(
out
)
...
@@ -1373,7 +1373,7 @@ class ScanSaveMem(gof.Optimizer):
...
@@ -1373,7 +1373,7 @@ class ScanSaveMem(gof.Optimizer):
# TODO: Simplify the number of steps needed.
# TODO: Simplify the number of steps needed.
# FB: This need good testing, left to later.
# FB: This need good testing, left to later.
# call get_scalar_constant_value()? it can
# call get_scalar_constant_value()? it can
# return python/numpy scalar or n
umpy
.ndarray
# return python/numpy scalar or n
p
.ndarray
# currently.
# currently.
# pval = pre_greedy_local_optimizer(list_opt_slice,
# pval = pre_greedy_local_optimizer(list_opt_slice,
# pval)
# pval)
...
...
theano/scan_module/scan_perform_ext.py
浏览文件 @
bea31470
...
@@ -12,7 +12,7 @@ import os
...
@@ -12,7 +12,7 @@ import os
import
sys
import
sys
import
warnings
import
warnings
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
config
from
theano
import
config
...
@@ -103,7 +103,7 @@ except ImportError:
...
@@ -103,7 +103,7 @@ except ImportError:
# During scan cython development, it is helpful to keep the old interface, to don't manually edit the c file each time.
# During scan cython development, it is helpful to keep the old interface, to don't manually edit the c file each time.
preargs
.
remove
(
'-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION'
)
preargs
.
remove
(
'-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION'
)
else
:
else
:
numpy_ver
=
[
int
(
n
)
for
n
in
n
umpy
.
__version__
.
split
(
'.'
)[:
2
]]
numpy_ver
=
[
int
(
n
)
for
n
in
n
p
.
__version__
.
split
(
'.'
)[:
2
]]
# Add add some macro to lower the number of edit
# Add add some macro to lower the number of edit
# needed to the c file.
# needed to the c file.
if
bool
(
numpy_ver
>=
[
1
,
7
]):
if
bool
(
numpy_ver
>=
[
1
,
7
]):
...
...
theano/scan_module/scan_utils.py
浏览文件 @
bea31470
...
@@ -20,7 +20,7 @@ import logging
...
@@ -20,7 +20,7 @@ import logging
import
warnings
import
warnings
from
collections
import
OrderedDict
from
collections
import
OrderedDict
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano.compat
import
izip
from
theano.compat
import
izip
...
@@ -589,8 +589,8 @@ def get_updates_and_outputs(ls):
...
@@ -589,8 +589,8 @@ def get_updates_and_outputs(ls):
def
isNaN_or_Inf_or_None
(
x
):
def
isNaN_or_Inf_or_None
(
x
):
isNone
=
x
is
None
isNone
=
x
is
None
try
:
try
:
isNaN
=
n
umpy
.
isnan
(
x
)
isNaN
=
n
p
.
isnan
(
x
)
isInf
=
n
umpy
.
isinf
(
x
)
isInf
=
n
p
.
isinf
(
x
)
isStr
=
isinstance
(
x
,
string_types
)
isStr
=
isinstance
(
x
,
string_types
)
except
Exception
:
except
Exception
:
isNaN
=
False
isNaN
=
False
...
@@ -599,8 +599,8 @@ def isNaN_or_Inf_or_None(x):
...
@@ -599,8 +599,8 @@ def isNaN_or_Inf_or_None(x):
if
not
isNaN
and
not
isInf
:
if
not
isNaN
and
not
isInf
:
try
:
try
:
val
=
get_scalar_constant_value
(
x
)
val
=
get_scalar_constant_value
(
x
)
isInf
=
n
umpy
.
isinf
(
val
)
isInf
=
n
p
.
isinf
(
val
)
isNaN
=
n
umpy
.
isnan
(
val
)
isNaN
=
n
p
.
isnan
(
val
)
except
Exception
:
except
Exception
:
isNaN
=
False
isNaN
=
False
isInf
=
False
isInf
=
False
...
@@ -959,7 +959,7 @@ def scan_can_remove_outs(op, out_idxs):
...
@@ -959,7 +959,7 @@ def scan_can_remove_outs(op, out_idxs):
added
=
False
added
=
False
for
pos
,
idx
in
enumerate
(
out_idxs
):
for
pos
,
idx
in
enumerate
(
out_idxs
):
if
(
out_idxs_mask
[
pos
]
and
if
(
out_idxs_mask
[
pos
]
and
n
umpy
.
any
([
x
in
required_inputs
for
x
in
out_ins
[
idx
]])):
n
p
.
any
([
x
in
required_inputs
for
x
in
out_ins
[
idx
]])):
# This output is required ..
# This output is required ..
out_idxs_mask
[
pos
]
=
0
out_idxs_mask
[
pos
]
=
0
required_inputs
+=
gof
.
graph
.
inputs
([
op
.
outputs
[
idx
]])
required_inputs
+=
gof
.
graph
.
inputs
([
op
.
outputs
[
idx
]])
...
...
theano/scan_module/tests/test_scan.py
浏览文件 @
bea31470
...
@@ -11,7 +11,7 @@ from collections import OrderedDict
...
@@ -11,7 +11,7 @@ from collections import OrderedDict
import
six.moves.cPickle
as
pickle
import
six.moves.cPickle
as
pickle
from
six.moves
import
xrange
from
six.moves
import
xrange
import
numpy
import
numpy
as
np
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
nose.tools
import
assert_raises
from
nose.tools
import
assert_raises
from
nose.tools
import
raises
from
nose.tools
import
raises
...
@@ -89,7 +89,7 @@ class multiple_outputs_numeric_grad:
...
@@ -89,7 +89,7 @@ class multiple_outputs_numeric_grad:
for
i
,
p
in
enumerate
(
pt
):
for
i
,
p
in
enumerate
(
pt
):
if
ndarray_mask
[
i
]:
if
ndarray_mask
[
i
]:
pt
[
i
]
=
n
umpy
.
array
(
p
)
pt
[
i
]
=
n
p
.
array
(
p
)
_eps
=
type_eps
[
str
(
pt
[
i
]
.
dtype
)]
_eps
=
type_eps
[
str
(
pt
[
i
]
.
dtype
)]
if
_eps
>
dtype_eps
:
if
_eps
>
dtype_eps
:
dtype_eps
=
_eps
dtype_eps
=
_eps
...
@@ -116,12 +116,12 @@ class multiple_outputs_numeric_grad:
...
@@ -116,12 +116,12 @@ class multiple_outputs_numeric_grad:
t
[
pos
]
+=
_eps
t
[
pos
]
+=
_eps
t
=
t
.
reshape
(
pt
[
i
]
.
shape
)
t
=
t
.
reshape
(
pt
[
i
]
.
shape
)
f_eps
=
f
(
*
(
pt
[:
i
]
+
[
t
]
+
pt
[
i
+
1
:]))
f_eps
=
f
(
*
(
pt
[:
i
]
+
[
t
]
+
pt
[
i
+
1
:]))
_g
.
append
(
n
umpy
.
asarray
((
f_eps
-
f_x
)
/
_eps
))
_g
.
append
(
n
p
.
asarray
((
f_eps
-
f_x
)
/
_eps
))
gx
.
append
(
n
umpy
.
asarray
(
_g
)
.
reshape
(
pt
[
i
]
.
shape
))
gx
.
append
(
n
p
.
asarray
(
_g
)
.
reshape
(
pt
[
i
]
.
shape
))
else
:
else
:
t
=
n
umpy
.
array
(
pt
[
i
]
+
_eps
)
t
=
n
p
.
array
(
pt
[
i
]
+
_eps
)
f_eps
=
f
(
*
(
pt
[:
i
]
+
[
t
]
+
pt
[
i
+
1
:]))
f_eps
=
f
(
*
(
pt
[:
i
]
+
[
t
]
+
pt
[
i
+
1
:]))
gx
.
append
(
n
umpy
.
asarray
((
f_eps
-
f_x
)
/
_eps
))
gx
.
append
(
n
p
.
asarray
((
f_eps
-
f_x
)
/
_eps
))
self
.
gx
=
gx
self
.
gx
=
gx
@staticmethod
@staticmethod
...
@@ -137,8 +137,8 @@ class multiple_outputs_numeric_grad:
...
@@ -137,8 +137,8 @@ class multiple_outputs_numeric_grad:
for
i
in
xrange
(
len
(
_g_pt
)):
for
i
in
xrange
(
len
(
_g_pt
)):
if
self
.
ndarray_mask
[
i
]:
if
self
.
ndarray_mask
[
i
]:
g_pt
.
append
(
_g_pt
[
i
])
g_pt
.
append
(
_g_pt
[
i
])
elif
isinstance
(
_g_pt
[
i
],
n
umpy
.
ndarray
):
elif
isinstance
(
_g_pt
[
i
],
n
p
.
ndarray
):
assert
n
umpy
.
all
(
_g_pt
[
i
]
==
0
)
assert
n
p
.
all
(
_g_pt
[
i
]
==
0
)
if
len
(
g_pt
)
!=
len
(
self
.
gx
):
if
len
(
g_pt
)
!=
len
(
self
.
gx
):
raise
ValueError
(
'argument has wrong number of elements'
,
raise
ValueError
(
'argument has wrong number of elements'
,
len
(
g_pt
))
len
(
g_pt
))
...
@@ -149,12 +149,12 @@ class multiple_outputs_numeric_grad:
...
@@ -149,12 +149,12 @@ class multiple_outputs_numeric_grad:
raise
ValueError
(
'argument element
%
i has wrong shape
%
s'
%
raise
ValueError
(
'argument element
%
i has wrong shape
%
s'
%
(
i
,
str
((
a
.
shape
,
b
.
shape
))))
(
i
,
str
((
a
.
shape
,
b
.
shape
))))
vv
=
multiple_outputs_numeric_grad
.
abs_rel_err
(
a
,
b
)
vv
=
multiple_outputs_numeric_grad
.
abs_rel_err
(
a
,
b
)
errs
.
append
(
n
umpy
.
max
(
errs
.
append
(
n
p
.
max
(
multiple_outputs_numeric_grad
.
abs_rel_err
(
a
,
b
)))
multiple_outputs_numeric_grad
.
abs_rel_err
(
a
,
b
)))
if
n
umpy
.
all
(
numpy
.
isfinite
(
errs
)):
if
n
p
.
all
(
np
.
isfinite
(
errs
)):
return
n
umpy
.
max
(
errs
),
numpy
.
argmax
(
errs
)
return
n
p
.
max
(
errs
),
np
.
argmax
(
errs
)
else
:
else
:
return
n
umpy
.
inf
,
0
return
n
p
.
inf
,
0
# TODO: Test this function, and if it works,
# TODO: Test this function, and if it works,
...
@@ -262,11 +262,11 @@ class T_Scan(unittest.TestCase):
...
@@ -262,11 +262,11 @@ class T_Scan(unittest.TestCase):
if
tmpdir
is
not
None
:
if
tmpdir
is
not
None
:
shutil
.
rmtree
(
tmpdir
)
shutil
.
rmtree
(
tmpdir
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
state
=
rng
.
uniform
()
state
=
rng
.
uniform
()
steps
=
5
steps
=
5
numpy_values
=
n
umpy
.
array
([
state
*
(
2
**
(
k
+
1
))
for
k
numpy_values
=
n
p
.
array
([
state
*
(
2
**
(
k
+
1
))
for
k
in
xrange
(
steps
)])
in
xrange
(
steps
)])
theano_values
=
my_f
(
state
,
steps
)
theano_values
=
my_f
(
state
,
steps
)
utt
.
assert_allclose
(
numpy_values
,
theano_values
)
utt
.
assert_allclose
(
numpy_values
,
theano_values
)
...
@@ -300,7 +300,7 @@ class T_Scan(unittest.TestCase):
...
@@ -300,7 +300,7 @@ class T_Scan(unittest.TestCase):
assert
all
(
i
.
value
is
None
for
i
in
scan_node
.
op
.
fn
.
input_storage
)
assert
all
(
i
.
value
is
None
for
i
in
scan_node
.
op
.
fn
.
input_storage
)
assert
all
(
o
.
value
is
None
for
o
in
scan_node
.
op
.
fn
.
output_storage
)
assert
all
(
o
.
value
is
None
for
o
in
scan_node
.
op
.
fn
.
output_storage
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
state
=
rng
.
uniform
()
state
=
rng
.
uniform
()
steps
=
5
steps
=
5
...
@@ -332,11 +332,11 @@ class T_Scan(unittest.TestCase):
...
@@ -332,11 +332,11 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
state
=
rng
.
uniform
()
state
=
rng
.
uniform
()
steps
=
5
steps
=
5
numpy_values
=
n
umpy
.
array
([
state
*
(
2
**
(
k
+
1
))
for
k
numpy_values
=
n
p
.
array
([
state
*
(
2
**
(
k
+
1
))
for
k
in
xrange
(
steps
)])
in
xrange
(
steps
)])
theano_values
=
my_f
(
state
,
steps
)
theano_values
=
my_f
(
state
,
steps
)
utt
.
assert_allclose
(
numpy_values
,
theano_values
[
0
])
utt
.
assert_allclose
(
numpy_values
,
theano_values
[
0
])
...
@@ -370,10 +370,10 @@ class T_Scan(unittest.TestCase):
...
@@ -370,10 +370,10 @@ class T_Scan(unittest.TestCase):
# This assertation fails if savemem optimization failed on scan
# This assertation fails if savemem optimization failed on scan
if
theano
.
config
.
mode
!=
"FAST_COMPILE"
:
if
theano
.
config
.
mode
!=
"FAST_COMPILE"
:
assert
nodes
[
0
]
.
op
.
_scan_savemem_visited
assert
nodes
[
0
]
.
op
.
_scan_savemem_visited
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
my_f
(
rng
.
uniform
(
size
=
(
3
,)),
my_f
(
rng
.
uniform
(
size
=
(
3
,)),
4
,
4
,
n
umpy
.
int64
([
2
,
2
,
3
]))
n
p
.
int64
([
2
,
2
,
3
]))
@attr
(
'slow'
)
@attr
(
'slow'
)
def
test_only_nonseq_inputs
(
self
):
def
test_only_nonseq_inputs
(
self
):
...
@@ -388,9 +388,9 @@ class T_Scan(unittest.TestCase):
...
@@ -388,9 +388,9 @@ class T_Scan(unittest.TestCase):
fun
=
theano
.
function
([
inp
],
[
broadcasted_inp
,
gr
])
fun
=
theano
.
function
([
inp
],
[
broadcasted_inp
,
gr
])
# Execute the Theano function and compare outputs to the expected outputs
# Execute the Theano function and compare outputs to the expected outputs
inputs
=
n
umpy
.
array
([[
1
,
2
],
[
3
,
4
]],
dtype
=
theano
.
config
.
floatX
)
inputs
=
n
p
.
array
([[
1
,
2
],
[
3
,
4
]],
dtype
=
theano
.
config
.
floatX
)
expected_out1
=
n
umpy
.
repeat
(
inputs
[
None
],
n_steps
,
axis
=
0
)
expected_out1
=
n
p
.
repeat
(
inputs
[
None
],
n_steps
,
axis
=
0
)
expected_out2
=
n
umpy
.
ones
(
inputs
.
shape
,
dtype
=
"int8"
)
*
n_steps
expected_out2
=
n
p
.
ones
(
inputs
.
shape
,
dtype
=
"int8"
)
*
n_steps
out1
,
out2
=
fun
(
inputs
)
out1
,
out2
=
fun
(
inputs
)
utt
.
assert_allclose
(
out1
,
expected_out1
)
utt
.
assert_allclose
(
out1
,
expected_out1
)
...
@@ -420,14 +420,14 @@ class T_Scan(unittest.TestCase):
...
@@ -420,14 +420,14 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_x0
=
rng
.
uniform
()
v_x0
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
# compute the output in numpy
# compute the output in numpy
v_out
=
n
umpy
.
zeros
((
4
,))
v_out
=
n
p
.
zeros
((
4
,))
v_out
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
v_out
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
for
step
in
xrange
(
1
,
4
):
for
step
in
xrange
(
1
,
4
):
v_out
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
v_out
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
...
@@ -437,7 +437,7 @@ class T_Scan(unittest.TestCase):
...
@@ -437,7 +437,7 @@ class T_Scan(unittest.TestCase):
# simple rnn, one input, one state, weights for each; input/state
# simple rnn, one input, one state, weights for each; input/state
# are vectors, weights are scalars; using shared variables
# are vectors, weights are scalars; using shared variables
def
test_one_sequence_one_output_weights_shared
(
self
):
def
test_one_sequence_one_output_weights_shared
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
u
=
theano
.
tensor
.
vector
(
'u'
)
u
=
theano
.
tensor
.
vector
(
'u'
)
x0
=
theano
.
tensor
.
scalar
(
'x0'
)
x0
=
theano
.
tensor
.
scalar
(
'x0'
)
W_in
=
theano
.
shared
(
asarrayX
(
rng
.
uniform
()),
name
=
'w_in'
)
W_in
=
theano
.
shared
(
asarrayX
(
rng
.
uniform
()),
name
=
'w_in'
)
...
@@ -462,19 +462,19 @@ class T_Scan(unittest.TestCase):
...
@@ -462,19 +462,19 @@ class T_Scan(unittest.TestCase):
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_x0
=
rng
.
uniform
()
v_x0
=
rng
.
uniform
()
# compute the output i numpy
# compute the output i numpy
v_out
=
n
umpy
.
zeros
((
4
,))
v_out
=
n
p
.
zeros
((
4
,))
v_out
[
0
]
=
v_u
[
0
]
*
W_in
.
get_value
()
+
v_x0
*
W
.
get_value
()
v_out
[
0
]
=
v_u
[
0
]
*
W_in
.
get_value
()
+
v_x0
*
W
.
get_value
()
for
step
in
xrange
(
1
,
4
):
for
step
in
xrange
(
1
,
4
):
v_out
[
step
]
=
(
v_u
[
step
]
*
W_in
.
get_value
()
+
v_out
[
step
]
=
(
v_u
[
step
]
*
W_in
.
get_value
()
+
v_out
[
step
-
1
]
*
W
.
get_value
())
v_out
[
step
-
1
]
*
W
.
get_value
())
theano_values
=
f3
(
v_u
,
v_x0
)
theano_values
=
f3
(
v_u
,
v_x0
)
assert
n
umpy
.
allclose
(
theano_values
,
v_out
)
assert
n
p
.
allclose
(
theano_values
,
v_out
)
# some rnn with multiple outputs and multiple inputs; other
# some rnn with multiple outputs and multiple inputs; other
# dimension instead of scalars/vectors
# dimension instead of scalars/vectors
def
test_multiple_inputs_multiple_outputs
(
self
):
def
test_multiple_inputs_multiple_outputs
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
...
@@ -511,15 +511,15 @@ class T_Scan(unittest.TestCase):
...
@@ -511,15 +511,15 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# compute the values in numpy
# compute the values in numpy
v_x
=
n
umpy
.
zeros
((
3
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_x
=
n
p
.
zeros
((
3
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
umpy
.
zeros
((
3
,),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
p
.
zeros
((
3
,),
dtype
=
theano
.
config
.
floatX
)
v_x
[
0
]
=
(
n
umpy
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
v_x
[
0
]
=
(
n
p
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
n
umpy
.
dot
(
v_x0
,
vW
))
n
p
.
dot
(
v_x0
,
vW
))
v_y
[
0
]
=
n
umpy
.
dot
(
v_x0
,
vWout
)
v_y
[
0
]
=
n
p
.
dot
(
v_x0
,
vWout
)
for
i
in
xrange
(
1
,
3
):
for
i
in
xrange
(
1
,
3
):
v_x
[
i
]
=
(
n
umpy
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
v_x
[
i
]
=
(
n
p
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
n
umpy
.
dot
(
v_x
[
i
-
1
],
vW
))
n
p
.
dot
(
v_x
[
i
-
1
],
vW
))
v_y
[
i
]
=
n
umpy
.
dot
(
v_x
[
i
-
1
],
vWout
)
v_y
[
i
]
=
n
p
.
dot
(
v_x
[
i
-
1
],
vWout
)
(
theano_x
,
theano_y
)
=
f4
(
v_u1
,
v_u2
,
v_x0
,
v_y0
,
vW_in1
)
(
theano_x
,
theano_y
)
=
f4
(
v_u1
,
v_u2
,
v_x0
,
v_y0
,
vW_in1
)
utt
.
assert_allclose
(
theano_x
,
v_x
)
utt
.
assert_allclose
(
theano_x
,
v_x
)
...
@@ -527,7 +527,7 @@ class T_Scan(unittest.TestCase):
...
@@ -527,7 +527,7 @@ class T_Scan(unittest.TestCase):
def
test_multiple_outs_taps
(
self
):
def
test_multiple_outs_taps
(
self
):
l
=
5
l
=
5
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
2
,
high
=.
2
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
2
,
high
=.
2
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
...
@@ -579,40 +579,40 @@ class T_Scan(unittest.TestCase):
...
@@ -579,40 +579,40 @@ class T_Scan(unittest.TestCase):
v_y0
,
v_y0
,
vW_in1
)
vW_in1
)
ny0
=
n
umpy
.
zeros
((
5
,
2
))
ny0
=
n
p
.
zeros
((
5
,
2
))
ny1
=
n
umpy
.
zeros
((
5
,))
ny1
=
n
p
.
zeros
((
5
,))
ny2
=
n
umpy
.
zeros
((
5
,
2
))
ny2
=
n
p
.
zeros
((
5
,
2
))
ny0
[
0
]
=
n
umpy
.
dot
(
v_u1
[
0
],
vW_in1
)
+
\
ny0
[
0
]
=
n
p
.
dot
(
v_u1
[
0
],
vW_in1
)
+
\
(
v_u2
[
1
]
+
v_u2
[
0
]
*
v_u2
[
2
])
*
vW_in2
+
n
umpy
.
dot
(
v_x0
,
vW
)
(
v_u2
[
1
]
+
v_u2
[
0
]
*
v_u2
[
2
])
*
vW_in2
+
n
p
.
dot
(
v_x0
,
vW
)
ny1
[
0
]
=
(
v_y0
[
2
]
+
v_y0
[
0
])
*
n
umpy
.
dot
(
v_x0
,
vWout
)
ny1
[
0
]
=
(
v_y0
[
2
]
+
v_y0
[
0
])
*
n
p
.
dot
(
v_x0
,
vWout
)
ny2
[
0
]
=
n
umpy
.
dot
(
v_u1
[
0
],
vW_in1
)
ny2
[
0
]
=
n
p
.
dot
(
v_u1
[
0
],
vW_in1
)
ny0
[
1
]
=
n
umpy
.
dot
(
v_u1
[
1
],
vW_in1
)
+
\
ny0
[
1
]
=
n
p
.
dot
(
v_u1
[
1
],
vW_in1
)
+
\
(
v_u2
[
2
]
+
v_u2
[
1
]
*
v_u2
[
3
])
*
vW_in2
+
n
umpy
.
dot
(
ny0
[
0
],
vW
)
(
v_u2
[
2
]
+
v_u2
[
1
]
*
v_u2
[
3
])
*
vW_in2
+
n
p
.
dot
(
ny0
[
0
],
vW
)
ny1
[
1
]
=
(
ny1
[
0
]
+
v_y0
[
1
])
*
n
umpy
.
dot
(
ny0
[
0
],
vWout
)
ny1
[
1
]
=
(
ny1
[
0
]
+
v_y0
[
1
])
*
n
p
.
dot
(
ny0
[
0
],
vWout
)
ny2
[
1
]
=
n
umpy
.
dot
(
v_u1
[
1
],
vW_in1
)
ny2
[
1
]
=
n
p
.
dot
(
v_u1
[
1
],
vW_in1
)
ny0
[
2
]
=
n
umpy
.
dot
(
v_u1
[
2
],
vW_in1
)
+
\
ny0
[
2
]
=
n
p
.
dot
(
v_u1
[
2
],
vW_in1
)
+
\
(
v_u2
[
3
]
+
v_u2
[
2
]
*
v_u2
[
4
])
*
vW_in2
+
\
(
v_u2
[
3
]
+
v_u2
[
2
]
*
v_u2
[
4
])
*
vW_in2
+
\
n
umpy
.
dot
(
ny0
[
1
],
vW
)
n
p
.
dot
(
ny0
[
1
],
vW
)
ny1
[
2
]
=
(
ny1
[
1
]
+
v_y0
[
2
])
*
n
umpy
.
dot
(
ny0
[
1
],
vWout
)
ny1
[
2
]
=
(
ny1
[
1
]
+
v_y0
[
2
])
*
n
p
.
dot
(
ny0
[
1
],
vWout
)
ny2
[
2
]
=
n
umpy
.
dot
(
v_u1
[
2
],
vW_in1
)
ny2
[
2
]
=
n
p
.
dot
(
v_u1
[
2
],
vW_in1
)
ny0
[
3
]
=
n
umpy
.
dot
(
v_u1
[
3
],
vW_in1
)
+
\
ny0
[
3
]
=
n
p
.
dot
(
v_u1
[
3
],
vW_in1
)
+
\
(
v_u2
[
4
]
+
v_u2
[
3
]
*
v_u2
[
5
])
*
vW_in2
+
\
(
v_u2
[
4
]
+
v_u2
[
3
]
*
v_u2
[
5
])
*
vW_in2
+
\
n
umpy
.
dot
(
ny0
[
2
],
vW
)
n
p
.
dot
(
ny0
[
2
],
vW
)
ny1
[
3
]
=
(
ny1
[
2
]
+
ny1
[
0
])
*
n
umpy
.
dot
(
ny0
[
2
],
vWout
)
ny1
[
3
]
=
(
ny1
[
2
]
+
ny1
[
0
])
*
n
p
.
dot
(
ny0
[
2
],
vWout
)
ny2
[
3
]
=
n
umpy
.
dot
(
v_u1
[
3
],
vW_in1
)
ny2
[
3
]
=
n
p
.
dot
(
v_u1
[
3
],
vW_in1
)
ny0
[
4
]
=
n
umpy
.
dot
(
v_u1
[
4
],
vW_in1
)
+
\
ny0
[
4
]
=
n
p
.
dot
(
v_u1
[
4
],
vW_in1
)
+
\
(
v_u2
[
5
]
+
v_u2
[
4
]
*
v_u2
[
6
])
*
vW_in2
+
\
(
v_u2
[
5
]
+
v_u2
[
4
]
*
v_u2
[
6
])
*
vW_in2
+
\
n
umpy
.
dot
(
ny0
[
3
],
vW
)
n
p
.
dot
(
ny0
[
3
],
vW
)
ny1
[
4
]
=
(
ny1
[
3
]
+
ny1
[
1
])
*
n
umpy
.
dot
(
ny0
[
3
],
vWout
)
ny1
[
4
]
=
(
ny1
[
3
]
+
ny1
[
1
])
*
n
p
.
dot
(
ny0
[
3
],
vWout
)
ny2
[
4
]
=
n
umpy
.
dot
(
v_u1
[
4
],
vW_in1
)
ny2
[
4
]
=
n
p
.
dot
(
v_u1
[
4
],
vW_in1
)
def
test_using_taps_sequence
(
self
):
def
test_using_taps_sequence
(
self
):
# this test refers to a bug reported by Nicolas
# this test refers to a bug reported by Nicolas
...
@@ -621,9 +621,9 @@ class T_Scan(unittest.TestCase):
...
@@ -621,9 +621,9 @@ class T_Scan(unittest.TestCase):
y
,
updates
=
theano
.
scan
(
lambda
x
:
[
x
],
y
,
updates
=
theano
.
scan
(
lambda
x
:
[
x
],
sequences
=
dict
(
input
=
x
,
taps
=
[
-
1
]),
sequences
=
dict
(
input
=
x
,
taps
=
[
-
1
]),
outputs_info
=
[
None
])
outputs_info
=
[
None
])
inp
=
n
umpy
.
arange
(
5
)
.
astype
(
'float64'
)
inp
=
n
p
.
arange
(
5
)
.
astype
(
'float64'
)
rval
=
theano
.
function
([
x
],
y
,
updates
=
updates
)(
inp
)
rval
=
theano
.
function
([
x
],
y
,
updates
=
updates
)(
inp
)
assert
n
umpy
.
all
(
rval
==
inp
[:
-
1
])
assert
n
p
.
all
(
rval
==
inp
[:
-
1
])
def
test_using_negative_taps_sequence
(
self
):
def
test_using_negative_taps_sequence
(
self
):
# This test refers to a bug reported on github on May 22 2015 by
# This test refers to a bug reported on github on May 22 2015 by
...
@@ -636,7 +636,7 @@ class T_Scan(unittest.TestCase):
...
@@ -636,7 +636,7 @@ class T_Scan(unittest.TestCase):
f
=
theano
.
function
([
x
],
res
,
updates
=
upd
)
f
=
theano
.
function
([
x
],
res
,
updates
=
upd
)
output
=
f
([
1
,
2
,
3
,
4
,
5
])
output
=
f
([
1
,
2
,
3
,
4
,
5
])
expected_output
=
n
umpy
.
array
([
1
,
2
,
3
],
dtype
=
"float32"
)
expected_output
=
n
p
.
array
([
1
,
2
,
3
],
dtype
=
"float32"
)
utt
.
assert_allclose
(
output
,
expected_output
)
utt
.
assert_allclose
(
output
,
expected_output
)
def
test_connection_pattern
(
self
):
def
test_connection_pattern
(
self
):
...
@@ -649,8 +649,8 @@ class T_Scan(unittest.TestCase):
...
@@ -649,8 +649,8 @@ class T_Scan(unittest.TestCase):
def
fn
(
a_m2
,
a_m1
,
b_m2
,
b_m1
):
def
fn
(
a_m2
,
a_m1
,
b_m2
,
b_m1
):
return
a_m1
,
b_m1
return
a_m1
,
b_m1
a0
=
theano
.
shared
(
n
umpy
.
arange
(
2
))
a0
=
theano
.
shared
(
n
p
.
arange
(
2
))
b0
=
theano
.
shared
(
n
umpy
.
arange
(
2
))
b0
=
theano
.
shared
(
n
p
.
arange
(
2
))
(
a
,
b
),
_
=
theano
.
scan
(
fn
,
(
a
,
b
),
_
=
theano
.
scan
(
fn
,
outputs_info
=
[{
'initial'
:
a0
,
'taps'
:
[
-
2
,
-
1
]},
outputs_info
=
[{
'initial'
:
a0
,
'taps'
:
[
-
2
,
-
1
]},
...
@@ -741,7 +741,7 @@ class T_Scan(unittest.TestCase):
...
@@ -741,7 +741,7 @@ class T_Scan(unittest.TestCase):
# Call verify_grad to ensure the correctness of the second gradients
# Call verify_grad to ensure the correctness of the second gradients
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
inputs_test_values
=
[
n
umpy
.
random
.
random
((
3
))
.
astype
(
floatX
)]
inputs_test_values
=
[
n
p
.
random
.
random
((
3
))
.
astype
(
floatX
)]
theano
.
tests
.
unittest_tools
.
verify_grad
(
get_sum_of_grad
,
theano
.
tests
.
unittest_tools
.
verify_grad
(
get_sum_of_grad
,
inputs_test_values
)
inputs_test_values
)
...
@@ -768,8 +768,8 @@ class T_Scan(unittest.TestCase):
...
@@ -768,8 +768,8 @@ class T_Scan(unittest.TestCase):
# Call verify_grad to ensure the correctness of the second gradients
# Call verify_grad to ensure the correctness of the second gradients
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
inputs_test_values
=
[
n
umpy
.
random
.
random
((
2
,
3
))
.
astype
(
floatX
),
inputs_test_values
=
[
n
p
.
random
.
random
((
2
,
3
))
.
astype
(
floatX
),
n
umpy
.
random
.
random
((
3
))
.
astype
(
floatX
)]
n
p
.
random
.
random
((
3
))
.
astype
(
floatX
)]
theano
.
tests
.
unittest_tools
.
verify_grad
(
get_sum_of_grad
,
theano
.
tests
.
unittest_tools
.
verify_grad
(
get_sum_of_grad
,
inputs_test_values
)
inputs_test_values
)
...
@@ -781,7 +781,7 @@ class T_Scan(unittest.TestCase):
...
@@ -781,7 +781,7 @@ class T_Scan(unittest.TestCase):
# forward pass
# forward pass
W
=
theano
.
shared
(
W
=
theano
.
shared
(
n
umpy
.
random
.
randn
(
2
,
2
)
.
astype
(
'float32'
),
n
p
.
random
.
randn
(
2
,
2
)
.
astype
(
'float32'
),
name
=
"W"
,
borrow
=
True
)
name
=
"W"
,
borrow
=
True
)
def
forward_scanner
(
x_t
):
def
forward_scanner
(
x_t
):
...
@@ -807,7 +807,7 @@ class T_Scan(unittest.TestCase):
...
@@ -807,7 +807,7 @@ class T_Scan(unittest.TestCase):
# vectors, weights are scalars; using shared variables and past
# vectors, weights are scalars; using shared variables and past
# taps (sequences and outputs)
# taps (sequences and outputs)
def
test_using_taps_input_output
(
self
):
def
test_using_taps_input_output
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW
=
asarrayX
(
rng
.
uniform
())
vW
=
asarrayX
(
rng
.
uniform
())
vW_in
=
asarrayX
(
rng
.
uniform
())
vW_in
=
asarrayX
(
rng
.
uniform
())
vu
=
asarrayX
(
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
))
vu
=
asarrayX
(
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
))
...
@@ -843,7 +843,7 @@ class T_Scan(unittest.TestCase):
...
@@ -843,7 +843,7 @@ class T_Scan(unittest.TestCase):
# in scan) which might seem strange, but then again why not use
# in scan) which might seem strange, but then again why not use
# v_0[t] instead of v_0[t-2] in a real application ??
# v_0[t] instead of v_0[t-2] in a real application ??
# also vx0[0] corresponds to vx0[-2], vx0[1] to vx0[-1]
# also vx0[0] corresponds to vx0[-2], vx0[1] to vx0[-1]
numpy_out
=
n
umpy
.
zeros
((
2
,))
numpy_out
=
n
p
.
zeros
((
2
,))
numpy_out
[
0
]
=
vu
[
0
]
*
vW_in
+
vx0
[
1
]
*
vW
+
vx0
[
0
]
numpy_out
[
0
]
=
vu
[
0
]
*
vW_in
+
vx0
[
1
]
*
vW
+
vx0
[
0
]
numpy_out
[
1
]
=
vu
[
1
]
*
vW_in
+
numpy_out
[
0
]
*
vW
+
vx0
[
1
]
numpy_out
[
1
]
=
vu
[
1
]
*
vW_in
+
numpy_out
[
0
]
*
vW
+
vx0
[
1
]
utt
.
assert_allclose
(
numpy_out
,
theano_out
)
utt
.
assert_allclose
(
numpy_out
,
theano_out
)
...
@@ -852,7 +852,7 @@ class T_Scan(unittest.TestCase):
...
@@ -852,7 +852,7 @@ class T_Scan(unittest.TestCase):
# vectors, weights are scalars; using shared variables and past
# vectors, weights are scalars; using shared variables and past
# taps (sequences and outputs) and future taps for sequences
# taps (sequences and outputs) and future taps for sequences
def
test_past_future_taps_shared
(
self
):
def
test_past_future_taps_shared
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW
=
asarrayX
(
rng
.
uniform
())
vW
=
asarrayX
(
rng
.
uniform
())
vW_in
=
asarrayX
(
rng
.
uniform
())
vW_in
=
asarrayX
(
rng
.
uniform
())
vu
=
asarrayX
(
rng
.
uniform
(
size
=
(
6
,),
low
=-
5.
,
high
=
5.
))
vu
=
asarrayX
(
rng
.
uniform
(
size
=
(
6
,),
low
=-
5.
,
high
=
5.
))
...
@@ -880,7 +880,7 @@ class T_Scan(unittest.TestCase):
...
@@ -880,7 +880,7 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
theano_out
=
f8
(
vu
,
vx0
)
theano_out
=
f8
(
vu
,
vx0
)
# compute output in numpy
# compute output in numpy
numpy_out
=
n
umpy
.
zeros
(
2
)
numpy_out
=
n
p
.
zeros
(
2
)
# think of vu[0] as vu[-2], vu[4] as vu[2]
# think of vu[0] as vu[-2], vu[4] as vu[2]
# and vx0[0] as vx0[-2], vx0[1] as vx0[-1]
# and vx0[0] as vx0[-2], vx0[1] as vx0[-1]
numpy_out
[
0
]
=
(
vu
[
0
]
+
vu
[
4
])
*
vW_in
+
vx0
[
1
]
*
vW
+
vx0
[
0
]
numpy_out
[
0
]
=
(
vu
[
0
]
+
vu
[
4
])
*
vW_in
+
vx0
[
1
]
*
vW
+
vx0
[
0
]
...
@@ -889,9 +889,9 @@ class T_Scan(unittest.TestCase):
...
@@ -889,9 +889,9 @@ class T_Scan(unittest.TestCase):
# simple rnn ; compute inplace version 1
# simple rnn ; compute inplace version 1
def
test_inplace1
(
self
):
def
test_inplace1
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW
=
asarrayX
(
n
umpy
.
random
.
uniform
())
vW
=
asarrayX
(
n
p
.
random
.
uniform
())
vW_in
=
asarrayX
(
n
umpy
.
random
.
uniform
())
vW_in
=
asarrayX
(
n
p
.
random
.
uniform
())
vu0
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
vu0
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
vu1
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
vu1
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
vu2
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
vu2
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
...
@@ -934,8 +934,8 @@ class T_Scan(unittest.TestCase):
...
@@ -934,8 +934,8 @@ class T_Scan(unittest.TestCase):
assert
0
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
assert
0
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
assert
1
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
assert
1
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
# compute output in numpy
# compute output in numpy
numpy_x0
=
n
umpy
.
zeros
((
3
,))
numpy_x0
=
n
p
.
zeros
((
3
,))
numpy_x1
=
n
umpy
.
zeros
((
3
,))
numpy_x1
=
n
p
.
zeros
((
3
,))
numpy_x0
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx0
*
vW
+
vu1
[
0
]
*
vu2
[
0
]
numpy_x0
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx0
*
vW
+
vu1
[
0
]
*
vu2
[
0
]
numpy_x1
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx1
*
vW
+
vu1
[
0
]
+
vu2
[
0
]
numpy_x1
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx1
*
vW
+
vu1
[
0
]
+
vu2
[
0
]
for
i
in
xrange
(
1
,
3
):
for
i
in
xrange
(
1
,
3
):
...
@@ -953,9 +953,9 @@ class T_Scan(unittest.TestCase):
...
@@ -953,9 +953,9 @@ class T_Scan(unittest.TestCase):
# simple rnn ; compute inplace version 2
# simple rnn ; compute inplace version 2
def
test_inplace2
(
self
):
def
test_inplace2
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW
=
asarrayX
(
n
umpy
.
random
.
uniform
())
vW
=
asarrayX
(
n
p
.
random
.
uniform
())
vW_in
=
asarrayX
(
n
umpy
.
random
.
uniform
())
vW_in
=
asarrayX
(
n
p
.
random
.
uniform
())
vu0
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
vu0
=
asarrayX
(
rng
.
uniform
(
size
=
(
3
,),
low
=-
5.
,
high
=
5.
))
vu1
=
asarrayX
(
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
))
vu1
=
asarrayX
(
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
))
vu2
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,),
low
=-
5.
,
high
=
5.
))
vu2
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,),
low
=-
5.
,
high
=
5.
))
...
@@ -1006,8 +1006,8 @@ class T_Scan(unittest.TestCase):
...
@@ -1006,8 +1006,8 @@ class T_Scan(unittest.TestCase):
assert
0
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
assert
0
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
assert
1
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
assert
1
in
scan_node
[
0
]
.
op
.
destroy_map
.
keys
()
# compute output in numpy
# compute output in numpy
numpy_x0
=
n
umpy
.
zeros
((
3
,))
numpy_x0
=
n
p
.
zeros
((
3
,))
numpy_x1
=
n
umpy
.
zeros
((
3
,))
numpy_x1
=
n
p
.
zeros
((
3
,))
numpy_x0
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx0
*
vW
+
vu1
[
0
]
*
vu1
[
1
]
numpy_x0
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx0
*
vW
+
vu1
[
0
]
*
vu1
[
1
]
numpy_x1
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx1
*
vW
+
vu2
[
0
]
+
vu2
[
1
]
+
vu2
[
2
]
numpy_x1
[
0
]
=
vu0
[
0
]
*
vW_in
+
vx1
*
vW
+
vu2
[
0
]
+
vu2
[
1
]
+
vu2
[
2
]
for
i
in
xrange
(
1
,
3
):
for
i
in
xrange
(
1
,
3
):
...
@@ -1024,7 +1024,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1024,7 +1024,7 @@ class T_Scan(unittest.TestCase):
utt
.
assert_allclose
(
theano_x1
,
numpy_x1
)
utt
.
assert_allclose
(
theano_x1
,
numpy_x1
)
def
test_inplace3
(
self
):
def
test_inplace3
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vx0
=
asarrayX
(
rng
.
uniform
())
vx0
=
asarrayX
(
rng
.
uniform
())
vx1
=
asarrayX
(
rng
.
uniform
())
vx1
=
asarrayX
(
rng
.
uniform
())
...
@@ -1035,7 +1035,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1035,7 +1035,7 @@ class T_Scan(unittest.TestCase):
[],
[],
[
x0
,
x1
],
[
x0
,
x1
],
n_steps
=
3
)
n_steps
=
3
)
x0
=
asarrayX
(
n
umpy
.
zeros
((
3
,)))
x0
=
asarrayX
(
n
p
.
zeros
((
3
,)))
x0
[
0
]
=
vx0
x0
[
0
]
=
vx0
x0
=
theano
.
tensor
.
constant
(
x0
)
x0
=
theano
.
tensor
.
constant
(
x0
)
to_replace
=
outputs
[
0
]
.
owner
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
to_replace
=
outputs
[
0
]
.
owner
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
...
@@ -1053,7 +1053,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1053,7 +1053,7 @@ class T_Scan(unittest.TestCase):
# Shared variable with updates
# Shared variable with updates
def
test_shared_arguments_with_updates
(
self
):
def
test_shared_arguments_with_updates
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW1
=
asarrayX
(
rng
.
rand
(
2
,
3
))
vW1
=
asarrayX
(
rng
.
rand
(
2
,
3
))
vW2
=
asarrayX
(
rng
.
rand
(
3
,
2
))
vW2
=
asarrayX
(
rng
.
rand
(
3
,
2
))
...
@@ -1128,22 +1128,22 @@ class T_Scan(unittest.TestCase):
...
@@ -1128,22 +1128,22 @@ class T_Scan(unittest.TestCase):
theano_y0
,
theano_y1
,
theano_y2
=
allstuff
theano_y0
,
theano_y1
,
theano_y2
=
allstuff
# do things in numpy
# do things in numpy
numpy_y0
=
n
umpy
.
zeros
((
6
,
2
))
numpy_y0
=
n
p
.
zeros
((
6
,
2
))
numpy_y1
=
n
umpy
.
zeros
((
4
,
2
))
numpy_y1
=
n
p
.
zeros
((
4
,
2
))
numpy_y2
=
n
umpy
.
zeros
((
3
,
3
))
numpy_y2
=
n
p
.
zeros
((
3
,
3
))
numpy_y0
[:
3
]
=
vy0
numpy_y0
[:
3
]
=
vy0
numpy_y1
[
0
]
=
vy1
numpy_y1
[
0
]
=
vy1
numpy_W1
=
vW1
.
copy
()
numpy_W1
=
vW1
.
copy
()
numpy_W2
=
vW2
.
copy
()
numpy_W2
=
vW2
.
copy
()
for
idx
in
xrange
(
3
):
for
idx
in
xrange
(
3
):
numpy_y0
[
idx
+
3
]
=
n
umpy
.
dot
(
numpy
.
dot
(
vu1
[
idx
,
:],
numpy_W1
),
numpy_y0
[
idx
+
3
]
=
n
p
.
dot
(
np
.
dot
(
vu1
[
idx
,
:],
numpy_W1
),
numpy_W2
)
+
\
numpy_W2
)
+
\
0.1
*
numpy_y0
[
idx
+
2
]
+
\
0.1
*
numpy_y0
[
idx
+
2
]
+
\
0.33
*
numpy_y0
[
idx
+
1
]
+
\
0.33
*
numpy_y0
[
idx
+
1
]
+
\
0.17
*
numpy_y0
[
idx
]
0.17
*
numpy_y0
[
idx
]
numpy_y1
[
idx
+
1
]
=
(
n
umpy
.
dot
(
vu2
[
idx
,
:],
numpy_W2
)
+
numpy_y1
[
idx
+
1
]
=
(
n
p
.
dot
(
vu2
[
idx
,
:],
numpy_W2
)
+
numpy_y1
[
idx
])
numpy_y1
[
idx
])
numpy_y2
[
idx
]
=
n
umpy
.
dot
(
vu1
[
idx
,
:],
numpy_W1
)
numpy_y2
[
idx
]
=
n
p
.
dot
(
vu1
[
idx
,
:],
numpy_W1
)
numpy_W1
=
numpy_W1
+
.
1
numpy_W1
=
numpy_W1
+
.
1
numpy_W2
=
numpy_W2
+
.
05
numpy_W2
=
numpy_W2
+
.
05
...
@@ -1174,7 +1174,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1174,7 +1174,7 @@ class T_Scan(unittest.TestCase):
f
=
theano
.
function
([
c
,
x
,
y
],
[
gX
,
gY
],
f
=
theano
.
function
([
c
,
x
,
y
],
[
gX
,
gY
],
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# Check for runtime errors
# Check for runtime errors
f
(
n
umpy
.
int32
(
0
),
numpy
.
float32
(
1.
),
numpy
.
float32
(
.
5
))
f
(
n
p
.
int32
(
0
),
np
.
float32
(
1.
),
np
.
float32
(
.
5
))
def
test_simple_shared_mrg_random
(
self
):
def
test_simple_shared_mrg_random
(
self
):
theano_rng
=
theano
.
sandbox
.
rng_mrg
.
MRG_RandomStreams
(
utt
.
fetch_seed
())
theano_rng
=
theano
.
sandbox
.
rng_mrg
.
MRG_RandomStreams
(
utt
.
fetch_seed
())
...
@@ -1211,10 +1211,10 @@ class T_Scan(unittest.TestCase):
...
@@ -1211,10 +1211,10 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng_seed
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
.
randint
(
2
**
30
)
rng_seed
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
.
randint
(
2
**
30
)
rng
=
n
umpy
.
random
.
RandomState
(
int
(
rng_seed
))
# int() is for 32bit
rng
=
n
p
.
random
.
RandomState
(
int
(
rng_seed
))
# int() is for 32bit
numpy_v
=
n
umpy
.
zeros
((
10
,
2
))
numpy_v
=
n
p
.
zeros
((
10
,
2
))
for
i
in
xrange
(
10
):
for
i
in
xrange
(
10
):
numpy_v
[
i
]
=
rng
.
uniform
(
-
1
,
1
,
size
=
(
2
,))
numpy_v
[
i
]
=
rng
.
uniform
(
-
1
,
1
,
size
=
(
2
,))
...
@@ -1224,12 +1224,12 @@ class T_Scan(unittest.TestCase):
...
@@ -1224,12 +1224,12 @@ class T_Scan(unittest.TestCase):
utt
.
assert_allclose
(
theano_v
,
numpy_v
[
5
:,
:])
utt
.
assert_allclose
(
theano_v
,
numpy_v
[
5
:,
:])
def
test_gibbs_chain
(
self
):
def
test_gibbs_chain
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_W
=
n
umpy
.
array
(
rng
.
rand
(
20
,
30
)
-
.
5
,
dtype
=
'float32'
)
v_W
=
n
p
.
array
(
rng
.
rand
(
20
,
30
)
-
.
5
,
dtype
=
'float32'
)
v_vsample
=
n
umpy
.
array
(
rng
.
binomial
(
1
,
.
5
,
size
=
(
3
,
20
),),
v_vsample
=
n
p
.
array
(
rng
.
binomial
(
1
,
.
5
,
size
=
(
3
,
20
),),
dtype
=
'float32'
)
dtype
=
'float32'
)
v_bvis
=
n
umpy
.
array
(
rng
.
rand
(
20
)
-
.
5
,
dtype
=
'float32'
)
v_bvis
=
n
p
.
array
(
rng
.
rand
(
20
)
-
.
5
,
dtype
=
'float32'
)
v_bhid
=
n
umpy
.
array
(
rng
.
rand
(
30
)
-
.
5
,
dtype
=
'float32'
)
v_bhid
=
n
p
.
array
(
rng
.
rand
(
30
)
-
.
5
,
dtype
=
'float32'
)
W
=
theano
.
shared
(
v_W
,
'vW'
)
W
=
theano
.
shared
(
v_W
,
'vW'
)
bhid
=
theano
.
shared
(
v_bhid
,
'vbhid'
)
bhid
=
theano
.
shared
(
v_bhid
,
'vbhid'
)
bvis
=
theano
.
shared
(
v_bvis
,
'vbvis'
)
bvis
=
theano
.
shared
(
v_bvis
,
'vbvis'
)
...
@@ -1261,24 +1261,24 @@ class T_Scan(unittest.TestCase):
...
@@ -1261,24 +1261,24 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
_rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
_rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng_seed
=
_rng
.
randint
(
2
**
30
)
rng_seed
=
_rng
.
randint
(
2
**
30
)
nrng1
=
n
umpy
.
random
.
RandomState
(
int
(
rng_seed
))
# int() is for 32bit
nrng1
=
n
p
.
random
.
RandomState
(
int
(
rng_seed
))
# int() is for 32bit
rng_seed
=
_rng
.
randint
(
2
**
30
)
rng_seed
=
_rng
.
randint
(
2
**
30
)
nrng2
=
n
umpy
.
random
.
RandomState
(
int
(
rng_seed
))
# int() is for 32bit
nrng2
=
n
p
.
random
.
RandomState
(
int
(
rng_seed
))
# int() is for 32bit
def
numpy_implementation
(
vsample
):
def
numpy_implementation
(
vsample
):
for
idx
in
range
(
10
):
for
idx
in
range
(
10
):
hmean
=
1.
/
(
1.
+
n
umpy
.
exp
(
-
(
numpy
.
dot
(
vsample
,
v_W
)
+
\
hmean
=
1.
/
(
1.
+
n
p
.
exp
(
-
(
np
.
dot
(
vsample
,
v_W
)
+
\
v_bhid
)))
v_bhid
)))
hsample
=
n
umpy
.
array
(
nrng1
.
binomial
(
1
,
hsample
=
n
p
.
array
(
nrng1
.
binomial
(
1
,
hmean
,
hmean
,
size
=
hmean
.
shape
),
size
=
hmean
.
shape
),
dtype
=
'float32'
)
dtype
=
'float32'
)
vmean
=
1.
/
(
1.
+
n
umpy
.
exp
(
-
(
numpy
.
dot
(
hsample
,
v_W
.
T
)
+
\
vmean
=
1.
/
(
1.
+
n
p
.
exp
(
-
(
np
.
dot
(
hsample
,
v_W
.
T
)
+
\
v_bvis
)))
v_bvis
)))
vsample
=
n
umpy
.
array
(
nrng2
.
binomial
(
1
,
vsample
=
n
p
.
array
(
nrng2
.
binomial
(
1
,
vmean
,
vmean
,
size
=
vmean
.
shape
),
size
=
vmean
.
shape
),
dtype
=
'float32'
)
dtype
=
'float32'
)
...
@@ -1290,7 +1290,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1290,7 +1290,7 @@ class T_Scan(unittest.TestCase):
utt
.
assert_allclose
(
t_result
,
n_result
)
utt
.
assert_allclose
(
t_result
,
n_result
)
def
test_only_shared_no_input_no_output
(
self
):
def
test_only_shared_no_input_no_output
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_state
=
asarrayX
(
rng
.
uniform
())
v_state
=
asarrayX
(
rng
.
uniform
())
state
=
theano
.
shared
(
v_state
,
'vstate'
)
state
=
theano
.
shared
(
v_state
,
'vstate'
)
...
@@ -1331,7 +1331,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1331,7 +1331,7 @@ class T_Scan(unittest.TestCase):
outputs
,
outputs
,
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
5
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
5
,),
low
=-
5.
,
high
=
5.
)
numpy_result
=
v_u
+
3
numpy_result
=
v_u
+
3
...
@@ -1352,7 +1352,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1352,7 +1352,7 @@ class T_Scan(unittest.TestCase):
updates
=
abs_updates
,
updates
=
abs_updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vals
=
rng
.
uniform
(
size
=
(
10
,),
low
=-
5.
,
high
=
5.
)
vals
=
rng
.
uniform
(
size
=
(
10
,),
low
=-
5.
,
high
=
5.
)
abs_vals
=
abs
(
vals
)
abs_vals
=
abs
(
vals
)
theano_vals
=
f
(
vals
)
theano_vals
=
f
(
vals
)
...
@@ -1380,14 +1380,14 @@ class T_Scan(unittest.TestCase):
...
@@ -1380,14 +1380,14 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_x0
=
rng
.
uniform
()
v_x0
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
# compute the output in numpy
# compute the output in numpy
v_out
=
n
umpy
.
zeros
((
4
,))
v_out
=
n
p
.
zeros
((
4
,))
v_out
[
0
]
=
v_u
[
3
]
*
W_in
+
v_x0
*
W
v_out
[
0
]
=
v_u
[
3
]
*
W_in
+
v_x0
*
W
for
step
in
xrange
(
1
,
4
):
for
step
in
xrange
(
1
,
4
):
v_out
[
step
]
=
v_u
[
3
-
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
v_out
[
step
]
=
v_u
[
3
-
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
...
@@ -1404,9 +1404,9 @@ class T_Scan(unittest.TestCase):
...
@@ -1404,9 +1404,9 @@ class T_Scan(unittest.TestCase):
result
,
result
,
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_v
=
rng
.
uniform
(
size
=
(
5
,),
low
=-
5.
,
high
=
5.
)
v_v
=
rng
.
uniform
(
size
=
(
5
,),
low
=-
5.
,
high
=
5.
)
assert
abs
(
n
umpy
.
sum
(
v_v
)
-
f
(
v_v
,
0.
))
<
1e-3
assert
abs
(
n
p
.
sum
(
v_v
)
-
f
(
v_v
,
0.
))
<
1e-3
def
test_grad_one_output
(
self
):
def
test_grad_one_output
(
self
):
def
f_rnn
(
u_t
,
x_tm1
,
W_in
,
W
):
def
f_rnn
(
u_t
,
x_tm1
,
W_in
,
W
):
...
@@ -1440,12 +1440,12 @@ class T_Scan(unittest.TestCase):
...
@@ -1440,12 +1440,12 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
10
,),
low
=-.
5
,
high
=.
5
),
v_u
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
10
,),
low
=-.
5
,
high
=.
5
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
v_x0
=
n
umpy
.
array
(
rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
v_x0
=
n
p
.
array
(
rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
W
=
n
umpy
.
array
(
rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
W
=
n
p
.
array
(
rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
W_in
=
n
umpy
.
array
(
rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
W_in
=
n
p
.
array
(
rng
.
uniform
(),
dtype
=
theano
.
config
.
floatX
)
analytic_grad
=
grad_fn
(
v_u
,
v_x0
,
W_in
,
W
)
analytic_grad
=
grad_fn
(
v_u
,
v_x0
,
W_in
,
W
)
num_grad
=
multiple_outputs_numeric_grad
(
num_grad
=
multiple_outputs_numeric_grad
(
...
@@ -1459,7 +1459,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1459,7 +1459,7 @@ class T_Scan(unittest.TestCase):
num_grad
.
gx
[
max_err_pos
]))
num_grad
.
gx
[
max_err_pos
]))
def
test_grad_multiple_outs
(
self
):
def
test_grad_multiple_outs
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
1
,
high
=.
1
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
1
,
high
=.
1
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
1
,
high
=.
1
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
1
,
high
=.
1
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
1
,
high
=.
1
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
1
,
high
=.
1
))
...
@@ -1524,7 +1524,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1524,7 +1524,7 @@ class T_Scan(unittest.TestCase):
@attr
(
'slow'
)
@attr
(
'slow'
)
def
test_grad_multiple_outs_taps
(
self
):
def
test_grad_multiple_outs_taps
(
self
):
l
=
5
l
=
5
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
2
,
high
=.
2
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
2
,
high
=.
2
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
...
@@ -1618,7 +1618,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1618,7 +1618,7 @@ class T_Scan(unittest.TestCase):
@attr
(
'slow'
)
@attr
(
'slow'
)
def
test_grad_multiple_outs_taps_backwards
(
self
):
def
test_grad_multiple_outs_taps_backwards
(
self
):
l
=
5
l
=
5
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
2
,
high
=.
2
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
2
,
high
=.
2
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
2
,
high
=.
2
))
...
@@ -1685,10 +1685,10 @@ class T_Scan(unittest.TestCase):
...
@@ -1685,10 +1685,10 @@ class T_Scan(unittest.TestCase):
num_grad
.
gx
[
max_err_pos
]))
num_grad
.
gx
[
max_err_pos
]))
def
test_grad_multiple_outs_some_uncomputable
(
self
):
def
test_grad_multiple_outs_some_uncomputable
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
3.
,
high
=
3.
))
vW_in
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
3.
,
high
=
3.
))
v_u
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,
2
),
low
=-
3.
,
high
=
3.
))
v_u
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,
2
),
low
=-
3.
,
high
=
3.
))
v_u2
=
n
umpy
.
array
([
1
,
3
,
4
,
6
,
8
],
dtype
=
'int32'
)
v_u2
=
n
p
.
array
([
1
,
3
,
4
,
6
,
8
],
dtype
=
'int32'
)
v_x0
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
3.
,
high
=
3.
))
v_x0
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
3.
,
high
=
3.
))
W_in
=
theano
.
tensor
.
matrix
(
'win'
)
W_in
=
theano
.
tensor
.
matrix
(
'win'
)
...
@@ -1730,9 +1730,9 @@ class T_Scan(unittest.TestCase):
...
@@ -1730,9 +1730,9 @@ class T_Scan(unittest.TestCase):
def
reset_rng_fn
(
fn
,
*
args
):
def
reset_rng_fn
(
fn
,
*
args
):
for
idx
,
arg
in
enumerate
(
fn
.
maker
.
expanded_inputs
):
for
idx
,
arg
in
enumerate
(
fn
.
maker
.
expanded_inputs
):
if
(
arg
.
value
and
type
(
arg
.
value
.
data
)
==
\
if
(
arg
.
value
and
type
(
arg
.
value
.
data
)
==
\
type
(
n
umpy
.
random
.
RandomState
(
123
))):
type
(
n
p
.
random
.
RandomState
(
123
))):
obj
=
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
obj
=
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
obj
.
data
=
n
umpy
.
random
.
RandomState
(
123
)
obj
.
data
=
n
p
.
random
.
RandomState
(
123
)
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
=
obj
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
=
obj
return
fn
(
*
args
)
return
fn
(
*
args
)
...
@@ -1764,7 +1764,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1764,7 +1764,7 @@ class T_Scan(unittest.TestCase):
assert
(
result
==
expected_result
)
assert
(
result
==
expected_result
)
def
test_grad_multiple_outs_some_truncate
(
self
):
def
test_grad_multiple_outs_some_truncate
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
1
,
high
=.
1
))
vW_in
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-.
1
,
high
=.
1
))
v_u
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,
2
),
low
=-.
1
,
high
=.
1
))
v_u
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,
2
),
low
=-.
1
,
high
=.
1
))
v_x0
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
1
,
high
=.
1
))
v_x0
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-.
1
,
high
=.
1
))
...
@@ -1807,9 +1807,9 @@ class T_Scan(unittest.TestCase):
...
@@ -1807,9 +1807,9 @@ class T_Scan(unittest.TestCase):
def
reset_rng_fn
(
fn
,
*
args
):
def
reset_rng_fn
(
fn
,
*
args
):
for
idx
,
arg
in
enumerate
(
fn
.
maker
.
expanded_inputs
):
for
idx
,
arg
in
enumerate
(
fn
.
maker
.
expanded_inputs
):
if
(
arg
.
value
and
if
(
arg
.
value
and
isinstance
(
arg
.
value
.
data
,
n
umpy
.
random
.
RandomState
)):
isinstance
(
arg
.
value
.
data
,
n
p
.
random
.
RandomState
)):
obj
=
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
obj
=
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
obj
.
data
=
n
umpy
.
random
.
RandomState
(
123
)
obj
.
data
=
n
p
.
random
.
RandomState
(
123
)
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
=
obj
fn
.
maker
.
expanded_inputs
[
idx
]
.
value
=
obj
out
=
fn
(
*
args
)
out
=
fn
(
*
args
)
return
out
return
out
...
@@ -1819,7 +1819,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1819,7 +1819,7 @@ class T_Scan(unittest.TestCase):
num_grad
=
multiple_outputs_numeric_grad
(
num_grad
=
multiple_outputs_numeric_grad
(
reset_rng_cost_fn
,
[
v_u
,
v_x0
,
vW_in
])
reset_rng_cost_fn
,
[
v_u
,
v_x0
,
vW_in
])
analytic_grad
=
reset_rng_grad_fn
(
v_u
,
v_x0
,
vW_in
)
analytic_grad
=
reset_rng_grad_fn
(
v_u
,
v_x0
,
vW_in
)
utt
.
assert_allclose
(
analytic_grad
[
0
][:
2
],
n
umpy
.
zeros
((
2
,
2
)))
utt
.
assert_allclose
(
analytic_grad
[
0
][:
2
],
n
p
.
zeros
((
2
,
2
)))
def
test_grad_multiple_outs_some_disconnected
(
self
):
def
test_grad_multiple_outs_some_disconnected
(
self
):
final_cost
=
self
.
_grad_mout_helper
(
100
,
mode_nodebug
)
final_cost
=
self
.
_grad_mout_helper
(
100
,
mode_nodebug
)
...
@@ -1833,7 +1833,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1833,7 +1833,7 @@ class T_Scan(unittest.TestCase):
def
_grad_mout_helper
(
self
,
n_iters
,
mode
):
def
_grad_mout_helper
(
self
,
n_iters
,
mode
):
# Created on Tue Oct 07 13:28:51 2014
# Created on Tue Oct 07 13:28:51 2014
# @author: vaneetke
# @author: vaneetke
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
n_hid
=
3
n_hid
=
3
n_in
=
1
n_in
=
1
n_out
=
1
n_out
=
1
...
@@ -1897,10 +1897,10 @@ class T_Scan(unittest.TestCase):
...
@@ -1897,10 +1897,10 @@ class T_Scan(unittest.TestCase):
mode
=
mode
)
mode
=
mode
)
# artificial data
# artificial data
x_v
=
n
umpy
.
arange
(
0.
,
10.49
,
0.21
,
dtype
=
theano
.
config
.
floatX
)
x_v
=
n
p
.
arange
(
0.
,
10.49
,
0.21
,
dtype
=
theano
.
config
.
floatX
)
x_v
=
x_v
.
reshape
(
len
(
x_v
),
1
)
x_v
=
x_v
.
reshape
(
len
(
x_v
),
1
)
s_v
=
n
umpy
.
sin
(
x_v
)
s_v
=
n
p
.
sin
(
x_v
)
t_v
=
n
umpy
.
roll
(
s_v
,
-
1
)[:
-
1
]
t_v
=
n
p
.
roll
(
s_v
,
-
1
)[:
-
1
]
s_v
=
s_v
[:
-
1
]
s_v
=
s_v
[:
-
1
]
for
i
in
xrange
(
n_iters
):
for
i
in
xrange
(
n_iters
):
cost
=
learn_rnn_fn
(
s_v
,
t_v
)
cost
=
learn_rnn_fn
(
s_v
,
t_v
)
...
@@ -1919,14 +1919,14 @@ class T_Scan(unittest.TestCase):
...
@@ -1919,14 +1919,14 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
nx
=
rng
.
uniform
(
size
=
(
10
,
10
))
nx
=
rng
.
uniform
(
size
=
(
10
,
10
))
ny1
,
nz1
=
f
(
nx
)
ny1
,
nz1
=
f
(
nx
)
ny2
,
nz2
=
f
(
nx
)
ny2
,
nz2
=
f
(
nx
)
utt
.
assert_allclose
([
ny1
,
ny1
],
nz1
)
utt
.
assert_allclose
([
ny1
,
ny1
],
nz1
)
utt
.
assert_allclose
([
ny2
,
ny2
],
nz2
)
utt
.
assert_allclose
([
ny2
,
ny2
],
nz2
)
assert
not
n
umpy
.
allclose
(
ny1
,
ny2
)
assert
not
n
p
.
allclose
(
ny1
,
ny2
)
def
test_grad_of_shared
(
self
):
def
test_grad_of_shared
(
self
):
x1
=
theano
.
shared
(
3.
)
x1
=
theano
.
shared
(
3.
)
...
@@ -1942,7 +1942,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1942,7 +1942,7 @@ class T_Scan(unittest.TestCase):
def
test_computing_gradient
(
self
):
def
test_computing_gradient
(
self
):
x1
=
theano
.
tensor
.
scalar
(
'x1'
)
x1
=
theano
.
tensor
.
scalar
(
'x1'
)
x2
=
theano
.
shared
(
n
umpy
.
array
([
1
,
2
,
3
,
4
,
5
]),
name
=
'x2'
)
x2
=
theano
.
shared
(
n
p
.
array
([
1
,
2
,
3
,
4
,
5
]),
name
=
'x2'
)
K
=
x2
*
x1
K
=
x2
*
x1
out
,
updates
=
theano
.
scan
(
lambda
i
,
v
:
theano
.
tensor
.
grad
(
K
[
i
],
v
),
out
,
updates
=
theano
.
scan
(
lambda
i
,
v
:
theano
.
tensor
.
grad
(
K
[
i
],
v
),
...
@@ -1950,10 +1950,10 @@ class T_Scan(unittest.TestCase):
...
@@ -1950,10 +1950,10 @@ class T_Scan(unittest.TestCase):
non_sequences
=
x1
)
non_sequences
=
x1
)
f
=
theano
.
function
([
x1
],
out
,
allow_input_downcast
=
True
)
f
=
theano
.
function
([
x1
],
out
,
allow_input_downcast
=
True
)
assert
n
umpy
.
all
(
f
(
3.
)
!=
0.
)
assert
n
p
.
all
(
f
(
3.
)
!=
0.
)
def
test_shared_updates
(
self
):
def
test_shared_updates
(
self
):
X
=
theano
.
shared
(
n
umpy
.
array
(
1
))
X
=
theano
.
shared
(
n
p
.
array
(
1
))
out
,
updates
=
theano
.
scan
(
out
,
updates
=
theano
.
scan
(
lambda
:
OrderedDict
([(
X
,
(
X
+
1
))]),
lambda
:
OrderedDict
([(
X
,
(
X
+
1
))]),
...
@@ -1967,8 +1967,8 @@ class T_Scan(unittest.TestCase):
...
@@ -1967,8 +1967,8 @@ class T_Scan(unittest.TestCase):
assert
X
.
get_value
()
==
11
assert
X
.
get_value
()
==
11
def
test_memory_aliasing_updates
(
self
):
def
test_memory_aliasing_updates
(
self
):
x
=
theano
.
shared
(
n
umpy
.
array
(
1
))
x
=
theano
.
shared
(
n
p
.
array
(
1
))
y
=
theano
.
shared
(
n
umpy
.
array
(
1
))
y
=
theano
.
shared
(
n
p
.
array
(
1
))
out
,
updates
=
theano
.
scan
(
out
,
updates
=
theano
.
scan
(
lambda
:
OrderedDict
([(
x
,
x
+
1
),
(
y
,
x
)]),
lambda
:
OrderedDict
([(
x
,
x
+
1
),
(
y
,
x
)]),
...
@@ -1979,7 +1979,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1979,7 +1979,7 @@ class T_Scan(unittest.TestCase):
f
=
theano
.
function
([],
[],
updates
=
updates
)
f
=
theano
.
function
([],
[],
updates
=
updates
)
f
()
f
()
assert
not
n
umpy
.
may_share_memory
(
x
.
container
.
storage
[
0
],
assert
not
n
p
.
may_share_memory
(
x
.
container
.
storage
[
0
],
y
.
container
.
storage
[
0
])
y
.
container
.
storage
[
0
])
assert
x
.
get_value
()
!=
y
.
get_value
()
assert
x
.
get_value
()
!=
y
.
get_value
()
...
@@ -1998,7 +1998,7 @@ class T_Scan(unittest.TestCase):
...
@@ -1998,7 +1998,7 @@ class T_Scan(unittest.TestCase):
"""
"""
a
=
theano
.
tensor
.
vector
()
a
=
theano
.
tensor
.
vector
()
init_a
=
theano
.
tensor
.
vector
()
init_a
=
theano
.
tensor
.
vector
()
b
=
theano
.
shared
(
n
umpy
.
random
.
rand
(
5
,
4
))
b
=
theano
.
shared
(
n
p
.
random
.
rand
(
5
,
4
))
def
inner_func
(
a
):
def
inner_func
(
a
):
return
a
+
1
,
OrderedDict
([(
b
,
2
*
b
)])
return
a
+
1
,
OrderedDict
([(
b
,
2
*
b
)])
...
@@ -2032,9 +2032,9 @@ class T_Scan(unittest.TestCase):
...
@@ -2032,9 +2032,9 @@ class T_Scan(unittest.TestCase):
non_sequences
=
[
gy
,
x
])
non_sequences
=
[
gy
,
x
])
f
=
theano
.
function
([
x
,
A
],
hy
,
allow_input_downcast
=
True
)
f
=
theano
.
function
([
x
,
A
],
hy
,
allow_input_downcast
=
True
)
vx
=
n
umpy
.
array
([
1.
,
1.
],
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
array
([
1.
,
1.
],
dtype
=
theano
.
config
.
floatX
)
vA
=
n
umpy
.
array
([[
1.
,
1.
],
[
1.
,
0.
]],
dtype
=
theano
.
config
.
floatX
)
vA
=
n
p
.
array
([[
1.
,
1.
],
[
1.
,
0.
]],
dtype
=
theano
.
config
.
floatX
)
vR
=
n
umpy
.
array
([[
3.6
,
1.8
],
[
1.8
,
0.9
]],
dtype
=
theano
.
config
.
floatX
)
vR
=
n
p
.
array
([[
3.6
,
1.8
],
[
1.8
,
0.9
]],
dtype
=
theano
.
config
.
floatX
)
out
=
f
(
vx
,
vA
)
out
=
f
(
vx
,
vA
)
utt
.
assert_allclose
(
out
,
vR
)
utt
.
assert_allclose
(
out
,
vR
)
...
@@ -2157,7 +2157,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2157,7 +2157,7 @@ class T_Scan(unittest.TestCase):
# some rnn with multiple outputs and multiple inputs; other
# some rnn with multiple outputs and multiple inputs; other
# dimension instead of scalars/vectors
# dimension instead of scalars/vectors
def
test_reordering
(
self
):
def
test_reordering
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
...
@@ -2200,15 +2200,15 @@ class T_Scan(unittest.TestCase):
...
@@ -2200,15 +2200,15 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# compute the values in numpy
# compute the values in numpy
v_x
=
n
umpy
.
zeros
((
3
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_x
=
n
p
.
zeros
((
3
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
umpy
.
zeros
((
3
,),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
p
.
zeros
((
3
,),
dtype
=
theano
.
config
.
floatX
)
v_x
[
0
]
=
n
umpy
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
\
v_x
[
0
]
=
n
p
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
\
n
umpy
.
dot
(
v_x0
,
vW
)
n
p
.
dot
(
v_x0
,
vW
)
v_y
[
0
]
=
n
umpy
.
dot
(
v_x0
,
vWout
)
+
v_y0
[
2
]
v_y
[
0
]
=
n
p
.
dot
(
v_x0
,
vWout
)
+
v_y0
[
2
]
for
i
in
xrange
(
1
,
3
):
for
i
in
xrange
(
1
,
3
):
v_x
[
i
]
=
n
umpy
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
\
v_x
[
i
]
=
n
p
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
\
n
umpy
.
dot
(
v_x
[
i
-
1
],
vW
)
n
p
.
dot
(
v_x
[
i
-
1
],
vW
)
v_y
[
i
]
=
n
umpy
.
dot
(
v_x
[
i
-
1
],
vWout
)
+
v_y
[
i
-
1
]
v_y
[
i
]
=
n
p
.
dot
(
v_x
[
i
-
1
],
vWout
)
+
v_y
[
i
-
1
]
(
theano_dump1
,
theano_dump2
,
theano_x
,
theano_y
)
=
f4
(
v_u1
,
(
theano_dump1
,
theano_dump2
,
theano_x
,
theano_y
)
=
f4
(
v_u1
,
v_u2
,
v_u2
,
...
@@ -2247,7 +2247,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2247,7 +2247,7 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
def
test_save_mem
(
self
):
def
test_save_mem
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
...
@@ -2288,16 +2288,16 @@ class T_Scan(unittest.TestCase):
...
@@ -2288,16 +2288,16 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# compute the values in numpy
# compute the values in numpy
v_x
=
n
umpy
.
zeros
((
8
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_x
=
n
p
.
zeros
((
8
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
umpy
.
zeros
((
8
,),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
p
.
zeros
((
8
,),
dtype
=
theano
.
config
.
floatX
)
v_x
[
0
]
=
n
umpy
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
\
v_x
[
0
]
=
n
p
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
\
n
umpy
.
dot
(
v_x0
,
vW
)
n
p
.
dot
(
v_x0
,
vW
)
v_y
[
0
]
=
n
umpy
.
dot
(
v_x0
,
vWout
)
+
v_y0
[
2
]
v_y
[
0
]
=
n
p
.
dot
(
v_x0
,
vWout
)
+
v_y0
[
2
]
for
i
in
xrange
(
1
,
8
):
for
i
in
xrange
(
1
,
8
):
v_x
[
i
]
=
n
umpy
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
\
v_x
[
i
]
=
n
p
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
\
n
umpy
.
dot
(
v_x
[
i
-
1
],
vW
)
n
p
.
dot
(
v_x
[
i
-
1
],
vW
)
v_y
[
i
]
=
n
umpy
.
dot
(
v_x
[
i
-
1
],
vWout
)
+
v_y
[
i
-
1
]
v_y
[
i
]
=
n
p
.
dot
(
v_x
[
i
-
1
],
vWout
)
+
v_y
[
i
-
1
]
(
theano_dump
,
theano_x
,
theano_y
)
=
f4
(
v_u1
,
v_u2
,
v_x0
,
v_y0
,
vW_in1
)
(
theano_dump
,
theano_x
,
theano_y
)
=
f4
(
v_u1
,
v_u2
,
v_x0
,
v_y0
,
vW_in1
)
...
@@ -2321,24 +2321,24 @@ class T_Scan(unittest.TestCase):
...
@@ -2321,24 +2321,24 @@ class T_Scan(unittest.TestCase):
sh
=
expr
.
shape
[
0
]
sh
=
expr
.
shape
[
0
]
v1
=
theano
.
shared
(
n
umpy
.
ones
(
5
,
dtype
=
theano
.
config
.
floatX
))
v1
=
theano
.
shared
(
n
p
.
ones
(
5
,
dtype
=
theano
.
config
.
floatX
))
v2
=
theano
.
shared
(
n
umpy
.
ones
((
5
,
5
),
dtype
=
theano
.
config
.
floatX
))
v2
=
theano
.
shared
(
n
p
.
ones
((
5
,
5
),
dtype
=
theano
.
config
.
floatX
))
shapef
=
theano
.
function
([
W
],
shapef
=
theano
.
function
([
W
],
expr
,
expr
,
givens
=
OrderedDict
([(
initial
,
v1
),
givens
=
OrderedDict
([(
initial
,
v1
),
(
inpt
,
v2
)]))
(
inpt
,
v2
)]))
# First execution to cache n_steps
# First execution to cache n_steps
shapef
(
n
umpy
.
ones
((
5
,
5
),
dtype
=
theano
.
config
.
floatX
))
shapef
(
n
p
.
ones
((
5
,
5
),
dtype
=
theano
.
config
.
floatX
))
cost
=
expr
.
sum
()
cost
=
expr
.
sum
()
d_cost_wrt_W
=
tensor
.
grad
(
cost
,
[
W
])
d_cost_wrt_W
=
tensor
.
grad
(
cost
,
[
W
])
f
=
theano
.
function
(
f
=
theano
.
function
(
[
W
,
inpt
],
d_cost_wrt_W
,
[
W
,
inpt
],
d_cost_wrt_W
,
givens
=
OrderedDict
([(
initial
,
theano
.
shared
(
n
umpy
.
zeros
(
5
)))]))
givens
=
OrderedDict
([(
initial
,
theano
.
shared
(
n
p
.
zeros
(
5
)))]))
rval
=
n
umpy
.
asarray
([[
5187989
]
*
5
]
*
5
,
dtype
=
theano
.
config
.
floatX
)
rval
=
n
p
.
asarray
([[
5187989
]
*
5
]
*
5
,
dtype
=
theano
.
config
.
floatX
)
arg1
=
n
umpy
.
ones
((
5
,
5
),
dtype
=
theano
.
config
.
floatX
)
arg1
=
n
p
.
ones
((
5
,
5
),
dtype
=
theano
.
config
.
floatX
)
arg2
=
n
umpy
.
ones
((
10
,
5
),
dtype
=
theano
.
config
.
floatX
)
arg2
=
n
p
.
ones
((
10
,
5
),
dtype
=
theano
.
config
.
floatX
)
utt
.
assert_allclose
(
f
(
arg1
,
arg2
),
rval
)
utt
.
assert_allclose
(
f
(
arg1
,
arg2
),
rval
)
def
test_save_mem_reduced_number_of_steps
(
self
):
def
test_save_mem_reduced_number_of_steps
(
self
):
...
@@ -2372,7 +2372,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2372,7 +2372,7 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
20
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
20
,),
low
=-
5.
,
high
=
5.
)
# compute the output in numpy
# compute the output in numpy
...
@@ -2428,7 +2428,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2428,7 +2428,7 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
20
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
20
,),
low
=-
5.
,
high
=
5.
)
# compute the output in numpy
# compute the output in numpy
...
@@ -2474,7 +2474,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2474,7 +2474,7 @@ class T_Scan(unittest.TestCase):
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
init_value
=
5.0
init_value
=
5.0
seq_value
=
n
umpy
.
arange
(
4
,
dtype
=
floatX
)
seq_value
=
n
p
.
arange
(
4
,
dtype
=
floatX
)
output1
,
output2
=
fct
(
init_value
,
seq_value
)
output1
,
output2
=
fct
(
init_value
,
seq_value
)
expected_output1
=
[
init_value
]
expected_output1
=
[
init_value
]
...
@@ -2509,13 +2509,13 @@ class T_Scan(unittest.TestCase):
...
@@ -2509,13 +2509,13 @@ class T_Scan(unittest.TestCase):
[
out1_direct
,
out2_direct
])
[
out1_direct
,
out2_direct
])
# Test that the function returns valid outputs
# Test that the function returns valid outputs
x_val
=
n
umpy
.
arange
(
0
,
4
)[:,
None
]
x_val
=
n
p
.
arange
(
0
,
4
)[:,
None
]
seq_val
=
n
umpy
.
arange
(
4
,
8
)[:,
None
]
seq_val
=
n
p
.
arange
(
4
,
8
)[:,
None
]
out1
,
out2
=
fct
(
x_val
,
seq_val
)
out1
,
out2
=
fct
(
x_val
,
seq_val
)
expected_out1
=
n
umpy
.
zeros
((
5
,
4
,
1
))
expected_out1
=
n
p
.
zeros
((
5
,
4
,
1
))
expected_out2
=
n
umpy
.
zeros
((
5
,
4
,
1
))
expected_out2
=
n
p
.
zeros
((
5
,
4
,
1
))
for
i
in
range
(
4
):
for
i
in
range
(
4
):
expected_out2
[
i
+
1
]
=
expected_out2
[
i
]
+
seq_val
[
i
]
expected_out2
[
i
+
1
]
=
expected_out2
[
i
]
+
seq_val
[
i
]
for
i
in
range
(
5
):
for
i
in
range
(
5
):
...
@@ -2565,7 +2565,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2565,7 +2565,7 @@ class T_Scan(unittest.TestCase):
diff
=
mitsot_m1
+
seq1
diff
=
mitsot_m1
+
seq1
next_mitsot_val
=
mitsot_m2
+
diff
next_mitsot_val
=
mitsot_m2
+
diff
next_sitsot_val
=
sitsot_m1
-
diff
next_sitsot_val
=
sitsot_m1
-
diff
nitsot_out
=
tensor
.
alloc
(
n
umpy
.
asarray
(
0.
,
'float32'
),
nitsot_out
=
tensor
.
alloc
(
n
p
.
asarray
(
0.
,
'float32'
),
next_mitsot_val
+
next_mitsot_val
+
next_sitsot_val
)
next_sitsot_val
)
return
next_sitsot_val
,
next_mitsot_val
,
nitsot_out
return
next_sitsot_val
,
next_mitsot_val
,
nitsot_out
...
@@ -2584,7 +2584,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2584,7 +2584,7 @@ class T_Scan(unittest.TestCase):
assert
(
len
(
scan_nodes_from_fct
(
f
))
==
1
)
assert
(
len
(
scan_nodes_from_fct
(
f
))
==
1
)
# This generate a scan crash during execution.
# This generate a scan crash during execution.
# output_shape = f(n
umpy
.arange(5), 5, [1, 2])
# output_shape = f(n
p
.arange(5), 5, [1, 2])
# assert(all(output_shape == (5, 6)))
# assert(all(output_shape == (5, 6)))
# The following test will fail in DebugMode if there are
# The following test will fail in DebugMode if there are
...
@@ -2608,7 +2608,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2608,7 +2608,7 @@ class T_Scan(unittest.TestCase):
go1
=
theano
.
tensor
.
grad
(
o1
.
mean
(),
wrt
=
x
)
go1
=
theano
.
tensor
.
grad
(
o1
.
mean
(),
wrt
=
x
)
f
=
theano
.
function
([
x
],
go1
,
updates
=
updates
,
f
=
theano
.
function
([
x
],
go1
,
updates
=
updates
,
allow_input_downcast
=
True
,
mode
=
mode_with_opt
)
allow_input_downcast
=
True
,
mode
=
mode_with_opt
)
self
.
assertTrue
(
n
umpy
.
allclose
(
f
([
1
,
2
,
3
]),
2.
/
3
))
self
.
assertTrue
(
n
p
.
allclose
(
f
([
1
,
2
,
3
]),
2.
/
3
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
# this new assert is here to test if scan_merging works ..
# this new assert is here to test if scan_merging works ..
...
@@ -2711,7 +2711,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2711,7 +2711,7 @@ class T_Scan(unittest.TestCase):
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)]
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)]
self
.
assertTrue
(
len
(
scans
)
==
2
)
self
.
assertTrue
(
len
(
scans
)
==
2
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
uniform
(
size
=
(
4
,))
.
astype
(
theano
.
config
.
floatX
)
x_val
=
rng
.
uniform
(
size
=
(
4
,))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
rng
.
uniform
(
size
=
(
4
,))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
rng
.
uniform
(
size
=
(
4
,))
.
astype
(
theano
.
config
.
floatX
)
# Run it so DebugMode can detect optimization problems.
# Run it so DebugMode can detect optimization problems.
...
@@ -2752,7 +2752,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2752,7 +2752,7 @@ class T_Scan(unittest.TestCase):
return
M
return
M
# some initializations
# some initializations
hypx
=
n
umpy
.
log
(
numpy
.
tile
([
1
,
1
,
1
,
1
,
1
,
1
,
0.01
],
(
3
,
1
)))
hypx
=
n
p
.
log
(
np
.
tile
([
1
,
1
,
1
,
1
,
1
,
1
,
0.01
],
(
3
,
1
)))
# variables used in the following expressions
# variables used in the following expressions
hyp
=
theano
.
shared
(
hypx
)
hyp
=
theano
.
shared
(
hypx
)
...
@@ -2763,10 +2763,10 @@ class T_Scan(unittest.TestCase):
...
@@ -2763,10 +2763,10 @@ class T_Scan(unittest.TestCase):
M
=
init_predictive_output
(
inputs
,
targets
,
hyp
,
x_star
,
s_star
)
M
=
init_predictive_output
(
inputs
,
targets
,
hyp
,
x_star
,
s_star
)
X
=
n
umpy
.
random
.
random
((
10
,
4
))
X
=
n
p
.
random
.
random
((
10
,
4
))
Y
=
n
umpy
.
random
.
random
((
10
,
3
))
Y
=
n
p
.
random
.
random
((
10
,
3
))
test_m
=
n
umpy
.
random
.
random
((
4
,))
test_m
=
n
p
.
random
.
random
((
4
,))
test_s
=
n
umpy
.
eye
(
4
)
test_s
=
n
p
.
eye
(
4
)
# Compute expected outputs (jacobian of M wrt x_star)
# Compute expected outputs (jacobian of M wrt x_star)
dfdm
=
theano
.
function
([
inputs
,
targets
,
x_star
,
s_star
],
dfdm
=
theano
.
function
([
inputs
,
targets
,
x_star
,
s_star
],
...
@@ -2851,9 +2851,9 @@ class T_Scan(unittest.TestCase):
...
@@ -2851,9 +2851,9 @@ class T_Scan(unittest.TestCase):
x
=
theano
.
tensor
.
fmatrix
(
'x'
)
x
=
theano
.
tensor
.
fmatrix
(
'x'
)
mem_val
=
n
umpy
.
zeros
((
2
,),
dtype
=
'float32'
)
mem_val
=
n
p
.
zeros
((
2
,),
dtype
=
'float32'
)
memory
=
theano
.
shared
(
mem_val
)
memory
=
theano
.
shared
(
mem_val
)
W
=
theano
.
shared
(
n
umpy
.
random
.
random
((
5
,
2
))
.
astype
(
'float32'
))
W
=
theano
.
shared
(
n
p
.
random
.
random
((
5
,
2
))
.
astype
(
'float32'
))
def
f
(
inp
,
mem
):
def
f
(
inp
,
mem
):
i
=
theano
.
tensor
.
join
(
0
,
inp
,
mem
)
i
=
theano
.
tensor
.
join
(
0
,
inp
,
mem
)
...
@@ -2867,7 +2867,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2867,7 +2867,7 @@ class T_Scan(unittest.TestCase):
f
=
theano
.
function
([
x
],
outs
[
0
])
f
=
theano
.
function
([
x
],
outs
[
0
])
f2
=
theano
.
function
([
x
],
outs
[
1
])
f2
=
theano
.
function
([
x
],
outs
[
1
])
x_val
=
n
umpy
.
random
.
random
((
4
,
3
))
.
astype
(
'float32'
)
x_val
=
n
p
.
random
.
random
((
4
,
3
))
.
astype
(
'float32'
)
f_vals
=
f
(
x_val
)
f_vals
=
f
(
x_val
)
memory
.
set_value
(
mem_val
)
memory
.
set_value
(
mem_val
)
...
@@ -2876,12 +2876,12 @@ class T_Scan(unittest.TestCase):
...
@@ -2876,12 +2876,12 @@ class T_Scan(unittest.TestCase):
def
test_reduce_memory_consumption
(
self
):
def
test_reduce_memory_consumption
(
self
):
x
=
theano
.
shared
(
n
umpy
.
asarray
(
x
=
theano
.
shared
(
n
p
.
asarray
(
n
umpy
.
random
.
uniform
(
size
=
(
10
,)),
dtype
=
theano
.
config
.
floatX
))
n
p
.
random
.
uniform
(
size
=
(
10
,)),
dtype
=
theano
.
config
.
floatX
))
o
,
_
=
theano
.
reduce
(
lambda
v
,
acc
:
acc
+
v
,
o
,
_
=
theano
.
reduce
(
lambda
v
,
acc
:
acc
+
v
,
x
,
x
,
theano
.
tensor
.
constant
(
theano
.
tensor
.
constant
(
n
umpy
.
asarray
(
0.
,
n
p
.
asarray
(
0.
,
dtype
=
theano
.
config
.
floatX
)))
dtype
=
theano
.
config
.
floatX
)))
mode
=
theano
.
compile
.
mode
.
FAST_RUN
mode
=
theano
.
compile
.
mode
.
FAST_RUN
mode
=
mode
.
excluding
(
'inplace'
)
mode
=
mode
.
excluding
(
'inplace'
)
...
@@ -2905,15 +2905,15 @@ class T_Scan(unittest.TestCase):
...
@@ -2905,15 +2905,15 @@ class T_Scan(unittest.TestCase):
gx
=
theano
.
tensor
.
grad
(
o
,
x
)
gx
=
theano
.
tensor
.
grad
(
o
,
x
)
f2
=
theano
.
function
([],
gx
)
f2
=
theano
.
function
([],
gx
)
utt
.
assert_allclose
(
f2
(),
n
umpy
.
ones
((
10
,)))
utt
.
assert_allclose
(
f2
(),
n
p
.
ones
((
10
,)))
def
test_foldl_memory_consumption
(
self
):
def
test_foldl_memory_consumption
(
self
):
x
=
theano
.
shared
(
n
umpy
.
asarray
(
x
=
theano
.
shared
(
n
p
.
asarray
(
n
umpy
.
random
.
uniform
(
size
=
(
10
,)),
dtype
=
theano
.
config
.
floatX
))
n
p
.
random
.
uniform
(
size
=
(
10
,)),
dtype
=
theano
.
config
.
floatX
))
o
,
_
=
theano
.
foldl
(
lambda
v
,
acc
:
acc
+
v
,
o
,
_
=
theano
.
foldl
(
lambda
v
,
acc
:
acc
+
v
,
x
,
x
,
theano
.
tensor
.
constant
(
theano
.
tensor
.
constant
(
n
umpy
.
asarray
(
0.
,
n
p
.
asarray
(
0.
,
dtype
=
theano
.
config
.
floatX
)))
dtype
=
theano
.
config
.
floatX
)))
mode
=
theano
.
compile
.
mode
.
FAST_RUN
mode
=
theano
.
compile
.
mode
.
FAST_RUN
...
@@ -2938,16 +2938,16 @@ class T_Scan(unittest.TestCase):
...
@@ -2938,16 +2938,16 @@ class T_Scan(unittest.TestCase):
gx
=
theano
.
tensor
.
grad
(
o
,
x
)
gx
=
theano
.
tensor
.
grad
(
o
,
x
)
f2
=
theano
.
function
([],
gx
)
f2
=
theano
.
function
([],
gx
)
utt
.
assert_allclose
(
f2
(),
n
umpy
.
ones
((
10
,)))
utt
.
assert_allclose
(
f2
(),
n
p
.
ones
((
10
,)))
def
test_foldr_memory_consumption
(
self
):
def
test_foldr_memory_consumption
(
self
):
x
=
theano
.
shared
(
n
umpy
.
asarray
(
x
=
theano
.
shared
(
n
p
.
asarray
(
n
umpy
.
random
.
uniform
(
size
=
(
10
,)),
dtype
=
theano
.
config
.
floatX
))
n
p
.
random
.
uniform
(
size
=
(
10
,)),
dtype
=
theano
.
config
.
floatX
))
o
,
_
=
theano
.
foldr
(
lambda
v
,
acc
:
acc
+
v
,
o
,
_
=
theano
.
foldr
(
lambda
v
,
acc
:
acc
+
v
,
x
,
x
,
theano
.
tensor
.
constant
(
theano
.
tensor
.
constant
(
n
umpy
.
asarray
(
0.
,
n
p
.
asarray
(
0.
,
dtype
=
theano
.
config
.
floatX
)))
dtype
=
theano
.
config
.
floatX
)))
mode
=
theano
.
compile
.
mode
.
FAST_RUN
mode
=
theano
.
compile
.
mode
.
FAST_RUN
...
@@ -2972,26 +2972,26 @@ class T_Scan(unittest.TestCase):
...
@@ -2972,26 +2972,26 @@ class T_Scan(unittest.TestCase):
gx
=
theano
.
tensor
.
grad
(
o
,
x
)
gx
=
theano
.
tensor
.
grad
(
o
,
x
)
f2
=
theano
.
function
([],
gx
)
f2
=
theano
.
function
([],
gx
)
utt
.
assert_allclose
(
f2
(),
n
umpy
.
ones
((
10
,)))
utt
.
assert_allclose
(
f2
(),
n
p
.
ones
((
10
,)))
@attr
(
'slow'
)
@attr
(
'slow'
)
def
test_rop2
(
self
):
def
test_rop2
(
self
):
seed
=
utt
.
fetch_seed
()
seed
=
utt
.
fetch_seed
()
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
v_u
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
3
,
5
))
-
.
5
,
dtype
=
floatX
)
v_u
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
3
,
5
))
-
.
5
,
dtype
=
floatX
)
v_W
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
))
-
.
5
,
dtype
=
floatX
)
v_W
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
))
-
.
5
,
dtype
=
floatX
)
v_h0
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,))
-
.
5
,
dtype
=
floatX
)
v_h0
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,))
-
.
5
,
dtype
=
floatX
)
v_eu
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
3
,
5
))
-
.
5
,
dtype
=
floatX
)
v_eu
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
3
,
5
))
-
.
5
,
dtype
=
floatX
)
v_eW
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
))
-
.
5
,
dtype
=
floatX
)
v_eW
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
))
-
.
5
,
dtype
=
floatX
)
v_eh0
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,))
-
.
5
,
dtype
=
floatX
)
v_eh0
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,))
-
.
5
,
dtype
=
floatX
)
def
rnn_fn
(
_u
,
_y
,
_W
):
def
rnn_fn
(
_u
,
_y
,
_W
):
srng
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
seed
)
srng
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
seed
)
tmp_val
=
_u
+
_y
+
srng
.
uniform
(
size
=
v_h0
.
shape
)
*
\
tmp_val
=
_u
+
_y
+
srng
.
uniform
(
size
=
v_h0
.
shape
)
*
\
n
umpy
.
asarray
(
1e-6
,
dtype
=
floatX
)
n
p
.
asarray
(
1e-6
,
dtype
=
floatX
)
sl_o
=
theano
.
tensor
.
tanh
(
theano
.
tensor
.
dot
(
_W
,
tmp_val
))
sl_o
=
theano
.
tensor
.
tanh
(
theano
.
tensor
.
dot
(
_W
,
tmp_val
))
return
sl_o
,
tmp_val
return
sl_o
,
tmp_val
...
@@ -3053,15 +3053,15 @@ class T_Scan(unittest.TestCase):
...
@@ -3053,15 +3053,15 @@ class T_Scan(unittest.TestCase):
def
test_rop
(
self
):
def
test_rop
(
self
):
seed
=
utt
.
fetch_seed
()
seed
=
utt
.
fetch_seed
()
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
v_u
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
20
,
5
)),
dtype
=
floatX
)
v_u
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
20
,
5
)),
dtype
=
floatX
)
v_W
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
)),
dtype
=
floatX
)
v_W
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
)),
dtype
=
floatX
)
v_h0
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,)),
dtype
=
floatX
)
v_h0
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,)),
dtype
=
floatX
)
v_eu
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
20
,
5
)),
dtype
=
floatX
)
v_eu
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
20
,
5
)),
dtype
=
floatX
)
v_eW
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
)),
dtype
=
floatX
)
v_eW
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
)),
dtype
=
floatX
)
v_eh0
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,)),
dtype
=
floatX
)
v_eh0
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,)),
dtype
=
floatX
)
def
rnn_fn
(
_u
,
_y
,
_W
):
def
rnn_fn
(
_u
,
_y
,
_W
):
sl_o
=
theano
.
tensor
.
tanh
(
theano
.
tensor
.
dot
(
_W
,
(
_u
+
_y
)))
sl_o
=
theano
.
tensor
.
tanh
(
theano
.
tensor
.
dot
(
_W
,
(
_u
+
_y
)))
...
@@ -3163,14 +3163,14 @@ class T_Scan(unittest.TestCase):
...
@@ -3163,14 +3163,14 @@ class T_Scan(unittest.TestCase):
assert
len
(
scan_nodes
)
==
0
assert
len
(
scan_nodes
)
==
0
seed
=
utt
.
fetch_seed
()
seed
=
utt
.
fetch_seed
()
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
v_h
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
floatX
)
v_h
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
2
,)),
dtype
=
floatX
)
v_W1
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
2
,
2
)),
dtype
=
floatX
)
v_W1
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
2
,
2
)),
dtype
=
floatX
)
v_W2
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
2
,
2
)),
dtype
=
floatX
)
v_W2
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
2
,
2
)),
dtype
=
floatX
)
v_out
=
n
umpy
.
dot
(
v_h
,
v_W1
+
v_W2
)
v_out
=
n
p
.
dot
(
v_h
,
v_W1
+
v_W2
)
sol
=
n
umpy
.
zeros
((
5
,
2
))
sol
=
n
p
.
zeros
((
5
,
2
))
# This line is here to make sol have the same shape as the output of
# This line is here to make sol have the same shape as the output of
# theano. Note that what we ask theano to do is to repeat the 2
# theano. Note that what we ask theano to do is to repeat the 2
# elements vector v_out 5 times
# elements vector v_out 5 times
...
@@ -3206,9 +3206,9 @@ class T_Scan(unittest.TestCase):
...
@@ -3206,9 +3206,9 @@ class T_Scan(unittest.TestCase):
f_ref
=
theano
.
function
([
W1
,
W2
,
step_indices
],
o
,
mode
=
'FAST_COMPILE'
)
f_ref
=
theano
.
function
([
W1
,
W2
,
step_indices
],
o
,
mode
=
'FAST_COMPILE'
)
# Compare the results of the two implementations
# Compare the results of the two implementations
input_values
=
[
n
umpy
.
random
.
random
((
5
,
5
))
.
astype
(
"float32"
),
input_values
=
[
n
p
.
random
.
random
((
5
,
5
))
.
astype
(
"float32"
),
n
umpy
.
random
.
random
((
5
,
5
))
.
astype
(
"float32"
),
n
p
.
random
.
random
((
5
,
5
))
.
astype
(
"float32"
),
n
umpy
.
arange
(
5
)
.
astype
(
"float32"
)]
n
p
.
arange
(
5
)
.
astype
(
"float32"
)]
out
=
f
(
*
input_values
)
out
=
f
(
*
input_values
)
out_ref
=
f_ref
(
*
input_values
)
out_ref
=
f_ref
(
*
input_values
)
...
@@ -3243,10 +3243,10 @@ class T_Scan(unittest.TestCase):
...
@@ -3243,10 +3243,10 @@ class T_Scan(unittest.TestCase):
([
i_t
,
i_tm1
],
_
)
=
theano
.
scan
(
([
i_t
,
i_tm1
],
_
)
=
theano
.
scan
(
fn
,
sequences
=
[
inp
],
fn
,
sequences
=
[
inp
],
outputs_info
=
[
n
umpy
.
asarray
([
0.0
,
0.0
],
theano
.
config
.
floatX
),
outputs_info
=
[
n
p
.
asarray
([
0.0
,
0.0
],
theano
.
config
.
floatX
),
None
])
None
])
f
=
theano
.
function
([
inp
],
[
i_t
,
i_tm1
])
f
=
theano
.
function
([
inp
],
[
i_t
,
i_tm1
])
val
=
n
umpy
.
arange
(
10
)
.
reshape
(
5
,
2
)
.
astype
(
theano
.
config
.
floatX
)
val
=
n
p
.
arange
(
10
)
.
reshape
(
5
,
2
)
.
astype
(
theano
.
config
.
floatX
)
ret
=
f
(
val
)
ret
=
f
(
val
)
utt
.
assert_allclose
(
ret
[
0
],
val
+
10
)
utt
.
assert_allclose
(
ret
[
0
],
val
+
10
)
utt
.
assert_allclose
(
ret
[
1
],
[[
0.
,
0.
],
utt
.
assert_allclose
(
ret
[
1
],
[[
0.
,
0.
],
...
@@ -3330,7 +3330,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3330,7 +3330,7 @@ class T_Scan(unittest.TestCase):
return
x_t
+
1
,
theano
.
scan_module
.
until
(
x_t
>
3
)
return
x_t
+
1
,
theano
.
scan_module
.
until
(
x_t
>
3
)
o
,
_
=
theano
.
scan
(
lambda_fn
,
x
)
o
,
_
=
theano
.
scan
(
lambda_fn
,
x
)
f
=
theano
.
function
([
x
],
o
)
f
=
theano
.
function
([
x
],
o
)
vx
=
n
umpy
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
[
23
]
=
4
vx
[
23
]
=
4
out
=
f
(
vx
)
out
=
f
(
vx
)
assert
len
(
out
)
==
24
assert
len
(
out
)
==
24
...
@@ -3344,11 +3344,11 @@ class T_Scan(unittest.TestCase):
...
@@ -3344,11 +3344,11 @@ class T_Scan(unittest.TestCase):
o2
,
_
=
theano
.
scan
(
lambda
x_t
:
x_t
+
2
,
x
)
o2
,
_
=
theano
.
scan
(
lambda
x_t
:
x_t
+
2
,
x
)
f
=
theano
.
function
([
x
],
[
o
,
o2
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
],
[
o
,
o2
],
mode
=
mode_with_opt
)
vx
=
n
umpy
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
[
23
]
=
4
vx
[
23
]
=
4
out
,
out2
=
f
(
vx
)
out
,
out2
=
f
(
vx
)
assert
len
(
out
)
==
24
assert
len
(
out
)
==
24
assert
n
umpy
.
all
(
out2
==
vx
+
2
)
assert
n
p
.
all
(
out2
==
vx
+
2
)
lssc
=
[
x
for
x
in
f
.
maker
.
fgraph
.
toposort
()
lssc
=
[
x
for
x
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
x
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)]
if
isinstance
(
x
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)]
# One scan node gets optimnized out
# One scan node gets optimnized out
...
@@ -3402,7 +3402,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3402,7 +3402,7 @@ class T_Scan(unittest.TestCase):
polynomial3
[
-
1
],
polynomial3
[
-
1
],
polynomial4
[
-
1
]])
polynomial4
[
-
1
]])
test_coeff
=
n
umpy
.
asarray
([
1
,
0
,
2
],
dtype
=
theano
.
config
.
floatX
)
test_coeff
=
n
p
.
asarray
([
1
,
0
,
2
],
dtype
=
theano
.
config
.
floatX
)
# This will be tested by DEBUG_MODE
# This will be tested by DEBUG_MODE
out
=
calculate_polynomial
(
test_coeff
,
3
)
out
=
calculate_polynomial
(
test_coeff
,
3
)
assert
out
[
0
]
==
19
assert
out
[
0
]
==
19
...
@@ -3480,7 +3480,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3480,7 +3480,7 @@ class T_Scan(unittest.TestCase):
x
)
x
)
f
=
theano
.
function
([
x
],
[
o
,
o2
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
],
[
o
,
o2
],
mode
=
mode_with_opt
)
vx
=
n
umpy
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
[
23
]
=
4
vx
[
23
]
=
4
out
,
out2
=
f
(
vx
)
out
,
out2
=
f
(
vx
)
assert
len
(
out
)
==
24
assert
len
(
out
)
==
24
...
@@ -3497,7 +3497,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3497,7 +3497,7 @@ class T_Scan(unittest.TestCase):
o
,
_
=
theano
.
scan
(
lambda_fn
,
x
)
o
,
_
=
theano
.
scan
(
lambda_fn
,
x
)
f
=
theano
.
function
([
x
],
o
.
shape
[
0
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
],
o
.
shape
[
0
],
mode
=
mode_with_opt
)
vx
=
n
umpy
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
[
23
]
=
4
vx
[
23
]
=
4
out
=
f
(
vx
)
out
=
f
(
vx
)
assert
out
==
24
assert
out
==
24
...
@@ -3516,7 +3516,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3516,7 +3516,7 @@ class T_Scan(unittest.TestCase):
[
o1
.
shape
[
0
],
o2
.
shape
[
0
]],
[
o1
.
shape
[
0
],
o2
.
shape
[
0
]],
mode
=
mode_with_opt
)
mode
=
mode_with_opt
)
vx
=
n
umpy
.
ones
((
10
,),
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
ones
((
10
,),
dtype
=
theano
.
config
.
floatX
)
out1
,
out2
=
f
(
vx
)
out1
,
out2
=
f
(
vx
)
assert
out1
==
10
assert
out1
==
10
assert
out2
==
10
assert
out2
==
10
...
@@ -3535,7 +3535,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3535,7 +3535,7 @@ class T_Scan(unittest.TestCase):
[
o1
.
shape
[
0
],
o2
.
shape
[
0
]],
[
o1
.
shape
[
0
],
o2
.
shape
[
0
]],
mode
=
mode_with_opt
)
mode
=
mode_with_opt
)
vx
=
n
umpy
.
ones
((
30
,),
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
ones
((
30
,),
dtype
=
theano
.
config
.
floatX
)
o1
,
o2
=
f
(
vx
)
o1
,
o2
=
f
(
vx
)
assert
o1
==
20
assert
o1
==
20
assert
o2
==
20
assert
o2
==
20
...
@@ -3635,13 +3635,13 @@ class T_Scan(unittest.TestCase):
...
@@ -3635,13 +3635,13 @@ class T_Scan(unittest.TestCase):
# Run the function and validate the outputs
# Run the function and validate the outputs
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
seq_value
=
n
umpy
.
random
.
random
((
10
,
3
))
.
astype
(
dtype
)
seq_value
=
n
p
.
random
.
random
((
10
,
3
))
.
astype
(
dtype
)
out_init_value
=
n
umpy
.
random
.
random
((
3
,
3
))
.
astype
(
dtype
)
out_init_value
=
n
p
.
random
.
random
((
3
,
3
))
.
astype
(
dtype
)
non_seq_value
=
n
umpy
.
random
.
random
((
3
))
.
astype
(
dtype
)
non_seq_value
=
n
p
.
random
.
random
((
3
))
.
astype
(
dtype
)
outputs
=
fct
(
seq_value
,
out_init_value
,
non_seq_value
)
outputs
=
fct
(
seq_value
,
out_init_value
,
non_seq_value
)
expected_g_seq
=
n
umpy
.
array
([[
4
,
4
,
4
],
expected_g_seq
=
n
p
.
array
([[
4
,
4
,
4
],
[
3
,
3
,
3
],
[
3
,
3
,
3
],
[
3
,
3
,
3
],
[
3
,
3
,
3
],
[
3
,
3
,
3
],
[
3
,
3
,
3
],
...
@@ -3652,7 +3652,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3652,7 +3652,7 @@ class T_Scan(unittest.TestCase):
[
1
,
1
,
1
],
[
1
,
1
,
1
],
[
1
,
1
,
1
]])
[
1
,
1
,
1
]])
expected_g_out_init
=
expected_g_seq
[:
3
]
expected_g_out_init
=
expected_g_seq
[:
3
]
expected_g_non_seq
=
n
umpy
.
array
([
22
,
22
,
22
])
expected_g_non_seq
=
n
p
.
array
([
22
,
22
,
22
])
utt
.
assert_allclose
(
outputs
[
0
],
expected_g_seq
)
utt
.
assert_allclose
(
outputs
[
0
],
expected_g_seq
)
utt
.
assert_allclose
(
outputs
[
1
],
expected_g_out_init
)
utt
.
assert_allclose
(
outputs
[
1
],
expected_g_out_init
)
...
@@ -3729,7 +3729,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3729,7 +3729,7 @@ class T_Scan(unittest.TestCase):
assert
tf
([
1.0
,
2.0
,
-
3.0
,
4.0
],
2.0
)
==
42
assert
tf
([
1.0
,
2.0
,
-
3.0
,
4.0
],
2.0
)
==
42
def
test_return_steps
(
self
):
def
test_return_steps
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW_in2
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vW
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
vWout
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,),
low
=-
5.
,
high
=
5.
))
...
@@ -3774,16 +3774,16 @@ class T_Scan(unittest.TestCase):
...
@@ -3774,16 +3774,16 @@ class T_Scan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
# compute the values in numpy
# compute the values in numpy
v_x
=
n
umpy
.
zeros
((
8
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_x
=
n
p
.
zeros
((
8
,
2
),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
umpy
.
zeros
((
8
,),
dtype
=
theano
.
config
.
floatX
)
v_y
=
n
p
.
zeros
((
8
,),
dtype
=
theano
.
config
.
floatX
)
v_x
[
0
]
=
n
umpy
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
\
v_x
[
0
]
=
n
p
.
dot
(
v_u1
[
0
],
vW_in1
)
+
v_u2
[
0
]
*
vW_in2
+
\
n
umpy
.
dot
(
v_x0
,
vW
)
n
p
.
dot
(
v_x0
,
vW
)
v_y
[
0
]
=
n
umpy
.
dot
(
v_x0
,
vWout
)
+
v_y0
[
2
]
v_y
[
0
]
=
n
p
.
dot
(
v_x0
,
vWout
)
+
v_y0
[
2
]
for
i
in
xrange
(
1
,
8
):
for
i
in
xrange
(
1
,
8
):
v_x
[
i
]
=
n
umpy
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
\
v_x
[
i
]
=
n
p
.
dot
(
v_u1
[
i
],
vW_in1
)
+
v_u2
[
i
]
*
vW_in2
+
\
n
umpy
.
dot
(
v_x
[
i
-
1
],
vW
)
n
p
.
dot
(
v_x
[
i
-
1
],
vW
)
v_y
[
i
]
=
n
umpy
.
dot
(
v_x
[
i
-
1
],
vWout
)
+
v_y
[
i
-
1
]
v_y
[
i
]
=
n
p
.
dot
(
v_x
[
i
-
1
],
vWout
)
+
v_y
[
i
-
1
]
(
theano_dump
,
theano_x
,
theano_y
)
=
f4
(
v_u1
,
v_u2
,
v_x0
,
v_y0
,
vW_in1
)
(
theano_dump
,
theano_x
,
theano_y
)
=
f4
(
v_u1
,
v_u2
,
v_x0
,
v_y0
,
vW_in1
)
...
@@ -3811,14 +3811,14 @@ class T_Scan(unittest.TestCase):
...
@@ -3811,14 +3811,14 @@ class T_Scan(unittest.TestCase):
assert
any
([
isinstance
(
node
.
op
,
tensor
.
blas
.
Dot22
)
assert
any
([
isinstance
(
node
.
op
,
tensor
.
blas
.
Dot22
)
for
node
in
topo
])
for
node
in
topo
])
vx
=
n
umpy
.
array
([[
1.
,
1.
],
[
2.
,
2.
]],
dtype
=
theano
.
config
.
floatX
)
vx
=
n
p
.
array
([[
1.
,
1.
],
[
2.
,
2.
]],
dtype
=
theano
.
config
.
floatX
)
vA
=
n
umpy
.
array
([[
1.
,
1.
],
[
1.
,
0.
]],
dtype
=
theano
.
config
.
floatX
)
vA
=
n
p
.
array
([[
1.
,
1.
],
[
1.
,
0.
]],
dtype
=
theano
.
config
.
floatX
)
vR
=
n
umpy
.
array
([[[
2
,
1
],
[
4
,
2
]],
[[
2
,
1
],
[
4
,
2
]]],
vR
=
n
p
.
array
([[[
2
,
1
],
[
4
,
2
]],
[[
2
,
1
],
[
4
,
2
]]],
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
utt
.
assert_allclose
(
f
(
vx
,
vA
),
vR
)
utt
.
assert_allclose
(
f
(
vx
,
vA
),
vR
)
def
test_savemem_opt
(
self
):
def
test_savemem_opt
(
self
):
y0
=
theano
.
shared
(
n
umpy
.
ones
((
2
,
10
)))
y0
=
theano
.
shared
(
n
p
.
ones
((
2
,
10
)))
[
y1
,
y2
],
updates
=
theano
.
scan
(
lambda
y
:
[
y
,
y
],
[
y1
,
y2
],
updates
=
theano
.
scan
(
lambda
y
:
[
y
,
y
],
outputs_info
=
[
dict
(
initial
=
y0
,
outputs_info
=
[
dict
(
initial
=
y0
,
taps
=
[
-
2
]),
None
],
taps
=
[
-
2
]),
None
],
...
@@ -3860,9 +3860,9 @@ class T_Scan(unittest.TestCase):
...
@@ -3860,9 +3860,9 @@ class T_Scan(unittest.TestCase):
f
=
theano
.
function
(
inputs
=
[
x
,
w
],
outputs
=
get_outputs
(
x
,
w
))
f
=
theano
.
function
(
inputs
=
[
x
,
w
],
outputs
=
get_outputs
(
x
,
w
))
# Test the function to ensure it returns valid results
# Test the function to ensure it returns valid results
x_value
=
n
umpy
.
random
.
random
((
2
,
2
,
3
))
.
astype
(
theano
.
config
.
floatX
)
x_value
=
n
p
.
random
.
random
((
2
,
2
,
3
))
.
astype
(
theano
.
config
.
floatX
)
w_value
=
n
umpy
.
random
.
random
((
3
,
3
))
.
astype
(
theano
.
config
.
floatX
)
w_value
=
n
p
.
random
.
random
((
3
,
3
))
.
astype
(
theano
.
config
.
floatX
)
expected_output
=
n
umpy
.
tile
(
x_value
[:,
0
]
.
sum
(
0
),
(
3
,
1
))
.
transpose
()
expected_output
=
n
p
.
tile
(
x_value
[:,
0
]
.
sum
(
0
),
(
3
,
1
))
.
transpose
()
output
=
f
(
x_value
,
w_value
)
output
=
f
(
x_value
,
w_value
)
utt
.
assert_allclose
(
output
,
expected_output
)
utt
.
assert_allclose
(
output
,
expected_output
)
...
@@ -3891,17 +3891,17 @@ class T_Scan(unittest.TestCase):
...
@@ -3891,17 +3891,17 @@ class T_Scan(unittest.TestCase):
gw
,
gx
=
tensor
.
grad
(
loss
,
[
w
,
xinit
])
gw
,
gx
=
tensor
.
grad
(
loss
,
[
w
,
xinit
])
grad_fn
=
theano
.
function
([
xinit
,
w
],
[
gx
,
gw
],
grad_fn
=
theano
.
function
([
xinit
,
w
],
[
gx
,
gw
],
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# If numbers are small, the gradients with respect to x are small
# If numbers are small, the gradients with respect to x are small
# and the numeric differentiation becomes unstable.
# and the numeric differentiation becomes unstable.
# To fix this issue I ensure we are sampling numbers larger in
# To fix this issue I ensure we are sampling numbers larger in
# absolute value than 1.
# absolute value than 1.
v_x
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
5
,
2
,
2
),
low
=
1.
,
high
=
3.
),
v_x
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
5
,
2
,
2
),
low
=
1.
,
high
=
3.
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
# Making some entries to be negative.
# Making some entries to be negative.
pos
=
rng
.
uniform
(
size
=
(
5
,
2
,
2
),
low
=
0.
,
high
=
1
)
<
.
5
pos
=
rng
.
uniform
(
size
=
(
5
,
2
,
2
),
low
=
0.
,
high
=
1
)
<
.
5
v_x
[
pos
]
=
-
1
*
v_x
[
pos
]
v_x
[
pos
]
=
-
1
*
v_x
[
pos
]
v_w
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=
1.
,
high
=
3.
),
v_w
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
2
,
2
),
low
=
1.
,
high
=
3.
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
pos
=
rng
.
uniform
(
size
=
(
2
,
2
),
low
=
0.
,
high
=
1.
)
<
.
5
pos
=
rng
.
uniform
(
size
=
(
2
,
2
),
low
=
0.
,
high
=
1.
)
<
.
5
v_w
[
pos
]
=
-
1
*
v_w
[
pos
]
v_w
[
pos
]
=
-
1
*
v_w
[
pos
]
...
@@ -3916,11 +3916,11 @@ class T_Scan(unittest.TestCase):
...
@@ -3916,11 +3916,11 @@ class T_Scan(unittest.TestCase):
num_grad
.
gx
[
max_err_pos
]))
num_grad
.
gx
[
max_err_pos
]))
def
test_grad_numeric_shared
(
self
):
def
test_grad_numeric_shared
(
self
):
shared_var
=
theano
.
shared
(
n
umpy
.
float32
(
1.
))
shared_var
=
theano
.
shared
(
n
p
.
float32
(
1.
))
def
inner_fn
():
def
inner_fn
():
return
[],
OrderedDict
(
return
[],
OrderedDict
(
[(
shared_var
,
shared_var
+
n
umpy
.
float32
(
1.
))])
[(
shared_var
,
shared_var
+
n
p
.
float32
(
1.
))])
_
,
updates
=
theano
.
scan
(
inner_fn
,
_
,
updates
=
theano
.
scan
(
inner_fn
,
n_steps
=
10
,
n_steps
=
10
,
truncate_gradient
=-
1
,
truncate_gradient
=-
1
,
...
@@ -3940,7 +3940,7 @@ class T_Scan(unittest.TestCase):
...
@@ -3940,7 +3940,7 @@ class T_Scan(unittest.TestCase):
n_pars
=
1
*
3
+
3
*
3
n_pars
=
1
*
3
+
3
*
3
# Allocate big parameter array.
# Allocate big parameter array.
pars
=
theano
.
shared
(
n
umpy
.
empty
(
n_pars
))
pars
=
theano
.
shared
(
n
p
.
empty
(
n_pars
))
# Assign slices.
# Assign slices.
W1
=
pars
[:
3
]
.
reshape
(
W1shape
)
W1
=
pars
[:
3
]
.
reshape
(
W1shape
)
...
@@ -3983,15 +3983,15 @@ class T_Scan(unittest.TestCase):
...
@@ -3983,15 +3983,15 @@ class T_Scan(unittest.TestCase):
Hp
=
tensor
.
Rop
(
d_cost_wrt_pars
,
pars
,
p
)
Hp
=
tensor
.
Rop
(
d_cost_wrt_pars
,
pars
,
p
)
def
test_seq_tap_bug_jeremiah
(
self
):
def
test_seq_tap_bug_jeremiah
(
self
):
inp
=
n
umpy
.
arange
(
10
)
.
reshape
(
-
1
,
1
)
.
astype
(
theano
.
config
.
floatX
)
inp
=
n
p
.
arange
(
10
)
.
reshape
(
-
1
,
1
)
.
astype
(
theano
.
config
.
floatX
)
exp_out
=
n
umpy
.
zeros
((
10
,
1
))
.
astype
(
theano
.
config
.
floatX
)
exp_out
=
n
p
.
zeros
((
10
,
1
))
.
astype
(
theano
.
config
.
floatX
)
exp_out
[
4
:]
=
inp
[:
-
4
]
exp_out
[
4
:]
=
inp
[:
-
4
]
def
onestep
(
x
,
x_tm4
):
def
onestep
(
x
,
x_tm4
):
return
x
,
x_tm4
return
x
,
x_tm4
seq
=
tensor
.
matrix
()
seq
=
tensor
.
matrix
()
initial_value
=
theano
.
shared
(
n
umpy
.
zeros
((
4
,
1
),
initial_value
=
theano
.
shared
(
n
p
.
zeros
((
4
,
1
),
dtype
=
theano
.
config
.
floatX
))
dtype
=
theano
.
config
.
floatX
))
outputs_info
=
[
OrderedDict
(
outputs_info
=
[
OrderedDict
(
[(
'initial'
,
initial_value
),
(
'taps'
,
[
-
4
])]),
None
]
[(
'initial'
,
initial_value
),
(
'taps'
,
[
-
4
])]),
None
]
...
@@ -4000,7 +4000,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4000,7 +4000,7 @@ class T_Scan(unittest.TestCase):
outputs_info
=
outputs_info
)
outputs_info
=
outputs_info
)
f
=
theano
.
function
([
seq
],
results
[
1
])
f
=
theano
.
function
([
seq
],
results
[
1
])
assert
n
umpy
.
all
(
exp_out
==
f
(
inp
))
assert
n
p
.
all
(
exp_out
==
f
(
inp
))
def
test_borrow_bug_jeremiah
(
self
):
def
test_borrow_bug_jeremiah
(
self
):
# This tests two things. The first is a bug occuring when scan wrongly
# This tests two things. The first is a bug occuring when scan wrongly
...
@@ -4008,29 +4008,29 @@ class T_Scan(unittest.TestCase):
...
@@ -4008,29 +4008,29 @@ class T_Scan(unittest.TestCase):
# method will be able to remove the Scan node from the graph in this
# method will be able to remove the Scan node from the graph in this
# case.
# case.
inp
=
n
umpy
.
arange
(
10
)
.
reshape
(
-
1
,
1
)
.
astype
(
theano
.
config
.
floatX
)
inp
=
n
p
.
arange
(
10
)
.
reshape
(
-
1
,
1
)
.
astype
(
theano
.
config
.
floatX
)
exp_out
=
n
umpy
.
zeros
((
10
,
1
))
.
astype
(
theano
.
config
.
floatX
)
exp_out
=
n
p
.
zeros
((
10
,
1
))
.
astype
(
theano
.
config
.
floatX
)
exp_out
[
4
:]
=
inp
[:
-
4
]
exp_out
[
4
:]
=
inp
[:
-
4
]
def
onestep
(
x
,
x_tm4
):
def
onestep
(
x
,
x_tm4
):
return
x
,
x_tm4
return
x
,
x_tm4
seq
=
tensor
.
matrix
()
seq
=
tensor
.
matrix
()
initial_value
=
theano
.
shared
(
n
umpy
.
zeros
((
4
,
1
),
initial_value
=
theano
.
shared
(
n
p
.
zeros
((
4
,
1
),
dtype
=
theano
.
config
.
floatX
))
dtype
=
theano
.
config
.
floatX
))
outputs_info
=
[
OrderedDict
([(
'initial'
,
initial_value
),
outputs_info
=
[
OrderedDict
([(
'initial'
,
initial_value
),
(
'taps'
,
[
-
4
])]),
None
]
(
'taps'
,
[
-
4
])]),
None
]
results
,
_
=
theano
.
scan
(
fn
=
onestep
,
results
,
_
=
theano
.
scan
(
fn
=
onestep
,
sequences
=
seq
,
sequences
=
seq
,
outputs_info
=
outputs_info
)
outputs_info
=
outputs_info
)
sharedvar
=
theano
.
shared
(
n
umpy
.
zeros
((
1
,
1
),
sharedvar
=
theano
.
shared
(
n
p
.
zeros
((
1
,
1
),
dtype
=
theano
.
config
.
floatX
))
dtype
=
theano
.
config
.
floatX
))
updates
=
OrderedDict
([(
sharedvar
,
results
[
0
][
-
1
:])])
updates
=
OrderedDict
([(
sharedvar
,
results
[
0
][
-
1
:])])
f
=
theano
.
function
([
seq
],
results
[
1
],
updates
=
updates
)
f
=
theano
.
function
([
seq
],
results
[
1
],
updates
=
updates
)
# This fails if scan uses wrongly the borrow flag
# This fails if scan uses wrongly the borrow flag
assert
n
umpy
.
all
(
exp_out
==
f
(
inp
))
assert
n
p
.
all
(
exp_out
==
f
(
inp
))
# This fails if Scan's infer_shape() is unable to remove the Scan
# This fails if Scan's infer_shape() is unable to remove the Scan
# node from the graph.
# node from the graph.
...
@@ -4070,9 +4070,9 @@ class T_Scan(unittest.TestCase):
...
@@ -4070,9 +4070,9 @@ class T_Scan(unittest.TestCase):
# Compare obtained outputs with expected outputs
# Compare obtained outputs with expected outputs
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
outputs
=
fct
(
n
umpy
.
arange
(
9
,
dtype
=
floatX
)
.
reshape
(
3
,
3
))
outputs
=
fct
(
n
p
.
arange
(
9
,
dtype
=
floatX
)
.
reshape
(
3
,
3
))
states
=
n
umpy
.
array
([[
0
,
1
,
2
],
states
=
n
p
.
array
([[
0
,
1
,
2
],
[
3
,
4
,
5
],
[
3
,
4
,
5
],
[
6
,
7
,
8
],
[
6
,
7
,
8
],
[
9
,
12
,
15
],
[
9
,
12
,
15
],
...
@@ -4144,8 +4144,8 @@ class T_Scan(unittest.TestCase):
...
@@ -4144,8 +4144,8 @@ class T_Scan(unittest.TestCase):
f
=
theano
.
function
([
v
],
gv
)
f
=
theano
.
function
([
v
],
gv
)
# Ensure the output of the function is valid
# Ensure the output of the function is valid
output
=
f
(
n
umpy
.
random
.
random
(
5
))
output
=
f
(
n
p
.
random
.
random
(
5
))
utt
.
assert_allclose
(
output
,
n
umpy
.
ones
(
5
))
utt
.
assert_allclose
(
output
,
n
p
.
ones
(
5
))
def
test_dot_optimization
(
self
):
def
test_dot_optimization
(
self
):
A
=
tensor
.
matrix
(
'A'
)
A
=
tensor
.
matrix
(
'A'
)
...
@@ -4155,10 +4155,10 @@ class T_Scan(unittest.TestCase):
...
@@ -4155,10 +4155,10 @@ class T_Scan(unittest.TestCase):
B
.
dimshuffle
(
0
,
'x'
,
1
)],
B
.
dimshuffle
(
0
,
'x'
,
1
)],
outputs_info
=
[
tensor
.
zeros_like
(
A
)])
outputs_info
=
[
tensor
.
zeros_like
(
A
)])
f
=
theano
.
function
([
A
,
B
],
S
.
owner
.
inputs
[
0
][
-
1
])
f
=
theano
.
function
([
A
,
B
],
S
.
owner
.
inputs
[
0
][
-
1
])
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
vA
=
rng
.
uniform
(
size
=
(
5
,
5
))
.
astype
(
theano
.
config
.
floatX
)
vA
=
rng
.
uniform
(
size
=
(
5
,
5
))
.
astype
(
theano
.
config
.
floatX
)
vB
=
rng
.
uniform
(
size
=
(
5
,
5
))
.
astype
(
theano
.
config
.
floatX
)
vB
=
rng
.
uniform
(
size
=
(
5
,
5
))
.
astype
(
theano
.
config
.
floatX
)
utt
.
assert_allclose
(
f
(
vA
,
vB
),
n
umpy
.
dot
(
vA
.
T
,
vB
))
utt
.
assert_allclose
(
f
(
vA
,
vB
),
n
p
.
dot
(
vA
.
T
,
vB
))
def
test_pregreedy_optimizer
(
self
):
def
test_pregreedy_optimizer
(
self
):
W
=
tensor
.
zeros
((
5
,
4
))
W
=
tensor
.
zeros
((
5
,
4
))
...
@@ -4171,7 +4171,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4171,7 +4171,7 @@ class T_Scan(unittest.TestCase):
lambda
x
:
tensor
.
dot
(
tensor
.
dot
(
x
,
W
)
+
bh_t
,
W
.
T
)
+
bv_t
,
lambda
x
:
tensor
.
dot
(
tensor
.
dot
(
x
,
W
)
+
bh_t
,
W
.
T
)
+
bv_t
,
outputs_info
=
v
,
outputs_info
=
v
,
n_steps
=
2
)
n_steps
=
2
)
theano
.
function
([
v
],
chain
)(
n
umpy
.
zeros
((
3
,
5
),
theano
.
function
([
v
],
chain
)(
n
p
.
zeros
((
3
,
5
),
dtype
=
theano
.
config
.
floatX
))
dtype
=
theano
.
config
.
floatX
))
def
test_savemem_does_not_duplicate_number_of_scan_nodes
(
self
):
def
test_savemem_does_not_duplicate_number_of_scan_nodes
(
self
):
...
@@ -4210,7 +4210,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4210,7 +4210,7 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
mode
=
theano
.
Mode
(
linker
=
'py'
),
mode
=
theano
.
Mode
(
linker
=
'py'
),
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,)))
v_u
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,)))
outs
=
f
(
v_u
,
[
0
,
0
,
0
],
0
)
outs
=
f
(
v_u
,
[
0
,
0
,
0
],
0
)
utt
.
assert_allclose
(
outs
[
0
],
v_u
+
1
)
utt
.
assert_allclose
(
outs
[
0
],
v_u
+
1
)
...
@@ -4243,7 +4243,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4243,7 +4243,7 @@ class T_Scan(unittest.TestCase):
updates
=
updates
,
updates
=
updates
,
mode
=
theano
.
Mode
(
linker
=
'py'
),
mode
=
theano
.
Mode
(
linker
=
'py'
),
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_w
=
asarrayX
(
rng
.
uniform
())
v_w
=
asarrayX
(
rng
.
uniform
())
outs
=
f
(
v_w
,
[
0
,
0
,
0
],
0
)
outs
=
f
(
v_w
,
[
0
,
0
,
0
],
0
)
utt
.
assert_allclose
(
outs
[
0
],
v_w
+
1
)
utt
.
assert_allclose
(
outs
[
0
],
v_w
+
1
)
...
@@ -4252,7 +4252,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4252,7 +4252,7 @@ class T_Scan(unittest.TestCase):
utt
.
assert_allclose
(
sh
.
get_value
(),
v_w
+
4
)
utt
.
assert_allclose
(
sh
.
get_value
(),
v_w
+
4
)
def
test_grad_bug_disconnected_input
(
self
):
def
test_grad_bug_disconnected_input
(
self
):
W
=
theano
.
shared
(
n
umpy
.
zeros
((
3
,
3
)),
name
=
'W'
)
W
=
theano
.
shared
(
n
p
.
zeros
((
3
,
3
)),
name
=
'W'
)
v
=
theano
.
tensor
.
ivector
(
name
=
'v'
)
v
=
theano
.
tensor
.
ivector
(
name
=
'v'
)
y
,
_
=
theano
.
scan
(
lambda
i
,
W
:
W
[
i
],
sequences
=
v
,
outputs_info
=
None
,
non_sequences
=
W
)
y
,
_
=
theano
.
scan
(
lambda
i
,
W
:
W
[
i
],
sequences
=
v
,
outputs_info
=
None
,
non_sequences
=
W
)
...
@@ -4270,14 +4270,14 @@ class T_Scan(unittest.TestCase):
...
@@ -4270,14 +4270,14 @@ class T_Scan(unittest.TestCase):
# theano.printing.debugprint(out)
# theano.printing.debugprint(out)
return
theano
.
function
([],
out
)()
return
theano
.
function
([],
out
)()
x
=
theano
.
shared
(
n
umpy
.
asarray
(
0.
,
dtype
=
theano
.
config
.
floatX
))
x
=
theano
.
shared
(
n
p
.
asarray
(
0.
,
dtype
=
theano
.
config
.
floatX
))
utt
.
assert_allclose
(
test
(
x
,
tensor
.
sum
((
x
+
1
)
**
2
),
mention_y
=
False
),
utt
.
assert_allclose
(
test
(
x
,
tensor
.
sum
((
x
+
1
)
**
2
),
mention_y
=
False
),
1.21000003815
)
1.21000003815
)
utt
.
assert_allclose
(
test
(
x
,
tensor
.
sum
((
x
+
1
)
**
2
),
mention_y
=
True
),
utt
.
assert_allclose
(
test
(
x
,
tensor
.
sum
((
x
+
1
)
**
2
),
mention_y
=
True
),
1.21000003815
)
1.21000003815
)
def
test_grad_find_input
(
self
):
def
test_grad_find_input
(
self
):
w
=
theano
.
shared
(
n
umpy
.
array
(
0
,
dtype
=
'float32'
),
name
=
'w'
)
w
=
theano
.
shared
(
n
p
.
array
(
0
,
dtype
=
'float32'
),
name
=
'w'
)
init
=
tensor
.
fscalar
(
'init'
)
init
=
tensor
.
fscalar
(
'init'
)
out
,
_
=
theano
.
scan
(
out
,
_
=
theano
.
scan
(
...
@@ -4333,7 +4333,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4333,7 +4333,7 @@ class T_Scan(unittest.TestCase):
for
out
in
[
y1
,
y2
,
y3
,
y4
,
y5
,
y6
]:
for
out
in
[
y1
,
y2
,
y3
,
y4
,
y5
,
y6
]:
# This used to raise an exception
# This used to raise an exception
f
=
theano
.
function
([
W
,
v
],
out
,
mode
=
mode_with_opt
)
f
=
theano
.
function
([
W
,
v
],
out
,
mode
=
mode_with_opt
)
f
(
n
umpy
.
zeros
((
3
,
3
),
dtype
=
theano
.
config
.
floatX
),
[
1
,
2
])
f
(
n
p
.
zeros
((
3
,
3
),
dtype
=
theano
.
config
.
floatX
),
[
1
,
2
])
scan_nodes
=
scan_nodes_from_fct
(
f
)
scan_nodes
=
scan_nodes_from_fct
(
f
)
assert
len
(
scan_nodes
)
==
1
assert
len
(
scan_nodes
)
==
1
...
@@ -4375,9 +4375,9 @@ class T_Scan(unittest.TestCase):
...
@@ -4375,9 +4375,9 @@ class T_Scan(unittest.TestCase):
# This used to raise an exception
# This used to raise an exception
f
=
theano
.
function
([
W
,
v
,
vv
],
out
,
on_unused_input
=
'ignore'
,
f
=
theano
.
function
([
W
,
v
,
vv
],
out
,
on_unused_input
=
'ignore'
,
mode
=
mode_with_opt
)
mode
=
mode_with_opt
)
f
(
n
umpy
.
zeros
((
3
,
3
),
theano
.
config
.
floatX
),
f
(
n
p
.
zeros
((
3
,
3
),
theano
.
config
.
floatX
),
[
1
,
2
],
[
1
,
2
],
n
umpy
.
zeros
((
3
,
3
),
theano
.
config
.
floatX
))
n
p
.
zeros
((
3
,
3
),
theano
.
config
.
floatX
))
scan_nodes
=
scan_nodes_from_fct
(
f
)
scan_nodes
=
scan_nodes_from_fct
(
f
)
assert
len
(
scan_nodes
)
==
1
assert
len
(
scan_nodes
)
==
1
...
@@ -4413,7 +4413,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4413,7 +4413,7 @@ class T_Scan(unittest.TestCase):
result_inner
,
_
=
theano
.
scan
(
result_inner
,
_
=
theano
.
scan
(
fn
=
loss_inner
,
fn
=
loss_inner
,
outputs_info
=
tensor
.
as_tensor_variable
(
outputs_info
=
tensor
.
as_tensor_variable
(
n
umpy
.
asarray
(
0
,
dtype
=
numpy
.
float32
)),
n
p
.
asarray
(
0
,
dtype
=
np
.
float32
)),
non_sequences
=
[
W
],
non_sequences
=
[
W
],
n_steps
=
1
,
n_steps
=
1
,
)
)
...
@@ -4422,7 +4422,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4422,7 +4422,7 @@ class T_Scan(unittest.TestCase):
result_outer
,
_
=
theano
.
scan
(
result_outer
,
_
=
theano
.
scan
(
fn
=
loss_outer
,
fn
=
loss_outer
,
outputs_info
=
tensor
.
as_tensor_variable
(
outputs_info
=
tensor
.
as_tensor_variable
(
n
umpy
.
asarray
(
0
,
dtype
=
numpy
.
float32
)),
n
p
.
asarray
(
0
,
dtype
=
np
.
float32
)),
non_sequences
=
[
W
],
non_sequences
=
[
W
],
n_steps
=
n_steps
,
n_steps
=
n_steps
,
return_list
=
True
,
return_list
=
True
,
...
@@ -4432,14 +4432,14 @@ class T_Scan(unittest.TestCase):
...
@@ -4432,14 +4432,14 @@ class T_Scan(unittest.TestCase):
H
=
theano
.
gradient
.
hessian
(
cost
,
W
)
H
=
theano
.
gradient
.
hessian
(
cost
,
W
)
print
(
"."
,
file
=
sys
.
stderr
)
print
(
"."
,
file
=
sys
.
stderr
)
f
=
theano
.
function
([
W
,
n_steps
],
H
)
f
=
theano
.
function
([
W
,
n_steps
],
H
)
f
(
n
umpy
.
ones
((
8
,),
dtype
=
'float32'
),
1
)
f
(
n
p
.
ones
((
8
,),
dtype
=
'float32'
),
1
)
def
test_strict_mode
(
self
):
def
test_strict_mode
(
self
):
n
=
10
n
=
10
w
=
n
umpy
.
array
([[
-
1
,
2
],
[
3
,
-
4
]])
.
astype
(
theano
.
config
.
floatX
)
w
=
n
p
.
array
([[
-
1
,
2
],
[
3
,
-
4
]])
.
astype
(
theano
.
config
.
floatX
)
w_
=
theano
.
shared
(
w
)
w_
=
theano
.
shared
(
w
)
x0
=
n
umpy
.
array
([
1
,
2
])
.
astype
(
theano
.
config
.
floatX
)
x0
=
n
p
.
array
([
1
,
2
])
.
astype
(
theano
.
config
.
floatX
)
x0_
=
tensor
.
vector
(
name
=
'x0'
,
dtype
=
theano
.
config
.
floatX
)
x0_
=
tensor
.
vector
(
name
=
'x0'
,
dtype
=
theano
.
config
.
floatX
)
def
_scan_loose
(
x
):
def
_scan_loose
(
x
):
...
@@ -4474,9 +4474,9 @@ class T_Scan(unittest.TestCase):
...
@@ -4474,9 +4474,9 @@ class T_Scan(unittest.TestCase):
def
test_strict_mode_ex
(
self
):
def
test_strict_mode_ex
(
self
):
n
=
10
n
=
10
w
=
n
umpy
.
array
([[
-
1
,
2
],
[
3
,
-
4
]])
.
astype
(
theano
.
config
.
floatX
)
w
=
n
p
.
array
([[
-
1
,
2
],
[
3
,
-
4
]])
.
astype
(
theano
.
config
.
floatX
)
w_
=
theano
.
shared
(
w
)
w_
=
theano
.
shared
(
w
)
x0
=
n
umpy
.
array
([
1
,
2
])
.
astype
(
theano
.
config
.
floatX
)
x0
=
n
p
.
array
([
1
,
2
])
.
astype
(
theano
.
config
.
floatX
)
x0_
=
tensor
.
vector
(
name
=
'x0'
,
dtype
=
theano
.
config
.
floatX
)
x0_
=
tensor
.
vector
(
name
=
'x0'
,
dtype
=
theano
.
config
.
floatX
)
def
_scan_loose
(
x
):
def
_scan_loose
(
x
):
...
@@ -4497,7 +4497,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4497,7 +4497,7 @@ class T_Scan(unittest.TestCase):
# Build a MonitorMode that counts how many values are greater than 10
# Build a MonitorMode that counts how many values are greater than 10
def
detect_large_outputs
(
i
,
node
,
fn
):
def
detect_large_outputs
(
i
,
node
,
fn
):
for
output
in
fn
.
outputs
:
for
output
in
fn
.
outputs
:
if
isinstance
(
output
[
0
],
n
umpy
.
ndarray
):
if
isinstance
(
output
[
0
],
n
p
.
ndarray
):
detect_large_outputs
.
large_count
+=
(
output
[
0
]
>
10
)
.
sum
()
detect_large_outputs
.
large_count
+=
(
output
[
0
]
>
10
)
.
sum
()
detect_large_outputs
.
large_count
=
0
detect_large_outputs
.
large_count
=
0
...
@@ -4516,7 +4516,7 @@ class T_Scan(unittest.TestCase):
...
@@ -4516,7 +4516,7 @@ class T_Scan(unittest.TestCase):
f
=
theano
.
function
(
inputs
=
[
A
,
k
],
f
=
theano
.
function
(
inputs
=
[
A
,
k
],
outputs
=
final_result
,
outputs
=
final_result
,
updates
=
updates
)
updates
=
updates
)
f
(
n
umpy
.
asarray
([
2
,
3
,
.
1
,
0
,
1
],
dtype
=
theano
.
config
.
floatX
),
4
)
f
(
n
p
.
asarray
([
2
,
3
,
.
1
,
0
,
1
],
dtype
=
theano
.
config
.
floatX
),
4
)
# There should be 3 outputs greater than 10: prior_result[0] at step 3,
# There should be 3 outputs greater than 10: prior_result[0] at step 3,
# and prior_result[1] at steps 2 and 3.
# and prior_result[1] at steps 2 and 3.
...
@@ -4574,19 +4574,19 @@ class ScanGpuTests:
...
@@ -4574,19 +4574,19 @@ class ScanGpuTests:
mode
=
self
.
mode_with_gpu
)
mode
=
self
.
mode_with_gpu
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_x0
=
rng
.
uniform
()
v_x0
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
v_u
=
n
umpy
.
asarray
(
v_u
,
dtype
=
'float32'
)
v_u
=
n
p
.
asarray
(
v_u
,
dtype
=
'float32'
)
v_x0
=
n
umpy
.
asarray
(
v_x0
,
dtype
=
'float32'
)
v_x0
=
n
p
.
asarray
(
v_x0
,
dtype
=
'float32'
)
W
=
n
umpy
.
asarray
(
W
,
dtype
=
'float32'
)
W
=
n
p
.
asarray
(
W
,
dtype
=
'float32'
)
W_in
=
n
umpy
.
asarray
(
W_in
,
dtype
=
'float32'
)
W_in
=
n
p
.
asarray
(
W_in
,
dtype
=
'float32'
)
# compute the output in numpy
# compute the output in numpy
v_out
=
n
umpy
.
zeros
((
4
,))
v_out
=
n
p
.
zeros
((
4
,))
v_out
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
v_out
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
for
step
in
xrange
(
1
,
4
):
for
step
in
xrange
(
1
,
4
):
v_out
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
v_out
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
...
@@ -4646,14 +4646,14 @@ class ScanGpuTests:
...
@@ -4646,14 +4646,14 @@ class ScanGpuTests:
mode
=
self
.
mode_with_gpu
)
mode
=
self
.
mode_with_gpu
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_x0
=
rng
.
uniform
()
v_x0
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
# compute the output in numpy
# compute the output in numpy
v_out
=
n
umpy
.
zeros
((
4
,))
v_out
=
n
p
.
zeros
((
4
,))
v_out
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
v_out
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
for
step
in
xrange
(
1
,
4
):
for
step
in
xrange
(
1
,
4
):
v_out
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
v_out
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out
[
step
-
1
]
*
W
...
@@ -4708,20 +4708,20 @@ class ScanGpuTests:
...
@@ -4708,20 +4708,20 @@ class ScanGpuTests:
mode
=
self
.
mode_with_gpu
)
mode
=
self
.
mode_with_gpu
)
# get random initial values
# get random initial values
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_u
=
rng
.
uniform
(
size
=
(
4
,),
low
=-
5.
,
high
=
5.
)
v_x0
=
rng
.
uniform
()
v_x0
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
W_in
=
rng
.
uniform
()
# compute the output in numpy
# compute the output in numpy
v_out1
=
n
umpy
.
zeros
((
4
,))
v_out1
=
n
p
.
zeros
((
4
,))
v_out2
=
n
umpy
.
zeros
((
4
,),
dtype
=
'int64'
)
v_out2
=
n
p
.
zeros
((
4
,),
dtype
=
'int64'
)
v_out1
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
v_out1
[
0
]
=
v_u
[
0
]
*
W_in
+
v_x0
*
W
v_out2
[
0
]
=
v_u
[
0
]
+
v_x0
v_out2
[
0
]
=
v_u
[
0
]
+
v_x0
for
step
in
xrange
(
1
,
4
):
for
step
in
xrange
(
1
,
4
):
v_out1
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out1
[
step
-
1
]
*
W
v_out1
[
step
]
=
v_u
[
step
]
*
W_in
+
v_out1
[
step
-
1
]
*
W
v_out2
[
step
]
=
n
umpy
.
int64
(
v_u
[
step
]
+
v_out1
[
step
-
1
])
v_out2
[
step
]
=
n
p
.
int64
(
v_u
[
step
]
+
v_out1
[
step
-
1
])
theano_out1
,
theano_out2
=
f2
(
v_u
,
v_x0
,
W_in
,
W
)
theano_out1
,
theano_out2
=
f2
(
v_u
,
v_x0
,
W_in
,
W
)
utt
.
assert_allclose
(
theano_out1
,
v_out1
)
utt
.
assert_allclose
(
theano_out1
,
v_out1
)
...
@@ -4735,8 +4735,8 @@ class ScanGpuTests:
...
@@ -4735,8 +4735,8 @@ class ScanGpuTests:
assert
self
.
is_scan_on_gpu
(
scan_node
)
assert
self
.
is_scan_on_gpu
(
scan_node
)
def
test_gibbs_chain
(
self
):
def
test_gibbs_chain
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_vsample
=
n
umpy
.
array
(
rng
.
binomial
(
1
,
.
5
,
size
=
(
3
,
20
),),
v_vsample
=
n
p
.
array
(
rng
.
binomial
(
1
,
.
5
,
size
=
(
3
,
20
),),
dtype
=
'float32'
)
dtype
=
'float32'
)
vsample
=
theano
.
shared
(
v_vsample
)
vsample
=
theano
.
shared
(
v_vsample
)
trng
=
theano
.
sandbox
.
rng_mrg
.
MRG_RandomStreams
(
trng
=
theano
.
sandbox
.
rng_mrg
.
MRG_RandomStreams
(
...
@@ -4788,11 +4788,11 @@ class ScanGpuTests:
...
@@ -4788,11 +4788,11 @@ class ScanGpuTests:
# Initialize the network parameters
# Initialize the network parameters
floatX
=
theano
.
config
.
floatX
floatX
=
theano
.
config
.
floatX
U
=
theano
.
shared
(
n
umpy
.
zeros
((
n_in
,
n_hid
),
dtype
=
"float32"
),
U
=
theano
.
shared
(
n
p
.
zeros
((
n_in
,
n_hid
),
dtype
=
"float32"
),
name
=
'W_xin_to_l1'
)
name
=
'W_xin_to_l1'
)
V
=
theano
.
shared
(
n
umpy
.
zeros
((
n_hid
,
n_hid
),
dtype
=
"float32"
),
V
=
theano
.
shared
(
n
p
.
zeros
((
n_hid
,
n_hid
),
dtype
=
"float32"
),
name
=
'W_l1_to_l1'
)
name
=
'W_l1_to_l1'
)
W
=
theano
.
shared
(
n
umpy
.
zeros
((
n_hid
,
n_out
),
dtype
=
"float32"
),
W
=
theano
.
shared
(
n
p
.
zeros
((
n_hid
,
n_out
),
dtype
=
"float32"
),
name
=
'W_l1_to_l2'
)
name
=
'W_l1_to_l2'
)
nparams
=
[
U
,
V
,
W
]
nparams
=
[
U
,
V
,
W
]
...
@@ -4802,7 +4802,7 @@ class ScanGpuTests:
...
@@ -4802,7 +4802,7 @@ class ScanGpuTests:
def
scan_l
(
baseline
,
last_step
):
def
scan_l
(
baseline
,
last_step
):
return
baseline
+
tensor
.
dot
(
last_step
,
V
)
return
baseline
+
tensor
.
dot
(
last_step
,
V
)
zero_output
=
tensor
.
alloc
(
n
umpy
.
asarray
(
0.
,
dtype
=
"float32"
),
zero_output
=
tensor
.
alloc
(
n
p
.
asarray
(
0.
,
dtype
=
"float32"
),
mb_size
,
n_hid
)
mb_size
,
n_hid
)
l1_out
,
_
=
theano
.
scan
(
scan_l
,
sequences
=
[
l1_base
],
l1_out
,
_
=
theano
.
scan
(
scan_l
,
sequences
=
[
l1_base
],
...
@@ -4833,9 +4833,9 @@ class ScanGpuTests:
...
@@ -4833,9 +4833,9 @@ class ScanGpuTests:
assert
len
(
grad_scan_node
.
outputs
)
==
2
,
len
(
grad_scan_node
.
outputs
)
assert
len
(
grad_scan_node
.
outputs
)
==
2
,
len
(
grad_scan_node
.
outputs
)
# Call the theano function to ensure the absence of a memory error
# Call the theano function to ensure the absence of a memory error
feval_backprop
(
n
umpy
.
zeros
((
mb_length
,
mb_size
,
n_in
),
feval_backprop
(
n
p
.
zeros
((
mb_length
,
mb_size
,
n_in
),
dtype
=
"float32"
),
dtype
=
"float32"
),
n
umpy
.
zeros
((
mb_length
,
mb_size
,
n_out
),
n
p
.
zeros
((
mb_length
,
mb_size
,
n_out
),
dtype
=
"float32"
))
dtype
=
"float32"
))
def
test_memory_reuse_gpudimshuffle
(
self
):
def
test_memory_reuse_gpudimshuffle
(
self
):
...
@@ -4864,11 +4864,11 @@ class ScanGpuTests:
...
@@ -4864,11 +4864,11 @@ class ScanGpuTests:
fct
=
theano
.
function
([
input1
,
init
],
[
out1
,
out2
],
fct
=
theano
.
function
([
input1
,
init
],
[
out1
,
out2
],
mode
=
self
.
mode_with_gpu
)
mode
=
self
.
mode_with_gpu
)
output
=
fct
(
n
umpy
.
ones
((
2
,
1
,
1
),
dtype
=
"float32"
),
output
=
fct
(
n
p
.
ones
((
2
,
1
,
1
),
dtype
=
"float32"
),
n
umpy
.
ones
((
1
,
1
,
1
),
dtype
=
"float32"
))
n
p
.
ones
((
1
,
1
,
1
),
dtype
=
"float32"
))
expected_output
=
(
n
umpy
.
array
([
2
,
4
],
dtype
=
"float32"
),
expected_output
=
(
n
p
.
array
([
2
,
4
],
dtype
=
"float32"
),
n
umpy
.
array
([
3
,
7
],
dtype
=
"float32"
))
n
p
.
array
([
3
,
7
],
dtype
=
"float32"
))
utt
.
assert_allclose
(
output
,
expected_output
)
utt
.
assert_allclose
(
output
,
expected_output
)
...
@@ -4985,7 +4985,7 @@ def test_speed():
...
@@ -4985,7 +4985,7 @@ def test_speed():
if
not
theano
.
config
.
cxx
:
if
not
theano
.
config
.
cxx
:
raise
SkipTest
(
"G++ not available, so we need to skip this test."
)
raise
SkipTest
(
"G++ not available, so we need to skip this test."
)
r
=
n
umpy
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
1000
,
10
)
r
=
n
p
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
1000
,
10
)
t0
=
time
.
time
()
t0
=
time
.
time
()
for
i
in
xrange
(
1
,
1000
):
for
i
in
xrange
(
1
,
1000
):
...
@@ -4993,7 +4993,7 @@ def test_speed():
...
@@ -4993,7 +4993,7 @@ def test_speed():
t1
=
time
.
time
()
t1
=
time
.
time
()
print
(
'python'
,
t1
-
t0
)
print
(
'python'
,
t1
-
t0
)
r
=
n
umpy
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
1000
,
10
)
r
=
n
p
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
1000
,
10
)
t0
=
time
.
time
()
t0
=
time
.
time
()
r_i
=
iter
(
r
[
1
:])
r_i
=
iter
(
r
[
1
:])
r_ii
=
iter
(
r
[:
-
1
])
r_ii
=
iter
(
r
[:
-
1
])
...
@@ -5015,7 +5015,7 @@ def test_speed():
...
@@ -5015,7 +5015,7 @@ def test_speed():
print
(
'python with builtin iterator'
,
t1
-
t0
)
print
(
'python with builtin iterator'
,
t1
-
t0
)
if
1
:
if
1
:
r
=
n
umpy
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
1000
,
10
)
r
=
n
p
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
1000
,
10
)
s_r
=
tensor
.
matrix
()
s_r
=
tensor
.
matrix
()
s_y
,
updates
=
theano
.
scan
(
fn
=
lambda
ri
,
rii
:
ri
+
rii
,
s_y
,
updates
=
theano
.
scan
(
fn
=
lambda
ri
,
rii
:
ri
+
rii
,
sequences
=
[
s_r
[
1
:]],
sequences
=
[
s_r
[
1
:]],
...
@@ -5030,9 +5030,9 @@ def test_speed():
...
@@ -5030,9 +5030,9 @@ def test_speed():
print
(
'theano (scan, cvm)'
,
t3
-
t2
)
print
(
'theano (scan, cvm)'
,
t3
-
t2
)
if
1
:
if
1
:
r
=
n
umpy
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
-
1
,
10
)
r
=
n
p
.
arange
(
10000
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
-
1
,
10
)
shared_r
=
theano
.
shared
(
r
)
shared_r
=
theano
.
shared
(
r
)
s_i
=
theano
.
shared
(
n
umpy
.
array
(
1
))
s_i
=
theano
.
shared
(
n
p
.
array
(
1
))
s_rinc
=
tensor
.
inc_subtensor
(
shared_r
[
s_i
],
shared_r
[
s_i
-
1
],
s_rinc
=
tensor
.
inc_subtensor
(
shared_r
[
s_i
],
shared_r
[
s_i
-
1
],
tolerate_inplace_aliasing
=
True
)
tolerate_inplace_aliasing
=
True
)
# theano.printing.debugprint(s_rinc)
# theano.printing.debugprint(s_rinc)
...
@@ -5075,18 +5075,18 @@ def test_speed_rnn():
...
@@ -5075,18 +5075,18 @@ def test_speed_rnn():
L
=
10000
L
=
10000
N
=
50
N
=
50
n
umpy
.
random
.
seed
(
2523452
)
n
p
.
random
.
seed
(
2523452
)
r
=
n
umpy
.
arange
(
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
N
)
r
=
n
p
.
arange
(
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
N
)
w
=
n
umpy
.
random
.
randn
(
N
,
N
)
.
astype
(
theano
.
config
.
floatX
)
w
=
n
p
.
random
.
randn
(
N
,
N
)
.
astype
(
theano
.
config
.
floatX
)
t0
=
time
.
time
()
t0
=
time
.
time
()
for
i
in
xrange
(
1
,
L
):
for
i
in
xrange
(
1
,
L
):
r
[
i
]
=
n
umpy
.
tanh
(
numpy
.
dot
(
r
[
i
-
1
],
w
))
r
[
i
]
=
n
p
.
tanh
(
np
.
dot
(
r
[
i
-
1
],
w
))
t1
=
time
.
time
()
t1
=
time
.
time
()
print
(
'python'
,
t1
-
t0
)
print
(
'python'
,
t1
-
t0
)
if
1
:
if
1
:
r
=
n
umpy
.
arange
(
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
N
)
r
=
n
p
.
arange
(
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
N
)
s_r
=
tensor
.
matrix
()
s_r
=
tensor
.
matrix
()
s_y
,
updates
=
theano
.
scan
(
s_y
,
updates
=
theano
.
scan
(
fn
=
lambda
ri
,
rii
:
tensor
.
tanh
(
tensor
.
dot
(
rii
,
w
)),
fn
=
lambda
ri
,
rii
:
tensor
.
tanh
(
tensor
.
dot
(
rii
,
w
)),
...
@@ -5102,7 +5102,7 @@ def test_speed_rnn():
...
@@ -5102,7 +5102,7 @@ def test_speed_rnn():
print
(
'theano (scan, cvm)'
,
t3
-
t2
)
print
(
'theano (scan, cvm)'
,
t3
-
t2
)
if
1
:
if
1
:
r
=
n
umpy
.
arange
(
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
N
)
r
=
n
p
.
arange
(
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
N
)
s_w
=
theano
.
shared
(
w
)
s_w
=
theano
.
shared
(
w
)
shared_r
=
theano
.
shared
(
r
)
shared_r
=
theano
.
shared
(
r
)
s_i
=
theano
.
scalar
.
sharedvar
.
shared
(
1
)
s_i
=
theano
.
scalar
.
sharedvar
.
shared
(
1
)
...
@@ -5154,18 +5154,18 @@ def test_speed_batchrnn():
...
@@ -5154,18 +5154,18 @@ def test_speed_batchrnn():
B
=
50
B
=
50
N
=
400
N
=
400
n
umpy
.
random
.
seed
(
2523452
)
n
p
.
random
.
seed
(
2523452
)
r
=
n
umpy
.
arange
(
B
*
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
B
,
N
)
r
=
n
p
.
arange
(
B
*
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
B
,
N
)
w
=
n
umpy
.
random
.
randn
(
N
,
N
)
.
astype
(
theano
.
config
.
floatX
)
w
=
n
p
.
random
.
randn
(
N
,
N
)
.
astype
(
theano
.
config
.
floatX
)
t0
=
time
.
time
()
t0
=
time
.
time
()
for
i
in
xrange
(
1
,
L
):
for
i
in
xrange
(
1
,
L
):
r
[
i
]
=
n
umpy
.
tanh
(
numpy
.
dot
(
r
[
i
-
1
],
w
))
r
[
i
]
=
n
p
.
tanh
(
np
.
dot
(
r
[
i
-
1
],
w
))
t1
=
time
.
time
()
t1
=
time
.
time
()
print
(
'python'
,
t1
-
t0
)
print
(
'python'
,
t1
-
t0
)
if
1
:
if
1
:
r
=
n
umpy
.
arange
(
B
*
L
*
N
)
.
astype
(
r
=
n
p
.
arange
(
B
*
L
*
N
)
.
astype
(
theano
.
config
.
floatX
)
.
reshape
(
L
,
B
,
N
)
theano
.
config
.
floatX
)
.
reshape
(
L
,
B
,
N
)
s_w
=
theano
.
shared
(
w
)
s_w
=
theano
.
shared
(
w
)
shared_r
=
theano
.
shared
(
r
)
shared_r
=
theano
.
shared
(
r
)
...
@@ -5328,9 +5328,9 @@ def test_compute_test_value():
...
@@ -5328,9 +5328,9 @@ def test_compute_test_value():
theano
.
config
.
compute_test_value
=
'raise'
theano
.
config
.
compute_test_value
=
'raise'
try
:
try
:
x
=
tensor
.
vector
(
'x'
)
x
=
tensor
.
vector
(
'x'
)
xv
=
n
umpy
.
ones
(
3
,
dtype
=
theano
.
config
.
floatX
)
xv
=
n
p
.
ones
(
3
,
dtype
=
theano
.
config
.
floatX
)
x
.
tag
.
test_value
=
xv
x
.
tag
.
test_value
=
xv
y
=
theano
.
shared
(
n
umpy
.
arange
(
3
,
dtype
=
theano
.
config
.
floatX
),
y
=
theano
.
shared
(
n
p
.
arange
(
3
,
dtype
=
theano
.
config
.
floatX
),
name
=
'y'
)
name
=
'y'
)
z
,
updates
=
theano
.
scan
(
z
,
updates
=
theano
.
scan
(
fn
=
lambda
u
,
v
:
u
+
v
,
fn
=
lambda
u
,
v
:
u
+
v
,
...
@@ -5351,10 +5351,10 @@ def test_compute_test_value_nonseq():
...
@@ -5351,10 +5351,10 @@ def test_compute_test_value_nonseq():
theano
.
config
.
compute_test_value
=
'raise'
theano
.
config
.
compute_test_value
=
'raise'
try
:
try
:
x
=
tensor
.
vector
(
'x'
)
x
=
tensor
.
vector
(
'x'
)
xv
=
n
umpy
.
ones
(
3
,
dtype
=
theano
.
config
.
floatX
)
xv
=
n
p
.
ones
(
3
,
dtype
=
theano
.
config
.
floatX
)
x
.
tag
.
test_value
=
xv
x
.
tag
.
test_value
=
xv
y
=
theano
.
shared
(
y
=
theano
.
shared
(
n
umpy
.
arange
(
9
,
dtype
=
theano
.
config
.
floatX
)
.
reshape
(
3
,
3
),
n
p
.
arange
(
9
,
dtype
=
theano
.
config
.
floatX
)
.
reshape
(
3
,
3
),
name
=
'y'
)
name
=
'y'
)
z
,
updates
=
theano
.
scan
(
z
,
updates
=
theano
.
scan
(
fn
=
lambda
u
,
v
:
u
+
v
,
fn
=
lambda
u
,
v
:
u
+
v
,
...
@@ -5373,7 +5373,7 @@ def test_compute_test_value_nonseq():
...
@@ -5373,7 +5373,7 @@ def test_compute_test_value_nonseq():
def
test_compute_test_value_grad
():
def
test_compute_test_value_grad
():
# Test case originally reported by Bitton Tenessi
# Test case originally reported by Bitton Tenessi
# https://groups.google.com/d/msg/theano-users/fAP3i2CbskQ/3OgBf4yjqiQJ
# https://groups.google.com/d/msg/theano-users/fAP3i2CbskQ/3OgBf4yjqiQJ
WEIGHT
=
n
umpy
.
array
([
1
,
2
,
1
,
3
,
4
,
1
,
5
,
6
,
1
,
7
,
8
,
1
],
WEIGHT
=
n
p
.
array
([
1
,
2
,
1
,
3
,
4
,
1
,
5
,
6
,
1
,
7
,
8
,
1
],
dtype
=
'float32'
)
dtype
=
'float32'
)
old_compute_test_val
=
theano
.
config
.
compute_test_value
old_compute_test_val
=
theano
.
config
.
compute_test_value
...
@@ -5387,13 +5387,13 @@ def test_compute_test_value_grad():
...
@@ -5387,13 +5387,13 @@ def test_compute_test_value_grad():
W
=
W_flat
.
reshape
((
2
,
2
,
3
))
W
=
W_flat
.
reshape
((
2
,
2
,
3
))
outputs_mi
=
tensor
.
as_tensor_variable
(
outputs_mi
=
tensor
.
as_tensor_variable
(
n
umpy
.
asarray
(
0
,
dtype
=
'float32'
))
n
p
.
asarray
(
0
,
dtype
=
'float32'
))
outputs_mi
.
tag
.
test_value
=
n
umpy
.
asarray
(
0
,
dtype
=
'float32'
)
outputs_mi
.
tag
.
test_value
=
n
p
.
asarray
(
0
,
dtype
=
'float32'
)
def
loss_mi
(
mi
,
sum_mi
,
W
):
def
loss_mi
(
mi
,
sum_mi
,
W
):
outputs_ti
=
tensor
.
as_tensor_variable
(
outputs_ti
=
tensor
.
as_tensor_variable
(
n
umpy
.
asarray
(
0
,
dtype
=
'float32'
))
n
p
.
asarray
(
0
,
dtype
=
'float32'
))
outputs_ti
.
tag
.
test_value
=
n
umpy
.
asarray
(
0
,
dtype
=
'float32'
)
outputs_ti
.
tag
.
test_value
=
n
p
.
asarray
(
0
,
dtype
=
'float32'
)
def
loss_ti
(
ti
,
sum_ti
,
mi
,
W
):
def
loss_ti
(
ti
,
sum_ti
,
mi
,
W
):
return
W
.
sum
()
.
sum
()
.
sum
()
+
sum_ti
return
W
.
sum
()
.
sum
()
.
sum
()
+
sum_ti
...
@@ -5430,10 +5430,10 @@ def test_compute_test_value_grad_cast():
...
@@ -5430,10 +5430,10 @@ def test_compute_test_value_grad_cast():
theano
.
config
.
compute_test_value
=
'raise'
theano
.
config
.
compute_test_value
=
'raise'
try
:
try
:
h
=
tensor
.
matrix
(
'h'
)
h
=
tensor
.
matrix
(
'h'
)
h
.
tag
.
test_value
=
n
umpy
.
array
([[
1
,
2
,
3
,
4
],
[
5
,
6
,
7
,
8
]],
h
.
tag
.
test_value
=
n
p
.
array
([[
1
,
2
,
3
,
4
],
[
5
,
6
,
7
,
8
]],
dtype
=
floatX
)
dtype
=
floatX
)
w
=
theano
.
shared
(
n
umpy
.
random
.
randn
(
4
,
3
)
.
astype
(
floatX
),
name
=
'w'
)
w
=
theano
.
shared
(
n
p
.
random
.
randn
(
4
,
3
)
.
astype
(
floatX
),
name
=
'w'
)
outputs
,
_
=
theano
.
scan
(
lambda
i
,
h
,
w
:
(
theano
.
dot
(
h
[
i
],
w
),
i
),
outputs
,
_
=
theano
.
scan
(
lambda
i
,
h
,
w
:
(
theano
.
dot
(
h
[
i
],
w
),
i
),
outputs_info
=
[
None
,
0
],
non_sequences
=
[
h
,
w
],
outputs_info
=
[
None
,
0
],
non_sequences
=
[
h
,
w
],
...
@@ -5473,10 +5473,10 @@ def test_outputs_taps_check():
...
@@ -5473,10 +5473,10 @@ def test_outputs_taps_check():
def
test_default_value_broadcasted
():
def
test_default_value_broadcasted
():
def
floatx
(
X
):
def
floatx
(
X
):
return
n
umpy
.
asarray
(
X
,
dtype
=
theano
.
config
.
floatX
)
return
n
p
.
asarray
(
X
,
dtype
=
theano
.
config
.
floatX
)
def
init_weights
(
shape
,
name
):
def
init_weights
(
shape
,
name
):
return
theano
.
shared
(
floatx
(
n
umpy
.
random
.
randn
(
*
shape
)
*
0.1
),
name
)
return
theano
.
shared
(
floatx
(
n
p
.
random
.
randn
(
*
shape
)
*
0.1
),
name
)
X
=
theano
.
tensor
.
matrix
(
'X'
)
X
=
theano
.
tensor
.
matrix
(
'X'
)
in_size
=
2
in_size
=
2
...
@@ -5494,14 +5494,14 @@ def test_default_value_broadcasted():
...
@@ -5494,14 +5494,14 @@ def test_default_value_broadcasted():
gW_x
=
theano
.
tensor
.
grad
(
cost
,
W_x
)
gW_x
=
theano
.
tensor
.
grad
(
cost
,
W_x
)
updates
=
[(
W_x
,
W_x
-
0.1
*
gW_x
)]
updates
=
[(
W_x
,
W_x
-
0.1
*
gW_x
)]
f
=
theano
.
function
([
X
],
outputs
=
cost
,
updates
=
updates
)
f
=
theano
.
function
([
X
],
outputs
=
cost
,
updates
=
updates
)
f
(
n
umpy
.
random
.
rand
(
10
,
in_size
)
.
astype
(
X
.
dtype
))
f
(
n
p
.
random
.
rand
(
10
,
in_size
)
.
astype
(
X
.
dtype
))
class
TestInconsistentBroadcast
(
unittest
.
TestCase
):
class
TestInconsistentBroadcast
(
unittest
.
TestCase
):
def
test_raise_error
(
self
):
def
test_raise_error
(
self
):
x
=
tensor
.
tensor3
()
x
=
tensor
.
tensor3
()
initial_x
=
tensor
.
constant
(
n
umpy
.
zeros
((
1
,
10
)))
initial_x
=
tensor
.
constant
(
n
p
.
zeros
((
1
,
10
)))
y
,
updates
=
theano
.
scan
(
fn
=
lambda
x
,
prev_x
:
x
+
prev_x
,
y
,
updates
=
theano
.
scan
(
fn
=
lambda
x
,
prev_x
:
x
+
prev_x
,
sequences
=
x
,
sequences
=
x
,
outputs_info
=
[
dict
(
initial
=
initial_x
)])
outputs_info
=
[
dict
(
initial
=
initial_x
)])
...
...
theano/scan_module/tests/test_scan_checkpoints.py
浏览文件 @
bea31470
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
unittest
import
unittest
import
theano
import
theano
...
@@ -39,14 +39,14 @@ class TestScanCheckpoint(unittest.TestCase):
...
@@ -39,14 +39,14 @@ class TestScanCheckpoint(unittest.TestCase):
f
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
f
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
outputs
=
[
self
.
result
,
self
.
result_check
])
outputs
=
[
self
.
result
,
self
.
result_check
])
out
,
out_check
=
f
(
range
(
10
),
101
)
out
,
out_check
=
f
(
range
(
10
),
101
)
assert
n
umpy
.
allclose
(
out
,
out_check
)
assert
n
p
.
allclose
(
out
,
out_check
)
def
test_backward_pass
(
self
):
def
test_backward_pass
(
self
):
"""Test gradient computation of A**k."""
"""Test gradient computation of A**k."""
f
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
f
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
outputs
=
[
self
.
grad_A
,
self
.
grad_A_check
])
outputs
=
[
self
.
grad_A
,
self
.
grad_A_check
])
out
,
out_check
=
f
(
range
(
10
),
101
)
out
,
out_check
=
f
(
range
(
10
),
101
)
assert
n
umpy
.
allclose
(
out
,
out_check
)
assert
n
p
.
allclose
(
out
,
out_check
)
@unittest.skipUnless
(
PYGPU_AVAILABLE
,
'Requires pygpu.'
)
@unittest.skipUnless
(
PYGPU_AVAILABLE
,
'Requires pygpu.'
)
def
test_memory
(
self
):
def
test_memory
(
self
):
...
@@ -59,7 +59,7 @@ class TestScanCheckpoint(unittest.TestCase):
...
@@ -59,7 +59,7 @@ class TestScanCheckpoint(unittest.TestCase):
f_check
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
f_check
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
outputs
=
self
.
grad_A_check
,
mode
=
mode_with_gpu
)
outputs
=
self
.
grad_A_check
,
mode
=
mode_with_gpu
)
free_gmem
=
theano
.
gpuarray
.
type
.
_context_reg
[
None
]
.
free_gmem
free_gmem
=
theano
.
gpuarray
.
type
.
_context_reg
[
None
]
.
free_gmem
data
=
n
umpy
.
ones
(
free_gmem
//
3000
,
dtype
=
numpy
.
float32
)
data
=
n
p
.
ones
(
free_gmem
//
3000
,
dtype
=
np
.
float32
)
# Check that it works with the checkpoints
# Check that it works with the checkpoints
f_check
(
data
,
1000
)
f_check
(
data
,
1000
)
# Check that the basic scan fails in that case
# Check that the basic scan fails in that case
...
...
theano/scan_module/tests/test_scan_opt.py
浏览文件 @
bea31470
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
unittest
import
unittest
import
theano
import
theano
...
@@ -18,7 +18,7 @@ class TestGaussNewton(unittest.TestCase):
...
@@ -18,7 +18,7 @@ class TestGaussNewton(unittest.TestCase):
This test case is based on code by Sigurd Spieckermann.
This test case is based on code by Sigurd Spieckermann.
"""
"""
def
setUp
(
self
):
def
setUp
(
self
):
self
.
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
def
_run
(
self
,
num_features
,
num_timesteps
,
batch_size
,
mode
):
def
_run
(
self
,
num_features
,
num_timesteps
,
batch_size
,
mode
):
# determine shapes of inputs and targets depending on the batch size
# determine shapes of inputs and targets depending on the batch size
...
@@ -58,8 +58,8 @@ class TestGaussNewton(unittest.TestCase):
...
@@ -58,8 +58,8 @@ class TestGaussNewton(unittest.TestCase):
W_hy
=
theano
.
shared
(
W_hy
=
theano
.
shared
(
(
0.01
*
self
.
rng
.
uniform
(
size
=
(
10
,
1
)))
.
astype
(
config
.
floatX
),
(
0.01
*
self
.
rng
.
uniform
(
size
=
(
10
,
1
)))
.
astype
(
config
.
floatX
),
borrow
=
True
)
borrow
=
True
)
b_h
=
theano
.
shared
(
n
umpy
.
zeros
(
10
)
.
astype
(
config
.
floatX
),
borrow
=
True
)
b_h
=
theano
.
shared
(
n
p
.
zeros
(
10
)
.
astype
(
config
.
floatX
),
borrow
=
True
)
b_y
=
theano
.
shared
(
n
umpy
.
zeros
(
1
)
.
astype
(
config
.
floatX
),
borrow
=
True
)
b_y
=
theano
.
shared
(
n
p
.
zeros
(
1
)
.
astype
(
config
.
floatX
),
borrow
=
True
)
params
=
[
W_xh
,
W_hh
,
W_hy
,
b_h
,
b_y
]
params
=
[
W_xh
,
W_hh
,
W_hy
,
b_h
,
b_y
]
...
@@ -171,8 +171,8 @@ class TestPushOutScanOutputDot(object):
...
@@ -171,8 +171,8 @@ class TestPushOutScanOutputDot(object):
# Ensure that the function compiled with the optimization produces
# Ensure that the function compiled with the optimization produces
# the same results as the function compiled without
# the same results as the function compiled without
v_value
=
n
umpy
.
random
.
random
((
4
))
.
astype
(
config
.
floatX
)
v_value
=
n
p
.
random
.
random
((
4
))
.
astype
(
config
.
floatX
)
m_value
=
n
umpy
.
random
.
random
((
4
,
5
))
.
astype
(
config
.
floatX
)
m_value
=
n
p
.
random
.
random
((
4
,
5
))
.
astype
(
config
.
floatX
)
output_opt
=
f_opt
(
v_value
,
m_value
)
output_opt
=
f_opt
(
v_value
,
m_value
)
output_no_opt
=
f_no_opt
(
v_value
,
m_value
)
output_no_opt
=
f_no_opt
(
v_value
,
m_value
)
...
@@ -217,8 +217,8 @@ class TestPushOutScanOutputDot(object):
...
@@ -217,8 +217,8 @@ class TestPushOutScanOutputDot(object):
# Ensure that the function compiled with the optimization produces
# Ensure that the function compiled with the optimization produces
# the same results as the function compiled without
# the same results as the function compiled without
a_value
=
n
umpy
.
random
.
random
((
3
,
4
))
.
astype
(
config
.
floatX
)
a_value
=
n
p
.
random
.
random
((
3
,
4
))
.
astype
(
config
.
floatX
)
b_value
=
n
umpy
.
random
.
random
((
4
,
5
))
.
astype
(
config
.
floatX
)
b_value
=
n
p
.
random
.
random
((
4
,
5
))
.
astype
(
config
.
floatX
)
output_opt
=
f_opt
(
a_value
,
b_value
)
output_opt
=
f_opt
(
a_value
,
b_value
)
output_no_opt
=
f_no_opt
(
a_value
,
b_value
)
output_no_opt
=
f_no_opt
(
a_value
,
b_value
)
...
@@ -263,8 +263,8 @@ class TestPushOutScanOutputDot(object):
...
@@ -263,8 +263,8 @@ class TestPushOutScanOutputDot(object):
# Ensure that the function compiled with the optimization produces
# Ensure that the function compiled with the optimization produces
# the same results as the function compiled without
# the same results as the function compiled without
a_value
=
n
umpy
.
random
.
random
((
3
,
4
))
.
astype
(
config
.
floatX
)
a_value
=
n
p
.
random
.
random
((
3
,
4
))
.
astype
(
config
.
floatX
)
b_value
=
n
umpy
.
random
.
random
((
4
,
5
))
.
astype
(
config
.
floatX
)
b_value
=
n
p
.
random
.
random
((
4
,
5
))
.
astype
(
config
.
floatX
)
output_opt
=
f_opt
(
a_value
,
b_value
)
output_opt
=
f_opt
(
a_value
,
b_value
)
output_no_opt
=
f_no_opt
(
a_value
,
b_value
)
output_no_opt
=
f_no_opt
(
a_value
,
b_value
)
...
@@ -296,7 +296,7 @@ class TestPushOutSumOfDot():
...
@@ -296,7 +296,7 @@ class TestPushOutSumOfDot():
dim
=
5
dim
=
5
# Weight matrices
# Weight matrices
U
=
theano
.
shared
(
n
umpy
.
random
.
normal
(
size
=
(
dim
,
dim
),
U
=
theano
.
shared
(
n
p
.
random
.
normal
(
size
=
(
dim
,
dim
),
scale
=
0.0001
)
.
astype
(
config
.
floatX
))
scale
=
0.0001
)
.
astype
(
config
.
floatX
))
U
.
name
=
'U'
U
.
name
=
'U'
V
=
theano
.
shared
(
U
.
get_value
())
V
=
theano
.
shared
(
U
.
get_value
())
...
@@ -306,7 +306,7 @@ class TestPushOutSumOfDot():
...
@@ -306,7 +306,7 @@ class TestPushOutSumOfDot():
# Variables and their values
# Variables and their values
x
=
T
.
tensor3
(
'x'
)
x
=
T
.
tensor3
(
'x'
)
x_value
=
n
umpy
.
random
.
normal
(
size
=
(
seq_len
,
batch_size
,
dim
),
x_value
=
n
p
.
random
.
normal
(
size
=
(
seq_len
,
batch_size
,
dim
),
scale
=
0.0001
)
.
astype
(
config
.
floatX
)
scale
=
0.0001
)
.
astype
(
config
.
floatX
)
ri
=
T
.
tensor3
(
'ri'
)
ri
=
T
.
tensor3
(
'ri'
)
...
@@ -315,7 +315,7 @@ class TestPushOutSumOfDot():
...
@@ -315,7 +315,7 @@ class TestPushOutSumOfDot():
zi
=
T
.
tensor3
(
'zi'
)
zi
=
T
.
tensor3
(
'zi'
)
zi_value
=
x_value
zi_value
=
x_value
init
=
T
.
alloc
(
n
umpy
.
cast
[
config
.
floatX
](
0
),
batch_size
,
dim
)
init
=
T
.
alloc
(
n
p
.
cast
[
config
.
floatX
](
0
),
batch_size
,
dim
)
def
rnn_step1
(
def
rnn_step1
(
# sequences
# sequences
x
,
ri
,
zi
,
x
,
ri
,
zi
,
...
@@ -375,8 +375,8 @@ class TestPushOutSumOfDot():
...
@@ -375,8 +375,8 @@ class TestPushOutSumOfDot():
input2
=
T
.
tensor3
()
input2
=
T
.
tensor3
()
input3
=
T
.
tensor3
()
input3
=
T
.
tensor3
()
W
=
theano
.
shared
(
n
umpy
.
random
.
normal
(
size
=
(
4
,
5
)))
.
astype
(
config
.
floatX
)
W
=
theano
.
shared
(
n
p
.
random
.
normal
(
size
=
(
4
,
5
)))
.
astype
(
config
.
floatX
)
U
=
theano
.
shared
(
n
umpy
.
random
.
normal
(
size
=
(
6
,
7
)))
.
astype
(
config
.
floatX
)
U
=
theano
.
shared
(
n
p
.
random
.
normal
(
size
=
(
6
,
7
)))
.
astype
(
config
.
floatX
)
def
inner_fct
(
seq1
,
seq2
,
seq3
,
previous_output
):
def
inner_fct
(
seq1
,
seq2
,
seq3
,
previous_output
):
temp1
=
T
.
dot
(
seq1
,
W
)
+
seq3
temp1
=
T
.
dot
(
seq1
,
W
)
+
seq3
...
@@ -384,7 +384,7 @@ class TestPushOutSumOfDot():
...
@@ -384,7 +384,7 @@ class TestPushOutSumOfDot():
dot_output
=
T
.
dot
(
temp1
,
temp2
)
dot_output
=
T
.
dot
(
temp1
,
temp2
)
return
previous_output
+
dot_output
return
previous_output
+
dot_output
init
=
T
.
as_tensor_variable
(
n
umpy
.
random
.
normal
(
size
=
(
3
,
7
)))
init
=
T
.
as_tensor_variable
(
n
p
.
random
.
normal
(
size
=
(
3
,
7
)))
# Compile the function twice, once with the optimization and once
# Compile the function twice, once with the optimization and once
# without
# without
...
@@ -410,9 +410,9 @@ class TestPushOutSumOfDot():
...
@@ -410,9 +410,9 @@ class TestPushOutSumOfDot():
# TODO
# TODO
# Compare the outputs of the 2 functions
# Compare the outputs of the 2 functions
input1_value
=
n
umpy
.
random
.
random
((
2
,
3
,
4
))
.
astype
(
config
.
floatX
)
input1_value
=
n
p
.
random
.
random
((
2
,
3
,
4
))
.
astype
(
config
.
floatX
)
input2_value
=
n
umpy
.
random
.
random
((
2
,
5
,
6
))
.
astype
(
config
.
floatX
)
input2_value
=
n
p
.
random
.
random
((
2
,
5
,
6
))
.
astype
(
config
.
floatX
)
input3_value
=
n
umpy
.
random
.
random
((
2
,
3
,
5
))
.
astype
(
config
.
floatX
)
input3_value
=
n
p
.
random
.
random
((
2
,
3
,
5
))
.
astype
(
config
.
floatX
)
output_opt
=
f_opt
(
input1_value
,
input2_value
,
input3_value
)
output_opt
=
f_opt
(
input1_value
,
input2_value
,
input3_value
)
output_no_opt
=
f_no_opt
(
input1_value
,
input2_value
,
input3_value
)
output_no_opt
=
f_no_opt
(
input1_value
,
input2_value
,
input3_value
)
...
...
theano/scan_module/tests/test_scan_utils.py
浏览文件 @
bea31470
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
itertools
import
itertools
import
unittest
import
unittest
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
tensor
from
theano
import
tensor
from
theano.scan_module.scan_utils
import
equal_computations
,
map_variables
from
theano.scan_module.scan_utils
import
equal_computations
,
map_variables
...
@@ -51,8 +51,8 @@ class TestMapVariables(unittest.TestCase):
...
@@ -51,8 +51,8 @@ class TestMapVariables(unittest.TestCase):
s2
,
=
map_variables
(
self
.
replacer
,
[
s
])
s2
,
=
map_variables
(
self
.
replacer
,
[
s
])
f
=
theano
.
function
([
x
,
y
,
z
],
[
s
,
s2
])
f
=
theano
.
function
([
x
,
y
,
z
],
[
s
,
s2
])
rval
=
f
(
x
=
n
umpy
.
array
([
1
,
2
,
3
],
dtype
=
numpy
.
float32
),
y
=
1
,
z
=
2
)
rval
=
f
(
x
=
n
p
.
array
([
1
,
2
,
3
],
dtype
=
np
.
float32
),
y
=
1
,
z
=
2
)
assert
n
umpy
.
array_equal
(
rval
,
[[
1
,
2
,
3
],
[
2
,
4
,
6
]])
assert
n
p
.
array_equal
(
rval
,
[[
1
,
2
,
3
],
[
2
,
4
,
6
]])
def
test_scan
(
self
):
def
test_scan
(
self
):
x
=
tensor
.
vector
(
'x'
)
x
=
tensor
.
vector
(
'x'
)
...
@@ -64,7 +64,7 @@ class TestMapVariables(unittest.TestCase):
...
@@ -64,7 +64,7 @@ class TestMapVariables(unittest.TestCase):
# should do this as well.
# should do this as well.
outer
=
tensor
.
scalar
(
"outer"
)
outer
=
tensor
.
scalar
(
"outer"
)
shared
=
theano
.
shared
(
shared
=
theano
.
shared
(
n
umpy
.
array
(
1.
,
dtype
=
theano
.
config
.
floatX
),
n
p
.
array
(
1.
,
dtype
=
theano
.
config
.
floatX
),
name
=
"shared"
)
name
=
"shared"
)
constant
=
tensor
.
constant
(
1
,
name
=
"constant"
)
constant
=
tensor
.
constant
(
1
,
name
=
"constant"
)
...
@@ -77,7 +77,7 @@ class TestMapVariables(unittest.TestCase):
...
@@ -77,7 +77,7 @@ class TestMapVariables(unittest.TestCase):
return
r
return
r
s
,
_
=
theano
.
scan
(
step
,
sequences
=
x
,
s
,
_
=
theano
.
scan
(
step
,
sequences
=
x
,
outputs_info
=
[
n
umpy
.
array
(
0.
)])
outputs_info
=
[
n
p
.
array
(
0.
)])
# ensure z is owned by the outer graph so map_variables() will need to
# ensure z is owned by the outer graph so map_variables() will need to
# jump through additional hoops to placate FunctionGraph.
# jump through additional hoops to placate FunctionGraph.
t
=
z
*
s
t
=
z
*
s
...
@@ -85,8 +85,8 @@ class TestMapVariables(unittest.TestCase):
...
@@ -85,8 +85,8 @@ class TestMapVariables(unittest.TestCase):
t2
=
z
*
s2
t2
=
z
*
s2
f
=
theano
.
function
([
x
,
outer
],
[
t
,
t2
])
f
=
theano
.
function
([
x
,
outer
],
[
t
,
t2
])
rval
=
f
(
x
=
n
umpy
.
array
([
1
,
2
,
3
],
dtype
=
numpy
.
float32
),
outer
=
0.5
)
rval
=
f
(
x
=
n
p
.
array
([
1
,
2
,
3
],
dtype
=
np
.
float32
),
outer
=
0.5
)
assert
n
umpy
.
array_equal
(
rval
,
[[
1
,
3
,
6
],
[
-
1
,
-
3
,
-
6
]])
assert
n
p
.
array_equal
(
rval
,
[[
1
,
3
,
6
],
[
-
1
,
-
3
,
-
6
]])
def
test_scan_with_shared_update
(
self
):
def
test_scan_with_shared_update
(
self
):
x
=
tensor
.
vector
(
'x'
)
x
=
tensor
.
vector
(
'x'
)
...
@@ -104,7 +104,7 @@ class TestMapVariables(unittest.TestCase):
...
@@ -104,7 +104,7 @@ class TestMapVariables(unittest.TestCase):
return
r
return
r
s
,
_
=
theano
.
scan
(
step
,
sequences
=
x
,
s
,
_
=
theano
.
scan
(
step
,
sequences
=
x
,
outputs_info
=
[
n
umpy
.
array
(
0.
)])
outputs_info
=
[
n
p
.
array
(
0.
)])
self
.
assertRaises
(
NotImplementedError
,
self
.
assertRaises
(
NotImplementedError
,
map_variables
,
self
.
replacer
,
[
s
])
map_variables
,
self
.
replacer
,
[
s
])
...
@@ -128,7 +128,7 @@ class TestMapVariables(unittest.TestCase):
...
@@ -128,7 +128,7 @@ class TestMapVariables(unittest.TestCase):
return
r
+
counter
return
r
+
counter
s
,
_
=
theano
.
scan
(
step
,
sequences
=
x
,
s
,
_
=
theano
.
scan
(
step
,
sequences
=
x
,
outputs_info
=
[
n
umpy
.
array
(
0.
)])
outputs_info
=
[
n
p
.
array
(
0.
)])
self
.
assertRaises
(
NotImplementedError
,
self
.
assertRaises
(
NotImplementedError
,
map_variables
,
self
.
replacer
,
[
s
])
map_variables
,
self
.
replacer
,
[
s
])
...
@@ -137,7 +137,7 @@ class TestMapVariables(unittest.TestCase):
...
@@ -137,7 +137,7 @@ class TestMapVariables(unittest.TestCase):
# inner graph.
# inner graph.
outer
=
tensor
.
scalar
(
"outer"
)
outer
=
tensor
.
scalar
(
"outer"
)
shared
=
theano
.
shared
(
shared
=
theano
.
shared
(
n
umpy
.
array
(
1.
,
dtype
=
theano
.
config
.
floatX
),
n
p
.
array
(
1.
,
dtype
=
theano
.
config
.
floatX
),
name
=
"shared"
)
name
=
"shared"
)
constant
=
tensor
.
constant
(
1.
,
name
=
"constant"
)
constant
=
tensor
.
constant
(
1.
,
name
=
"constant"
)
z
=
outer
*
(
shared
+
constant
)
z
=
outer
*
(
shared
+
constant
)
...
...
theano/tensor/blas.py
浏览文件 @
bea31470
...
@@ -130,10 +130,10 @@ import logging
...
@@ -130,10 +130,10 @@ import logging
import
os
import
os
import
time
import
time
import
numpy
import
numpy
as
np
import
numpy.distutils
import
numpy.distutils
try
:
try
:
import
numpy.distutils.__config__
import
numpy.distutils.__config__
# noqa
except
ImportError
:
except
ImportError
:
pass
pass
...
@@ -166,10 +166,10 @@ try:
...
@@ -166,10 +166,10 @@ try:
# `scipy.linalg.blas.fblas` with `scipy.linalg.blas`.
# `scipy.linalg.blas.fblas` with `scipy.linalg.blas`.
# See http://github.com/scipy/scipy/pull/358
# See http://github.com/scipy/scipy/pull/358
fblas
=
scipy
.
linalg
.
blas
fblas
=
scipy
.
linalg
.
blas
_blas_gemv_fns
=
{
n
umpy
.
dtype
(
'float32'
):
fblas
.
sgemv
,
_blas_gemv_fns
=
{
n
p
.
dtype
(
'float32'
):
fblas
.
sgemv
,
n
umpy
.
dtype
(
'float64'
):
fblas
.
dgemv
,
n
p
.
dtype
(
'float64'
):
fblas
.
dgemv
,
n
umpy
.
dtype
(
'complex64'
):
fblas
.
cgemv
,
n
p
.
dtype
(
'complex64'
):
fblas
.
cgemv
,
n
umpy
.
dtype
(
'complex128'
):
fblas
.
zgemv
}
n
p
.
dtype
(
'complex128'
):
fblas
.
zgemv
}
except
ImportError
as
e
:
except
ImportError
as
e
:
have_fblas
=
False
have_fblas
=
False
# This is used in Gemv and ScipyGer. We use CGemv and CGer
# This is used in Gemv and ScipyGer. We use CGemv and CGer
...
@@ -190,12 +190,12 @@ def check_init_y():
...
@@ -190,12 +190,12 @@ def check_init_y():
if
not
have_fblas
:
if
not
have_fblas
:
check_init_y
.
_result
=
False
check_init_y
.
_result
=
False
y
=
float
(
'NaN'
)
*
n
umpy
.
ones
((
2
,))
y
=
float
(
'NaN'
)
*
n
p
.
ones
((
2
,))
x
=
n
umpy
.
ones
((
2
,))
x
=
n
p
.
ones
((
2
,))
A
=
n
umpy
.
ones
((
2
,
2
))
A
=
n
p
.
ones
((
2
,
2
))
gemv
=
_blas_gemv_fns
[
y
.
dtype
]
gemv
=
_blas_gemv_fns
[
y
.
dtype
]
gemv
(
1.0
,
A
.
T
,
x
,
0.0
,
y
,
overwrite_y
=
True
,
trans
=
True
)
gemv
(
1.0
,
A
.
T
,
x
,
0.0
,
y
,
overwrite_y
=
True
,
trans
=
True
)
check_init_y
.
_result
=
n
umpy
.
isnan
(
y
)
.
any
()
check_init_y
.
_result
=
n
p
.
isnan
(
y
)
.
any
()
return
check_init_y
.
_result
return
check_init_y
.
_result
...
@@ -269,7 +269,7 @@ class Gemv(Op):
...
@@ -269,7 +269,7 @@ class Gemv(Op):
out_storage
[
0
][
0
]
=
gemv
(
alpha
,
A
.
T
,
x
,
beta
,
y
,
out_storage
[
0
][
0
]
=
gemv
(
alpha
,
A
.
T
,
x
,
beta
,
y
,
overwrite_y
=
self
.
inplace
,
trans
=
True
)
overwrite_y
=
self
.
inplace
,
trans
=
True
)
else
:
else
:
out
=
n
umpy
.
dot
(
A
,
x
)
out
=
n
p
.
dot
(
A
,
x
)
if
alpha
!=
1
:
if
alpha
!=
1
:
out
*=
alpha
out
*=
alpha
if
beta
!=
0
:
if
beta
!=
0
:
...
@@ -277,7 +277,7 @@ class Gemv(Op):
...
@@ -277,7 +277,7 @@ class Gemv(Op):
out
+=
beta
*
y
out
+=
beta
*
y
else
:
else
:
out
+=
y
out
+=
y
out_storage
[
0
][
0
]
=
n
umpy
.
asarray
(
out
,
dtype
=
y
.
dtype
)
out_storage
[
0
][
0
]
=
n
p
.
asarray
(
out
,
dtype
=
y
.
dtype
)
def
infer_shape
(
self
,
node
,
input_shapes
):
def
infer_shape
(
self
,
node
,
input_shapes
):
return
[
input_shapes
[
0
]]
return
[
input_shapes
[
0
]]
...
@@ -341,9 +341,9 @@ class Ger(Op):
...
@@ -341,9 +341,9 @@ class Ger(Op):
else
:
else
:
A
=
cA
.
copy
()
A
=
cA
.
copy
()
if
calpha
!=
1
:
if
calpha
!=
1
:
A
+=
calpha
*
n
umpy
.
outer
(
cx
,
cy
)
A
+=
calpha
*
n
p
.
outer
(
cx
,
cy
)
else
:
else
:
A
+=
n
umpy
.
outer
(
cx
,
cy
)
A
+=
n
p
.
outer
(
cx
,
cy
)
cZ
[
0
]
=
A
cZ
[
0
]
=
A
def
infer_shape
(
self
,
node
,
input_shapes
):
def
infer_shape
(
self
,
node
,
input_shapes
):
...
@@ -900,26 +900,26 @@ class Gemm(GemmRelated):
...
@@ -900,26 +900,26 @@ class Gemm(GemmRelated):
if
not
self
.
inplace
:
if
not
self
.
inplace
:
z
=
z
.
copy
()
# the original z will not be changed
z
=
z
.
copy
()
# the original z will not be changed
if
z
.
shape
==
():
if
z
.
shape
==
():
z
.
itemset
(
z
*
a
+
b
*
n
umpy
.
dot
(
x
,
y
))
z
.
itemset
(
z
*
a
+
b
*
n
p
.
dot
(
x
,
y
))
zout
[
0
]
=
z
zout
[
0
]
=
z
else
:
else
:
if
b
==
0.0
:
if
b
==
0.0
:
if
a
==
1.0
:
if
a
==
1.0
:
z
[:]
=
n
umpy
.
dot
(
x
,
y
)
z
[:]
=
n
p
.
dot
(
x
,
y
)
elif
a
==
-
1.0
:
elif
a
==
-
1.0
:
z
[:]
=
-
n
umpy
.
dot
(
x
,
y
)
z
[:]
=
-
n
p
.
dot
(
x
,
y
)
else
:
else
:
z
[:]
=
a
*
n
umpy
.
dot
(
x
,
y
)
z
[:]
=
a
*
n
p
.
dot
(
x
,
y
)
elif
b
==
1.0
:
elif
b
==
1.0
:
if
a
==
1.0
:
if
a
==
1.0
:
z
+=
n
umpy
.
dot
(
x
,
y
)
z
+=
n
p
.
dot
(
x
,
y
)
elif
a
==
-
1.0
:
elif
a
==
-
1.0
:
z
-=
n
umpy
.
dot
(
x
,
y
)
z
-=
n
p
.
dot
(
x
,
y
)
else
:
else
:
z
+=
a
*
n
umpy
.
dot
(
x
,
y
)
z
+=
a
*
n
p
.
dot
(
x
,
y
)
else
:
else
:
z
*=
b
z
*=
b
z
+=
a
*
n
umpy
.
dot
(
x
,
y
)
z
+=
a
*
n
p
.
dot
(
x
,
y
)
zout
[
0
]
=
z
zout
[
0
]
=
z
def
infer_shape
(
self
,
node
,
input_shapes
):
def
infer_shape
(
self
,
node
,
input_shapes
):
...
@@ -1066,7 +1066,7 @@ def _as_scalar(res, dtype=None):
...
@@ -1066,7 +1066,7 @@ def _as_scalar(res, dtype=None):
"""Return None or a TensorVariable whose type is in T.float_scalar_types"""
"""Return None or a TensorVariable whose type is in T.float_scalar_types"""
if
dtype
is
None
:
if
dtype
is
None
:
dtype
=
config
.
floatX
dtype
=
config
.
floatX
if
n
umpy
.
all
(
res
.
type
.
broadcastable
):
if
n
p
.
all
(
res
.
type
.
broadcastable
):
while
res
.
owner
and
isinstance
(
res
.
owner
.
op
,
T
.
DimShuffle
):
while
res
.
owner
and
isinstance
(
res
.
owner
.
op
,
T
.
DimShuffle
):
res
=
res
.
owner
.
inputs
[
0
]
res
=
res
.
owner
.
inputs
[
0
]
# may still have some number of True's
# may still have some number of True's
...
@@ -1216,7 +1216,7 @@ def _gemm_canonicalize(r, scale, rval, maxclients):
...
@@ -1216,7 +1216,7 @@ def _gemm_canonicalize(r, scale, rval, maxclients):
vectors
=
[]
vectors
=
[]
matrices
=
[]
matrices
=
[]
for
i
in
r
.
owner
.
inputs
:
for
i
in
r
.
owner
.
inputs
:
if
n
umpy
.
all
(
i
.
type
.
broadcastable
):
if
n
p
.
all
(
i
.
type
.
broadcastable
):
while
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
T
.
DimShuffle
):
while
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
T
.
DimShuffle
):
i
=
i
.
owner
.
inputs
[
0
]
i
=
i
.
owner
.
inputs
[
0
]
if
i
.
type
.
broadcastable
:
if
i
.
type
.
broadcastable
:
...
@@ -1539,7 +1539,7 @@ class Dot22(GemmRelated):
...
@@ -1539,7 +1539,7 @@ class Dot22(GemmRelated):
x
,
y
=
inp
x
,
y
=
inp
z
,
=
out
z
,
=
out
try
:
try
:
z
[
0
]
=
n
umpy
.
asarray
(
numpy
.
dot
(
x
,
y
))
z
[
0
]
=
n
p
.
asarray
(
np
.
dot
(
x
,
y
))
except
ValueError
as
e
:
except
ValueError
as
e
:
# The error raised by numpy has no shape information, we mean to
# The error raised by numpy has no shape information, we mean to
# add that
# add that
...
@@ -1704,8 +1704,8 @@ def local_dot22_to_ger_or_gemv(node):
...
@@ -1704,8 +1704,8 @@ def local_dot22_to_ger_or_gemv(node):
x
,
y
=
node
.
inputs
x
,
y
=
node
.
inputs
xb
=
x
.
broadcastable
xb
=
x
.
broadcastable
yb
=
y
.
broadcastable
yb
=
y
.
broadcastable
one
=
T
.
as_tensor_variable
(
n
umpy
.
asarray
(
1
,
dtype
=
x
.
dtype
))
one
=
T
.
as_tensor_variable
(
n
p
.
asarray
(
1
,
dtype
=
x
.
dtype
))
zero
=
T
.
as_tensor_variable
(
n
umpy
.
asarray
(
0
,
dtype
=
x
.
dtype
))
zero
=
T
.
as_tensor_variable
(
n
p
.
asarray
(
0
,
dtype
=
x
.
dtype
))
if
xb
[
1
]
and
yb
[
0
]:
if
xb
[
1
]
and
yb
[
0
]:
# x and y are both vectors so this might qualifies for a GER
# x and y are both vectors so this might qualifies for a GER
xv
=
x
.
dimshuffle
(
0
)
xv
=
x
.
dimshuffle
(
0
)
...
@@ -1810,7 +1810,7 @@ class Dot22Scalar(GemmRelated):
...
@@ -1810,7 +1810,7 @@ class Dot22Scalar(GemmRelated):
x
,
y
,
scalar
=
inp
x
,
y
,
scalar
=
inp
z
,
=
out
z
,
=
out
try
:
try
:
z
[
0
]
=
n
umpy
.
asarray
(
scalar
*
numpy
.
dot
(
x
,
y
))
z
[
0
]
=
n
p
.
asarray
(
scalar
*
np
.
dot
(
x
,
y
))
except
ValueError
as
e
:
except
ValueError
as
e
:
# The error raised by numpy has no shape information, we
# The error raised by numpy has no shape information, we
# mean to add that
# mean to add that
...
@@ -2034,9 +2034,9 @@ class BatchedDot(Op):
...
@@ -2034,9 +2034,9 @@ class BatchedDot(Op):
shape
=
self
.
infer_shape
(
node
,
[
i
.
shape
for
i
in
inp
])[
0
]
shape
=
self
.
infer_shape
(
node
,
[
i
.
shape
for
i
in
inp
])[
0
]
dtype
=
node
.
outputs
[
0
]
.
dtype
dtype
=
node
.
outputs
[
0
]
.
dtype
z0
=
z
[
0
]
=
n
umpy
.
empty
(
shape
,
dtype
=
dtype
)
z0
=
z
[
0
]
=
n
p
.
empty
(
shape
,
dtype
=
dtype
)
for
i
in
xrange
(
z0
.
shape
[
0
]):
for
i
in
xrange
(
z0
.
shape
[
0
]):
z0
[
i
]
=
n
umpy
.
dot
(
x
[
i
],
y
[
i
])
z0
[
i
]
=
n
p
.
dot
(
x
[
i
],
y
[
i
])
def
c_support_code
(
self
):
def
c_support_code
(
self
):
batch_gemm_defn
=
"""
batch_gemm_defn
=
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论