Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bc2f50e5
提交
bc2f50e5
authored
1月 13, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #340 from pascanur/fix_tests_sandbox_scan
Fix tests sandbox scan
上级
5e10c1f1
38a8c842
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
195 行增加
和
73 行删除
+195
-73
scan.py
theano/sandbox/scan_module/scan.py
+18
-5
scan_op.py
theano/sandbox/scan_module/scan_op.py
+2
-0
scan_utils.py
theano/sandbox/scan_module/scan_utils.py
+19
-7
test_scan.py
theano/sandbox/scan_module/tests/test_scan.py
+150
-55
test_utils.py
theano/sandbox/scan_module/tests/test_utils.py
+6
-6
没有找到文件。
theano/sandbox/scan_module/scan.py
浏览文件 @
bc2f50e5
...
@@ -49,7 +49,8 @@ import numpy
...
@@ -49,7 +49,8 @@ import numpy
from
theano.compile
import
SharedVariable
,
function
from
theano.compile
import
SharedVariable
,
function
from
theano
import
compile
from
theano
import
compile
from
theano
import
gof
from
theano
import
gof
from
theano.tensor
import
opt
from
theano.tensor
import
opt
,
TensorVariable
from
theano.tensor.sharedvar
import
TensorSharedVariable
from
theano
import
tensor
from
theano
import
tensor
from
theano
import
config
from
theano
import
config
from
theano.updates
import
Updates
from
theano.updates
import
Updates
...
@@ -435,10 +436,10 @@ def scan(fn,
...
@@ -435,10 +436,10 @@ def scan(fn,
pos
=
len
(
lengths
)
pos
=
len
(
lengths
)
for
sv
in
shared_inputs
:
for
sv
in
shared_inputs
:
if
sv
in
update_d
:
if
sv
in
update_d
:
if
isinstance
(
sv
,
TensorType
):
if
isinstance
(
sv
,
(
TensorVariable
,
TensorSharedVariable
)
):
# We can treat it as a sit sot
# We can treat it as a sit sot
nw_state
=
scan_utils
.
expand
(
nw_state
=
scan_utils
.
expand
(
tensor
.
unbroadcast
(
tensor
.
shape_padleft
(
sv
,
0
),
T
)
)
tensor
.
unbroadcast
(
tensor
.
shape_padleft
(
sv
),
0
),
T
)
additional_lengths
.
append
(
scalar_shared
(
numpy
.
int64
(
0
),
additional_lengths
.
append
(
scalar_shared
(
numpy
.
int64
(
0
),
name
=
'l
%
d'
%
pos
))
name
=
'l
%
d'
%
pos
))
pos
=
pos
+
1
pos
=
pos
+
1
...
@@ -454,6 +455,17 @@ def scan(fn,
...
@@ -454,6 +455,17 @@ def scan(fn,
non_numeric_output_states
.
append
(
update_d
[
sv
])
non_numeric_output_states
.
append
(
update_d
[
sv
])
original_non_numeric_shared_variables
.
append
(
sv
)
original_non_numeric_shared_variables
.
append
(
sv
)
# Replace shared variables in the update
_additional_output_states
=
[]
replace
=
{}
for
sv
,
buf
in
zip
(
original_numeric_shared_variables
,
additional_input_states
):
replace
[
sv
]
=
buf
[
t
]
for
out
in
additional_output_states
:
_additional_output_states
.
append
(
scan_utils
.
clone
(
out
,
replace
=
replace
))
additional_output_states
=
_additional_output_states
# 5.2 Collect inputs/outputs of the inner function
# 5.2 Collect inputs/outputs of the inner function
inputs
=
[]
inputs
=
[]
outputs
=
[]
outputs
=
[]
...
@@ -515,7 +527,7 @@ def scan(fn,
...
@@ -515,7 +527,7 @@ def scan(fn,
for
pos
in
xrange
(
len
(
states_and_outputs
)):
for
pos
in
xrange
(
len
(
states_and_outputs
)):
out
=
scan_utils
.
ScanPermutation
(
mintaps
[
pos
])(
out
=
scan_utils
.
ScanPermutation
(
mintaps
[
pos
])(
scan_outputs_update_rules
[
pos
],
t
)
scan_outputs_update_rules
[
pos
],
t
)
scan_outputs
.
append
(
out
[
mintap
:])
scan_outputs
.
append
(
out
[
mintap
s
[
pos
]
:])
# 5.6 Construct updates dictionary
# 5.6 Construct updates dictionary
update_rules
=
scan_outputs_update_rules
[
len
(
states_and_outputs
):]
update_rules
=
scan_outputs_update_rules
[
len
(
states_and_outputs
):]
updates
=
{}
updates
=
{}
...
@@ -553,7 +565,8 @@ def one_step_scan(fn,
...
@@ -553,7 +565,8 @@ def one_step_scan(fn,
arg_info
)
arg_info
)
# go through the taps
# go through the taps
mintap
=
abs
(
numpy
.
min
(
arg_info
[
'taps'
]))
mintap
=
abs
(
numpy
.
min
(
arg_info
[
'taps'
]))
states_slices
.
append
(
arg_info
[
'initial'
][
k
+
mintap
])
states_slices
.
extend
(
[
arg_info
[
'initial'
][
k
+
mintap
]
for
k
in
arg_info
[
'taps'
]])
# Re-order args
# Re-order args
args
=
(
inputs_slices
+
states_slices
+
parameters
)
args
=
(
inputs_slices
+
states_slices
+
parameters
)
...
...
theano/sandbox/scan_module/scan_op.py
浏览文件 @
bc2f50e5
...
@@ -277,6 +277,7 @@ class ScanOp(PureOp):
...
@@ -277,6 +277,7 @@ class ScanOp(PureOp):
for
var
,
length
,
val
in
state_buffers
:
for
var
,
length
,
val
in
state_buffers
:
var
.
set_value
(
val
[
0
],
borrow
=
True
)
var
.
set_value
(
val
[
0
],
borrow
=
True
)
length
.
set_value
(
val
[
0
]
.
shape
[
0
],
borrow
=
True
)
length
.
set_value
(
val
[
0
]
.
shape
[
0
],
borrow
=
True
)
self
.
index
.
set_value
(
numpy
.
int64
(
0
))
# grab fixed arguments
# grab fixed arguments
fix_args
=
[
x
[
0
]
for
x
in
non_tensor_buffers
]
fix_args
=
[
x
[
0
]
for
x
in
non_tensor_buffers
]
while
cont
and
pos
<
node_input_storage
[
0
][
0
]:
while
cont
and
pos
<
node_input_storage
[
0
][
0
]:
...
@@ -320,6 +321,7 @@ class ScanOp(PureOp):
...
@@ -320,6 +321,7 @@ class ScanOp(PureOp):
for
var
,
length
,
val
in
state_buffers
:
for
var
,
length
,
val
in
state_buffers
:
var
.
set_value
(
val
[
0
],
borrow
=
True
)
var
.
set_value
(
val
[
0
],
borrow
=
True
)
length
.
set_value
(
val
[
0
]
.
shape
[
0
],
borrow
=
True
)
length
.
set_value
(
val
[
0
]
.
shape
[
0
],
borrow
=
True
)
self
.
index
.
set_value
(
numpy
.
int64
(
0
))
# grab fixed arguments
# grab fixed arguments
fix_args
=
[
x
[
0
]
for
x
in
non_tensor_buffers
]
fix_args
=
[
x
[
0
]
for
x
in
non_tensor_buffers
]
for
dx
in
xrange
(
node_input_storage
[
0
][
0
]):
for
dx
in
xrange
(
node_input_storage
[
0
][
0
]):
...
...
theano/sandbox/scan_module/scan_utils.py
浏览文件 @
bc2f50e5
...
@@ -220,17 +220,29 @@ def canonical_arguments(sequences,
...
@@ -220,17 +220,29 @@ def canonical_arguments(sequences,
# We cut the sequence such that seq[i] to correspond to
# We cut the sequence such that seq[i] to correspond to
# seq[i-k]
# seq[i-k]
if
maxtap
<
0
:
if
maxtap
<
0
:
offset
=
abs
(
maxtap
)
offset
_max
=
abs
(
maxtap
)
else
:
else
:
offset
=
0
offset_max
=
0
if
mintap
<
0
:
offset_min
=
abs
(
mintap
)
else
:
offset_min
=
0
nw_input
=
orig_input
nw_input
=
orig_input
if
maxtap
==
mintap
and
maxtap
!=
0
:
if
maxtap
==
mintap
and
maxtap
!=
0
:
nw_input
=
nw_input
[:
abs
(
maxtap
)]
if
maxtap
>
0
:
elif
maxtap
-
k
!=
0
:
nw_input
=
nw_input
[
maxtap
:]
nw_input
=
nw_input
[
offset
+
k
-
mintap
:
\
else
:
-
(
maxtap
-
k
)]
nw_input
=
nw_input
[:
maxtap
]
else
:
st
=
k
+
offset_min
if
maxtap
>
0
:
ed
=
-
(
maxtap
+
offset_min
-
st
)
else
:
ed
=
-
(
offset_min
-
st
)
if
ed
!=
0
:
nw_input
=
nw_input
[
st
:
ed
]
else
:
else
:
nw_input
=
nw_input
[
offset
+
k
-
mintap
:]
nw_input
=
nw_input
[
st
:]
inputs
.
append
(
nw_input
)
inputs
.
append
(
nw_input
)
else
:
else
:
raise
ValueError
(
'Provided sequence makes no sense'
,
str
(
input
))
raise
ValueError
(
'Provided sequence makes no sense'
,
str
(
input
))
...
...
theano/sandbox/scan_module/tests/test_scan.py
浏览文件 @
bc2f50e5
...
@@ -2,6 +2,7 @@ import os
...
@@ -2,6 +2,7 @@ import os
import
shutil
import
shutil
from
tempfile
import
mkdtemp
from
tempfile
import
mkdtemp
import
time
import
time
import
sys
import
unittest
import
unittest
import
cPickle
import
cPickle
...
@@ -18,6 +19,7 @@ from numpy.testing.noseclasses import KnownFailureTest
...
@@ -18,6 +19,7 @@ from numpy.testing.noseclasses import KnownFailureTest
from
test_utils
import
*
from
test_utils
import
*
import
theano.sandbox.scan_module
as
scan_module
import
theano.sandbox.scan_module
as
scan_module
from
theano.sandbox.scan_module.scan_op
import
ScanOp
class
TestScan
(
unittest
.
TestCase
):
class
TestScan
(
unittest
.
TestCase
):
...
@@ -52,6 +54,10 @@ class TestScan(unittest.TestCase):
...
@@ -52,6 +54,10 @@ class TestScan(unittest.TestCase):
Number of shared variable with updates. They are all numeric.
Number of shared variable with updates. They are all numeric.
"""
"""
# Check the scan node has at least one output
if
n_outputs
+
n_shared_updates
+
len
(
states_info
)
==
0
:
return
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
n_ins
=
len
(
inputs_info
)
n_ins
=
len
(
inputs_info
)
inputs
=
[
tensor
.
matrix
(
'u
%
d'
%
k
)
for
k
in
xrange
(
n_ins
)]
inputs
=
[
tensor
.
matrix
(
'u
%
d'
%
k
)
for
k
in
xrange
(
n_ins
)]
...
@@ -60,23 +66,23 @@ class TestScan(unittest.TestCase):
...
@@ -60,23 +66,23 @@ class TestScan(unittest.TestCase):
scan_inputs
.
append
(
dict
(
input
=
inp
,
taps
=
[
x
[
'tap'
]
for
x
in
scan_inputs
.
append
(
dict
(
input
=
inp
,
taps
=
[
x
[
'tap'
]
for
x
in
info
]))
info
]))
n_states
=
len
(
states_info
)
n_states
=
len
(
states_info
)
states
=
[
tensor
.
matrix
(
'x
%
d'
%
k
)
for
k
in
xrange
(
n_states
)]
scan_states
=
[]
scan_states
=
[]
states
=
[]
states
=
[]
for
state
,
info
in
zip
(
states
,
states_info
)
:
for
info
in
states_info
:
if
len
(
info
)
==
1
and
info
[
0
][
'tap'
]
==
-
1
:
if
len
(
info
)
==
1
and
info
[
0
][
'tap'
]
==
-
1
:
state
=
tensor
.
vector
(
'x
%
d'
%
k
)
state
=
tensor
.
vector
(
'x
%
d'
%
k
)
states
.
append
(
state
)
states
.
append
(
state
)
scan_states
.
append
(
state
)
scan_states
.
append
(
state
)
else
:
else
:
state
=
tensor
.
matrix
(
'x
%
d'
%
k
)
state
=
tensor
.
matrix
(
'x
%
d'
%
k
)
states
.
append
(
state
s
)
states
.
append
(
state
)
scan_states
.
append
(
scan_states
.
append
(
dict
(
initial
=
state
,
taps
=
[
x
[
'tap'
]
for
x
in
info
]))
dict
(
initial
=
state
,
taps
=
[
x
[
'tap'
]
for
x
in
info
]))
n_parameters
=
len
(
parameters_info
)
n_parameters
=
len
(
parameters_info
)
parameters
=
[
tensor
.
vector
(
'p
%
d'
%
k
)
for
k
in
xrange
(
n_parameters
)]
parameters
=
[
tensor
.
vector
(
'p
%
d'
%
k
)
for
k
in
xrange
(
n_parameters
)]
original_shared_values
=
[]
original_shared_values
=
[]
shared_vars
=
[]
shared_vars
=
[]
for
k
in
xrange
(
n_shared_updates
):
for
k
in
xrange
(
n_shared_updates
):
data
=
rng
.
uniform
(
size
=
(
4
,))
.
astype
(
theano
.
config
.
floatX
)
data
=
rng
.
uniform
(
size
=
(
4
,))
.
astype
(
theano
.
config
.
floatX
)
original_shared_values
.
append
(
data
)
original_shared_values
.
append
(
data
)
...
@@ -101,15 +107,14 @@ class TestScan(unittest.TestCase):
...
@@ -101,15 +107,14 @@ class TestScan(unittest.TestCase):
states_out
=
[
to_add
]
*
n_states
states_out
=
[
to_add
]
*
n_states
for
dx
,
st_info
in
enumerate
(
states_info
):
for
dx
,
st_info
in
enumerate
(
states_info
):
for
info
in
st_info
:
for
info
in
st_info
:
try
:
arg
=
args
[
arg_pos
]
arg
=
args
[
arg_pos
]
except
:
# import ipdb; ipdb.set_trace()
raise
arg_pos
+=
1
arg_pos
+=
1
if
info
[
'use'
]:
if
info
[
'use'
]:
if
states_out
[
dx
]:
states_out
[
dx
]
=
states_out
[
dx
]
+
arg
*
3
states_out
[
dx
]
=
states_out
[
dx
]
+
arg
*
3
for
info
in
paramters_info
:
else
:
states_out
[
dx
]
=
arg
*
3
for
info
in
parameters_info
:
arg
=
args
[
arg_pos
]
arg
=
args
[
arg_pos
]
arg_pos
+=
1
arg_pos
+=
1
if
info
[
'use'
]:
if
info
[
'use'
]:
...
@@ -117,9 +122,20 @@ class TestScan(unittest.TestCase):
...
@@ -117,9 +122,20 @@ class TestScan(unittest.TestCase):
to_add
=
arg
*
4
to_add
=
arg
*
4
else
:
else
:
to_add
=
to_add
+
arg
*
4
to_add
=
to_add
+
arg
*
4
if
to_add
is
not
None
:
shared_outs
=
[
sh
*
5
+
to_add
for
sh
in
shared_vars
]
shared_outs
=
[
sh
*
5
+
to_add
for
sh
in
shared_vars
]
states_out
=
[
x
+
to_add
for
x
in
states_out
]
rval
=
[]
pure_outs
=
[
to_add
**
2
for
x
in
xrange
(
n_outs
)]
for
arg
in
states_out
:
if
arg
is
None
:
rval
.
append
(
to_add
)
else
:
rval
.
append
(
arg
+
to_add
)
states_out
=
rval
pure_outs
=
[
to_add
**
2
for
x
in
xrange
(
n_outputs
)]
else
:
shared_outs
=
[
sh
*
5
for
sh
in
shared_vars
]
states_out
=
[
x
for
x
in
states_out
]
pure_outs
=
[
2
for
x
in
xrange
(
n_outputs
)]
return
states_out
+
pure_outs
,
dict
(
zip
(
shared_vars
,
return
states_out
+
pure_outs
,
dict
(
zip
(
shared_vars
,
shared_outs
))
shared_outs
))
...
@@ -130,16 +146,25 @@ class TestScan(unittest.TestCase):
...
@@ -130,16 +146,25 @@ class TestScan(unittest.TestCase):
"""
"""
# Check if you need to go back in time over the sequences (the
# Check if you need to go back in time over the sequences (the
# first argument is n_steps, the second is go_backwards)
# first argument is n_steps, the second is go_backwards)
n
_
steps
=
args
[
0
]
nsteps
=
args
[
0
]
invert
=
False
invert
=
False
if
n_steps
<
0
or
args
[
1
]:
if
args
[
1
]:
nsteps
=
nsteps
*
-
1
if
nsteps
<
0
:
new_ins
=
[
x
[::
-
1
]
for
x
in
args
[
2
:
2
+
n_ins
]]
new_ins
=
[
x
[::
-
1
]
for
x
in
args
[
2
:
2
+
n_ins
]]
n_steps
=
abs
(
n_steps
)
else
:
new_ins
=
[
x
for
x
in
args
[
2
:
2
+
n_ins
]]
nsteps
=
abs
(
nsteps
)
# Simplify the inputs by slicing them according to the taps
# Simplify the inputs by slicing them according to the taps
nw_inputs
=
[]
nw_inputs
=
[]
for
inp
,
info
in
zip
(
new_ins
,
inputs_info
):
for
inp
,
info
in
zip
(
new_ins
,
inputs_info
):
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
nw_inputs
+=
[
inp
[
abs
(
numpy
.
min
(
taps
))
+
k
:]
for
k
in
taps
]
if
numpy
.
min
(
taps
)
<
0
:
_offset
=
abs
(
numpy
.
min
(
taps
))
else
:
_offset
=
0
nw_inputs
+=
[
inp
[
_offset
+
k
:]
for
k
in
taps
]
# Simplify the states by slicing them according to the taps.
# Simplify the states by slicing them according to the taps.
# Note that if the memory buffer for the inputs and outputs is
# Note that if the memory buffer for the inputs and outputs is
# the same, by changing the outputs we also change the outputs
# the same, by changing the outputs we also change the outputs
...
@@ -148,16 +173,23 @@ class TestScan(unittest.TestCase):
...
@@ -148,16 +173,23 @@ class TestScan(unittest.TestCase):
for
st
,
info
in
zip
(
args
[
2
+
n_ins
:
2
+
n_ins
+
n_states
],
for
st
,
info
in
zip
(
args
[
2
+
n_ins
:
2
+
n_ins
+
n_states
],
states_info
):
states_info
):
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
membuf
=
numpy
.
zeros
((
n_steps
+
numpy
.
max
(
abs
(
taps
)),
4
))
membuf
[:
numpy
.
max
(
abs
(
taps
))]
=
st
[:
numpy
.
max
(
abs
(
taps
))]
membuf
=
numpy
.
zeros
((
nsteps
+
abs
(
numpy
.
min
(
taps
)),
4
))
nw_states_inputs
+=
[
membuf
[
numpy
.
max
(
abs
(
taps
))
+
k
:]
if
abs
(
numpy
.
min
(
taps
))
!=
1
:
membuf
[:
abs
(
numpy
.
min
(
taps
))]
=
st
[:
abs
(
numpy
.
min
(
taps
))]
else
:
membuf
[:
abs
(
numpy
.
min
(
taps
))]
=
st
nw_states_inputs
+=
[
membuf
[
abs
(
numpy
.
min
(
taps
))
+
k
:]
for
k
in
taps
]
for
k
in
taps
]
nw_states_outs
.
append
(
membuf
[
numpy
.
max
(
abs
(
taps
)):])
nw_states_outs
.
append
(
membuf
[
abs
(
numpy
.
min
(
taps
)):])
paramters
=
args
[
2
+
n_ins
+
n_states
:]
parameters_vals
=
args
[
2
+
n_ins
+
n_states
:]
out_mem_buffers
=
[
numpy
.
zeros
((
n_steps
,
4
))
for
k
in
n_outs
]
out_mem_buffers
=
[
numpy
.
zeros
((
nsteps
,
4
))
for
k
in
xrange
(
n_outputs
)]
shared_values
=
[
x
.
copy
()
for
x
in
original_shared_values
]
shared_values
=
[
x
.
copy
()
for
x
in
original_shared_values
]
for
step
in
xrange
(
n_steps
):
for
step
in
xrange
(
nsteps
):
arg_pos
=
0
arg_pos
=
0
to_add
=
None
to_add
=
None
for
in_info
in
inputs_info
:
for
in_info
in
inputs_info
:
...
@@ -170,29 +202,35 @@ class TestScan(unittest.TestCase):
...
@@ -170,29 +202,35 @@ class TestScan(unittest.TestCase):
to_add
=
arg
*
2
to_add
=
arg
*
2
else
:
else
:
to_add
=
to_add
+
arg
*
2
to_add
=
to_add
+
arg
*
2
states_out
=
[
to_add
]
*
n_states
arg_pos
=
0
arg_pos
=
0
for
dx
,
st_info
in
enumerate
(
states_info
):
for
dx
,
st_info
in
enumerate
(
states_info
):
if
to_add
is
not
None
:
nw_states_outs
[
dx
][
step
]
=
to_add
nw_states_outs
[
dx
][
step
]
=
to_add
for
info
in
st_info
:
for
info
in
st_info
:
arg
=
nw_states_inputs
[
arg_pos
][
step
]
arg
=
nw_states_inputs
[
arg_pos
][
step
]
arg_pos
+=
1
arg_pos
+=
1
if
info
[
'use'
]:
if
info
[
'use'
]:
nw_states_outs
[
dx
][
step
]
+=
arg
*
3
nw_states_outs
[
dx
][
step
]
+=
arg
*
3
for
arg
,
info
in
zip
(
parameters
,
param
ters_info
):
for
arg
,
info
in
zip
(
parameters
_vals
,
parame
ters_info
):
if
info
[
'use'
]:
if
info
[
'use'
]:
if
to_add
is
None
:
if
to_add
is
None
:
to_add
=
arg
*
4
to_add
=
arg
*
4
else
:
else
:
to_add
=
to_add
+
arg
*
4
to_add
=
to_add
+
arg
*
4
if
to_add
is
not
None
:
shared_values
=
[
sh
*
5
+
to_add
for
sh
in
shared_values
]
shared_values
=
[
sh
*
5
+
to_add
for
sh
in
shared_values
]
for
state
in
nw_states_outs
:
for
state
in
nw_states_outs
:
state
[
step
]
+=
to_add
state
[
step
]
+=
to_add
for
out
in
out_mem_buffers
:
for
out
in
out_mem_buffers
:
out
[
step
]
=
to_add
**
2
out
[
step
]
=
to_add
**
2
else
:
shared_values
=
[
sh
*
5
for
sh
in
shared_values
]
for
out
in
out_mem_buffers
:
out
[
step
]
=
2
return
nw_states_outs
+
out_mem_buffers
,
shared_values
return
nw_states_outs
+
out_mem_buffers
,
shared_values
possible_n_steps
=
[
-
1
,
1
,
5
,
-
5
]
if
n_ins
>
0
:
possible_n_steps
.
append
(
None
)
for
n_steps
in
[
-
1
,
1
,
5
,
-
5
,
None
]:
for
n_steps
in
[
-
1
,
1
,
5
,
-
5
,
None
]:
for
go_backwards
in
[
True
,
False
]:
for
go_backwards
in
[
True
,
False
]:
outputs
,
updates
=
scan_module
.
scan
(
outputs
,
updates
=
scan_module
.
scan
(
...
@@ -209,30 +247,48 @@ class TestScan(unittest.TestCase):
...
@@ -209,30 +247,48 @@ class TestScan(unittest.TestCase):
allow_input_downcast
=
True
)
allow_input_downcast
=
True
)
if
n_steps
is
not
None
and
abs
(
n_steps
)
==
1
:
if
n_steps
is
not
None
and
abs
(
n_steps
)
==
1
:
assert
len
([
x
for
x
in
my_f
.
maker
.
env
.
toposort
()
all_nodes
=
my_f
.
maker
.
env
.
toposort
()
if
isinstance
(
x
.
op
,
scan_module
.
scan_op
.
ScanOp
)])
==
0
assert
len
([
x
for
x
in
all_nodes
if
isinstance
(
x
.
op
,
ScanOp
)])
==
0
print
>>
sys
.
stderr
,
' n_steps'
,
n_steps
print
>>
sys
.
stderr
,
' go_backwards'
,
go_backwards
print
>>
sys
.
stderr
,
' Scenario 1. Correct shape'
if
n_steps
is
not
None
:
_n_steps
=
n_steps
else
:
_n_steps
=
8
# Generating data
# Generating data
# Scenario 1 : Good fit shapes
# Scenario 1 : Good fit shapes
inputs
_values
=
[]
input
_values
=
[]
for
info
in
inputs_info
:
for
info
in
inputs_info
:
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
offset
=
abs
(
numpy
.
min
([
x
for
x
in
taps
if
x
<
0
]))
offset
=
0
if
len
([
x
for
x
in
taps
if
x
<
0
])
>
0
:
offset
+=
abs
(
numpy
.
min
([
x
for
x
in
taps
if
x
<
0
]))
if
len
([
x
for
x
in
taps
if
x
>
0
])
>
0
:
offset
+=
numpy
.
max
([
x
for
x
in
taps
if
x
>
0
])
offset
+=
numpy
.
max
([
x
for
x
in
taps
if
x
>
0
])
data
=
rng
.
uniform
(
size
=
(
n_steps
+
offset
,
4
))
data
=
rng
.
uniform
(
size
=
(
abs
(
_n_steps
)
+
offset
,
4
))
inputs
_values
.
append
(
data
)
input
_values
.
append
(
data
)
state_values
=
[]
state_values
=
[]
for
info
in
states_info
:
for
info
in
states_info
:
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
offset
=
abs
(
numpy
.
min
(
taps
))
offset
=
abs
(
numpy
.
min
(
taps
))
if
offset
>
1
:
data
=
rng
.
uniform
(
size
=
(
offset
,
4
))
data
=
rng
.
uniform
(
size
=
(
offset
,
4
))
else
:
data
=
rng
.
uniform
(
size
=
(
4
,))
data
=
numpy
.
arange
(
4
)
state_values
.
append
(
data
)
state_values
.
append
(
data
)
param_values
=
[
rng
.
uniform
(
size
=
(
4
,))
for
k
in
param_values
=
[
rng
.
uniform
(
size
=
(
4
,))
for
k
in
xrange
(
n_parameters
)]
xrange
(
n_parameters
)]
param_values
=
[
numpy
.
arange
(
4
)
for
k
in
xrange
(
n_parameters
)]
for
var
,
val
in
zip
(
shared_vars
,
original_shared_values
):
for
var
,
val
in
zip
(
shared_vars
,
original_shared_values
):
var
.
set_value
(
val
)
var
.
set_value
(
val
)
theano_outs
=
my_f
(
*
(
inputs
_values
+
state_values
+
theano_outs
=
my_f
(
*
(
input
_values
+
state_values
+
param_values
))
param_values
))
args
=
([
n_steps
,
go_backwards
]
+
args
=
([
_
n_steps
,
go_backwards
]
+
input_values
+
input_values
+
state_values
+
state_values
+
param_values
)
param_values
)
...
@@ -241,30 +297,48 @@ class TestScan(unittest.TestCase):
...
@@ -241,30 +297,48 @@ class TestScan(unittest.TestCase):
assert
len
(
numpy_outs
)
==
len
(
theano_outs
)
assert
len
(
numpy_outs
)
==
len
(
theano_outs
)
assert
len
(
numpy_shared
)
==
len
(
shared_vars
)
assert
len
(
numpy_shared
)
==
len
(
shared_vars
)
for
th_out
,
num_out
in
zip
(
theano_outs
,
numpy_outs
):
for
th_out
,
num_out
in
zip
(
theano_outs
,
numpy_outs
):
try
:
assert
numpy
.
allclose
(
th_out
,
num_out
)
assert
numpy
.
allclose
(
th_out
,
num_out
)
for
th_out
,
num_out
in
zip
(
shared_outs
,
numpy_shared
):
except
:
import
ipdb
;
ipdb
.
set_trace
()
for
th_out
,
num_out
in
zip
(
shared_vars
,
numpy_shared
):
try
:
assert
numpy
.
allclose
(
th_out
.
get_value
(),
num_out
)
assert
numpy
.
allclose
(
th_out
.
get_value
(),
num_out
)
except
:
import
ipdb
;
ipdb
.
set_trace
()
# Scenario 2 : Loose fit (sequences longer then required)
# Scenario 2 : Loose fit (sequences longer then required)
inputs_values
=
[]
print
>>
sys
.
stderr
,
' Scenario 2. Loose shapes'
input_values
=
[]
for
pos
,
info
in
enumerate
(
inputs_info
):
for
pos
,
info
in
enumerate
(
inputs_info
):
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
offset
=
abs
(
numpy
.
min
([
x
for
x
in
taps
if
x
<
0
]))
offset
=
0
if
len
([
x
for
x
in
taps
if
x
<
0
])
>
0
:
offset
+=
abs
(
numpy
.
min
([
x
for
x
in
taps
if
x
<
0
]))
if
len
([
x
for
x
in
taps
if
x
>
0
])
>
0
:
offset
+=
numpy
.
max
([
x
for
x
in
taps
if
x
>
0
])
offset
+=
numpy
.
max
([
x
for
x
in
taps
if
x
>
0
])
data
=
rng
.
uniform
(
size
=
(
n_steps
+
offset
+
pos
+
1
,
4
))
if
n_steps
is
not
None
:
inputs_values
.
append
(
data
)
# loose inputs make sense only when n_steps is
# defined
data
=
rng
.
uniform
(
size
=
(
abs
(
_n_steps
)
+
offset
+
pos
+
1
,
4
))
else
:
data
=
rng
.
uniform
(
size
=
(
abs
(
_n_steps
)
+
offset
,
4
))
input_values
.
append
(
data
)
state_values
=
[]
state_values
=
[]
for
pos
,
info
in
enumerate
(
states_info
):
for
pos
,
info
in
enumerate
(
states_info
):
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
offset
=
abs
(
numpy
.
min
(
taps
))
offset
=
abs
(
numpy
.
min
(
taps
))
if
offset
>
1
:
data
=
rng
.
uniform
(
size
=
(
offset
+
pos
+
1
,
4
))
data
=
rng
.
uniform
(
size
=
(
offset
+
pos
+
1
,
4
))
else
:
data
=
rng
.
uniform
(
size
=
(
4
,))
state_values
.
append
(
data
)
state_values
.
append
(
data
)
param_values
=
[
rng
.
uniform
(
size
=
(
4
,))
for
k
in
param_values
=
[
rng
.
uniform
(
size
=
(
4
,))
for
k
in
xrange
(
n_parameters
)]
xrange
(
n_parameters
)]
for
var
,
val
in
zip
(
shared_vars
,
original_shared_values
):
for
var
,
val
in
zip
(
shared_vars
,
original_shared_values
):
var
.
set_value
(
val
)
var
.
set_value
(
val
)
theano_outs
=
my_f
(
*
(
inputs
_values
+
state_values
+
theano_outs
=
my_f
(
*
(
input
_values
+
state_values
+
param_values
))
param_values
))
args
=
([
n_steps
,
go_backwards
]
+
args
=
([
_
n_steps
,
go_backwards
]
+
input_values
+
input_values
+
state_values
+
state_values
+
param_values
)
param_values
)
...
@@ -272,18 +346,23 @@ class TestScan(unittest.TestCase):
...
@@ -272,18 +346,23 @@ class TestScan(unittest.TestCase):
numpy_outs
,
numpy_shared
=
rvals
numpy_outs
,
numpy_shared
=
rvals
assert
len
(
numpy_outs
)
==
len
(
theano_outs
)
assert
len
(
numpy_outs
)
==
len
(
theano_outs
)
assert
len
(
numpy_shared
)
==
len
(
shared_vars
)
assert
len
(
numpy_shared
)
==
len
(
shared_vars
)
for
th_out
,
num_out
in
zip
(
theano_outs
,
numpy_outs
):
for
th_out
,
num_out
in
zip
(
theano_outs
,
numpy_outs
):
assert
numpy
.
allclose
(
th_out
,
num_out
)
assert
numpy
.
allclose
(
th_out
,
num_out
)
for
th_out
,
num_out
in
zip
(
shared_out
s
,
numpy_shared
):
for
th_out
,
num_out
in
zip
(
shared_var
s
,
numpy_shared
):
assert
numpy
.
allclose
(
th_out
.
get_value
(),
num_out
)
assert
numpy
.
allclose
(
th_out
.
get_value
(),
num_out
)
# Scenario 3 : Less data then required
# Scenario 3 : Less data then required
inputs_values
=
[]
print
>>
sys
.
stderr
,
' Scenario 2. Wrong shapes'
input_values
=
[]
for
pos
,
info
in
enumerate
(
inputs_info
):
for
pos
,
info
in
enumerate
(
inputs_info
):
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
offset
=
abs
(
numpy
.
min
([
x
for
x
in
taps
if
x
<
0
]))
offset
=
0
if
len
([
x
for
x
in
taps
if
x
<
0
])
>
0
:
offset
+=
abs
(
numpy
.
min
([
x
for
x
in
taps
if
x
<
0
]))
if
len
([
x
for
x
in
taps
if
x
>
0
])
>
0
:
offset
+=
numpy
.
max
([
x
for
x
in
taps
if
x
>
0
])
offset
+=
numpy
.
max
([
x
for
x
in
taps
if
x
>
0
])
data
=
rng
.
uniform
(
size
=
(
n_steps
+
offset
-
1
,
4
))
data
=
rng
.
uniform
(
size
=
(
abs
(
_n_steps
)
+
offset
-
1
,
4
))
inputs
_values
.
append
(
data
)
input
_values
.
append
(
data
)
state_values
=
[]
state_values
=
[]
for
pos
,
info
in
enumerate
(
states_info
):
for
pos
,
info
in
enumerate
(
states_info
):
taps
=
[
x
[
'tap'
]
for
x
in
info
]
taps
=
[
x
[
'tap'
]
for
x
in
info
]
...
@@ -297,7 +376,7 @@ class TestScan(unittest.TestCase):
...
@@ -297,7 +376,7 @@ class TestScan(unittest.TestCase):
self
.
assertRaises
(
Exception
,
my_f
,
self
.
assertRaises
(
Exception
,
my_f
,
inputs
+
state_values
+
param_values
)
inputs
+
state_values
+
param_values
)
def
test00
0
_generate_tests
(
self
):
def
test00
1
_generate_tests
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
all_inputs_info
=
[[]]
all_inputs_info
=
[[]]
possible_taps_use_pairs
=
[[
dict
(
tap
=
0
,
use
=
True
)],
possible_taps_use_pairs
=
[[
dict
(
tap
=
0
,
use
=
True
)],
...
@@ -320,11 +399,13 @@ class TestScan(unittest.TestCase):
...
@@ -320,11 +399,13 @@ class TestScan(unittest.TestCase):
dict
(
tap
=
3
,
use
=
True
)],
dict
(
tap
=
3
,
use
=
True
)],
[
dict
(
tap
=-
2
,
use
=
True
),
[
dict
(
tap
=-
2
,
use
=
True
),
dict
(
tap
=
3
,
use
=
True
)]]
dict
(
tap
=
3
,
use
=
True
)]]
test_nb
=
0
for
n_ins
in
[
1
,
2
]:
for
n_ins
in
[
1
,
2
]:
# Randomly pick up 4*n_ins combinations of arguments
# Randomly pick up 4*n_ins combinations of arguments
for
k
in
xrange
(
4
*
n_ins
):
for
k
in
xrange
(
4
*
n_ins
):
inp
=
[]
inp
=
[]
for
inp_nb
in
xrange
(
n_ins
):
for
inp_nb
in
xrange
(
n_ins
):
pos
=
rng
.
randint
(
len
(
possible_taps_use_pairs
))
pos
=
rng
.
randint
(
len
(
possible_taps_use_pairs
))
inp
.
append
(
possible_taps_use_pairs
[
pos
])
inp
.
append
(
possible_taps_use_pairs
[
pos
])
all_inputs_info
.
append
(
inp
)
all_inputs_info
.
append
(
inp
)
...
@@ -356,13 +437,26 @@ class TestScan(unittest.TestCase):
...
@@ -356,13 +437,26 @@ class TestScan(unittest.TestCase):
[
dict
(
use
=
True
)],
[
dict
(
use
=
True
)],
[
dict
(
use
=
True
),
dict
(
use
=
True
)],
[
dict
(
use
=
True
),
dict
(
use
=
True
)],
[
dict
(
use
=
True
),
dict
(
use
=
False
)]]
[
dict
(
use
=
True
),
dict
(
use
=
False
)]]
# This generates errors related to some unfixed bug in the current
# version of scan
# The test will also have to be changesd following some further
# restriction of scan and reduction of the number of corner cases
return
for
n_outputs
in
[
0
,
1
,
2
]:
for
n_outputs
in
[
0
,
1
,
2
]:
for
n_shared_updates
in
[
0
,
1
,
2
]:
for
n_shared_updates
in
[
0
,
1
,
2
]:
for
n_random_combinations
in
xrange
(
1
4
):
for
n_random_combinations
in
xrange
(
1
):
pos_inp
=
rng
.
randint
(
len
(
all_inputs_info
))
pos_inp
=
rng
.
randint
(
len
(
all_inputs_info
))
pos_st
=
rng
.
randint
(
len
(
all_states_info
))
pos_st
=
rng
.
randint
(
len
(
all_states_info
))
pos_param
=
rng
.
randint
(
len
(
all_parameters_info
))
pos_param
=
rng
.
randint
(
len
(
all_parameters_info
))
print
>>
sys
.
stderr
print
>>
sys
.
stderr
,
'Test nb'
,
test_nb
print
>>
sys
.
stderr
,
' inputs'
,
all_inputs_info
[
pos_inp
]
print
>>
sys
.
stderr
,
' states'
,
all_states_info
[
pos_st
]
print
>>
sys
.
stderr
,
' parameters'
,
\
all_parameters_info
[
pos_param
]
print
>>
sys
.
stderr
,
' n_outputs'
,
n_outputs
print
>>
sys
.
stderr
,
' n_shared_updates'
,
n_shared_updates
test_nb
+=
1
self
.
new_run
(
inputs_info
=
all_inputs_info
[
pos_inp
],
self
.
new_run
(
inputs_info
=
all_inputs_info
[
pos_inp
],
states_info
=
all_states_info
[
pos_st
],
states_info
=
all_states_info
[
pos_st
],
parameters_info
=
all_parameters_info
[
pos_param
],
parameters_info
=
all_parameters_info
[
pos_param
],
...
@@ -371,7 +465,7 @@ class TestScan(unittest.TestCase):
...
@@ -371,7 +465,7 @@ class TestScan(unittest.TestCase):
def
test00
1
_generator_one_scalar_output
(
self
):
def
test00
2
_generator_one_scalar_output
(
self
):
def
f_pow2
(
x_tm1
):
def
f_pow2
(
x_tm1
):
return
2
*
x_tm1
return
2
*
x_tm1
...
@@ -401,13 +495,14 @@ class TestScan(unittest.TestCase):
...
@@ -401,13 +495,14 @@ class TestScan(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
# are vectors, weights are scalars
def
test00
2
_one_sequence_one_output_and_weights
(
self
):
def
test00
3
_one_sequence_one_output_and_weights
(
self
):
def
f_rnn
(
u_t
,
x_tm1
,
W_in
,
W
):
def
f_rnn
(
u_t
,
x_tm1
,
W_in
,
W
):
return
u_t
*
W_in
+
x_tm1
*
W
return
u_t
*
W_in
+
x_tm1
*
W
u
=
theano
.
tensor
.
vector
(
'u'
)
u
=
theano
.
tensor
.
vector
(
'u'
)
x0
=
theano
.
tensor
.
scalar
(
'x0'
)
x0
=
theano
.
tensor
.
scalar
(
'x0'
)
W_in
=
theano
.
tensor
.
scalar
(
'win'
)
W_in
=
theano
.
tensor
.
scalar
(
'win'
)
W
=
theano
.
tensor
.
scalar
(
'w'
)
W
=
theano
.
tensor
.
scalar
(
'w'
)
n_steps
=
5
output
,
updates
=
scan_module
.
scan
(
f_rnn
,
output
,
updates
=
scan_module
.
scan
(
f_rnn
,
u
,
u
,
x0
,
x0
,
...
@@ -448,14 +543,14 @@ class TestScan(unittest.TestCase):
...
@@ -448,14 +543,14 @@ class TestScan(unittest.TestCase):
theano_values
=
my_f
(
v_u
,
v_x0
,
W_in
,
W
)
theano_values
=
my_f
(
v_u
,
v_x0
,
W_in
,
W
)
assert
numpy
.
allclose
(
theano_values
,
v_out
)
assert
numpy
.
allclose
(
theano_values
,
v_out
)
def
test00
3
_multiple_inputs_multiple_outputs
(
self
):
def
test00
4
_multiple_inputs_multiple_outputs
(
self
):
pass
pass
def
test00
4
_collect_parameters_outer_graph
(
self
):
def
test00
5
_collect_parameters_outer_graph
(
self
):
pass
pass
def
test00
5
_multiple_taps
(
self
):
def
test00
6
_multiple_taps
(
self
):
pass
pass
def
test00
6
_updates
(
self
):
def
test00
7
_updates
(
self
):
pass
pass
theano/sandbox/scan_module/tests/test_utils.py
浏览文件 @
bc2f50e5
...
@@ -166,7 +166,7 @@ def grab_scan_node(output):
...
@@ -166,7 +166,7 @@ def grab_scan_node(output):
class
TestScanUtils
(
unittest
.
TestCase
):
class
TestScanUtils
(
unittest
.
TestCase
):
def
test_cloning_no_replace_strict_copy_inputs
(
self
):
def
test
001
_cloning_no_replace_strict_copy_inputs
(
self
):
# This has nothing to do with scan, but it refers to the clone
# This has nothing to do with scan, but it refers to the clone
# function that scan uses internally and that pfunc uses now and
# function that scan uses internally and that pfunc uses now and
# that users might want to use
# that users might want to use
...
@@ -185,7 +185,7 @@ class TestScanUtils(unittest.TestCase):
...
@@ -185,7 +185,7 @@ class TestScanUtils(unittest.TestCase):
assert
x
in
f2_inp
assert
x
in
f2_inp
assert
y
in
f2_inp
assert
y
in
f2_inp
def
test_cloning_no_replace_strict_not_copy_inputs
(
self
):
def
test
002
_cloning_no_replace_strict_not_copy_inputs
(
self
):
# This has nothing to do with scan, but it refers to the clone
# This has nothing to do with scan, but it refers to the clone
# function that scan uses internally and that pfunc uses now and
# function that scan uses internally and that pfunc uses now and
# that users might want to use
# that users might want to use
...
@@ -204,7 +204,7 @@ class TestScanUtils(unittest.TestCase):
...
@@ -204,7 +204,7 @@ class TestScanUtils(unittest.TestCase):
assert
not
x
in
f2_inp
assert
not
x
in
f2_inp
assert
not
y
in
f2_inp
assert
not
y
in
f2_inp
def
test_cloning_replace_strict_copy_inputs
(
self
):
def
test
003
_cloning_replace_strict_copy_inputs
(
self
):
# This has nothing to do with scan, but it refers to the clone
# This has nothing to do with scan, but it refers to the clone
# function that scan uses internally and that pfunc uses now and
# function that scan uses internally and that pfunc uses now and
# that users might want to use
# that users might want to use
...
@@ -223,7 +223,7 @@ class TestScanUtils(unittest.TestCase):
...
@@ -223,7 +223,7 @@ class TestScanUtils(unittest.TestCase):
assert
x
in
f2_inp
assert
x
in
f2_inp
assert
y2
in
f2_inp
assert
y2
in
f2_inp
def
test_cloning_replace_not_strict_copy_inputs
(
self
):
def
test
004
_cloning_replace_not_strict_copy_inputs
(
self
):
# This has nothing to do with scan, but it refers to the clone
# This has nothing to do with scan, but it refers to the clone
# function that scan uses internally and that pfunc uses now and
# function that scan uses internally and that pfunc uses now and
# that users might want to use
# that users might want to use
...
@@ -242,7 +242,7 @@ class TestScanUtils(unittest.TestCase):
...
@@ -242,7 +242,7 @@ class TestScanUtils(unittest.TestCase):
assert
x
in
f2_inp
assert
x
in
f2_inp
assert
y2
in
f2_inp
assert
y2
in
f2_inp
def
test_cloning_replace_strict_not_copy_inputs
(
self
):
def
test
005
_cloning_replace_strict_not_copy_inputs
(
self
):
# This has nothing to do with scan, but it refers to the clone
# This has nothing to do with scan, but it refers to the clone
# function that scan uses internally and that pfunc uses now and
# function that scan uses internally and that pfunc uses now and
# that users might want to use
# that users might want to use
...
@@ -261,7 +261,7 @@ class TestScanUtils(unittest.TestCase):
...
@@ -261,7 +261,7 @@ class TestScanUtils(unittest.TestCase):
assert
not
x
in
f2_inp
assert
not
x
in
f2_inp
assert
not
y2
in
f2_inp
assert
not
y2
in
f2_inp
def
test_cloning_replace_not_strict_not_copy_inputs
(
self
):
def
test
006
_cloning_replace_not_strict_not_copy_inputs
(
self
):
# This has nothing to do with scan, but it refers to the clone
# This has nothing to do with scan, but it refers to the clone
# function that scan uses internally and that pfunc uses now and
# function that scan uses internally and that pfunc uses now and
# that users might want to use
# that users might want to use
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论