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 个修改的文件
包含
179 行增加
和
179 行删除
+179
-179
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
+0
-0
test_multinomial.py
theano/sandbox/tests/test_multinomial.py
+17
-17
test_multinomial_wo_replacement.py
theano/sandbox/tests/test_multinomial_wo_replacement.py
+29
-29
test_rng_mrg.py
theano/sandbox/tests/test_rng_mrg.py
+0
-0
scan.py
theano/scan_module/scan.py
+10
-10
scan_op.py
theano/scan_module/scan_op.py
+23
-23
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
+0
-0
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
差异被折叠。
点击展开。
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
差异被折叠。
点击展开。
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,44 +901,44 @@ class Scan(PureOp):
...
@@ -901,44 +901,44 @@ 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
=
n
umpy
.
asarray
(
self
.
vector_seqs
,
cython_vector_seqs
=
n
p
.
asarray
(
self
.
vector_seqs
,
dtype
=
'int32'
)
dtype
=
'int32'
)
cython_vector_outs
=
n
umpy
.
asarray
(
self
.
vector_outs
,
cython_vector_outs
=
n
p
.
asarray
(
self
.
vector_outs
,
dtype
=
'int32'
)
dtype
=
'int32'
)
cython_mitmots_preallocated
=
n
umpy
.
asarray
(
self
.
mitmots_preallocated
,
cython_mitmots_preallocated
=
n
p
.
asarray
(
self
.
mitmots_preallocated
,
dtype
=
'int32'
)
dtype
=
'int32'
)
cython_inps_is_tensor
=
n
umpy
.
asarray
(
self
.
inps_is_tensor
,
cython_inps_is_tensor
=
n
p
.
asarray
(
self
.
inps_is_tensor
,
dtype
=
'int32'
)
dtype
=
'int32'
)
cython_outs_is_tensor
=
n
umpy
.
asarray
(
self
.
outs_is_tensor
,
cython_outs_is_tensor
=
n
p
.
asarray
(
self
.
outs_is_tensor
,
dtype
=
'int32'
)
dtype
=
'int32'
)
if
hasattr
(
self
,
'destroy_map'
):
if
hasattr
(
self
,
'destroy_map'
):
...
@@ -946,7 +946,7 @@ class Scan(PureOp):
...
@@ -946,7 +946,7 @@ class Scan(PureOp):
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
...
@@ -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,7 +2726,7 @@ class Scan(PureOp):
...
@@ -2726,7 +2726,7 @@ 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
]):
...
@@ -2742,7 +2742,7 @@ class Scan(PureOp):
...
@@ -2742,7 +2742,7 @@ 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
=
[]
...
@@ -2795,7 +2795,7 @@ class Scan(PureOp):
...
@@ -2795,7 +2795,7 @@ 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
...
...
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
差异被折叠。
点击展开。
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论