Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2e5fcea2
提交
2e5fcea2
authored
12月 17, 2012
作者:
lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1054 from pascanur/new_optimizations_scan
New optimizations scan
上级
351b4edf
2a03db63
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
88 行增加
和
24 行删除
+88
-24
scan_op.py
theano/scan_module/scan_op.py
+7
-7
scan_opt.py
theano/scan_module/scan_opt.py
+0
-0
scan_utils.py
theano/scan_module/scan_utils.py
+3
-6
test_scan.py
theano/scan_module/tests/test_scan.py
+78
-11
没有找到文件。
theano/scan_module/scan_op.py
浏览文件 @
2e5fcea2
...
@@ -1375,18 +1375,18 @@ class Scan(PureOp):
...
@@ -1375,18 +1375,18 @@ class Scan(PureOp):
def
compute_gradient
(
y
,
g_y
):
def
compute_gradient
(
y
,
g_y
):
if
'int'
in
str
(
g_y
.
dtype
):
if
'int'
in
str
(
g_y
.
dtype
):
raise
TypeError
(
"Gradients may never be integers but g_y "
raise
TypeError
(
"Gradients may never be integers but g_y "
"has type "
+
str
(
g_y
.
type
))
"has type "
+
str
(
g_y
.
type
))
wrt
=
[
x
for
x
in
theano
.
gof
.
graph
.
inputs
([
y
])
wrt
=
[
x
for
x
in
theano
.
gof
.
graph
.
inputs
([
y
])
if
x
in
diff_inputs
]
if
x
in
diff_inputs
]
grads
=
gradient
.
grad
(
grads
=
gradient
.
grad
(
cost
=
None
,
cost
=
None
,
known_grads
=
{
y
:
g_y
},
known_grads
=
{
y
:
g_y
},
wrt
=
wrt
,
consider_constant
=
wrt
,
wrt
=
wrt
,
consider_constant
=
wrt
,
disconnected_inputs
=
'ignore'
,
disconnected_inputs
=
'ignore'
,
return_disconnected
=
'None'
)
return_disconnected
=
'None'
)
gmp
=
dict
(
zip
(
wrt
,
grads
))
gmp
=
dict
(
zip
(
wrt
,
grads
))
rval
=
[
gmp
.
get
(
p
,
None
)
for
p
in
diff_inputs
]
rval
=
[
gmp
.
get
(
p
,
None
)
for
p
in
diff_inputs
]
return
rval
return
rval
dC_dinps_t
=
[
None
for
inp
in
diff_inputs
]
dC_dinps_t
=
[
None
for
inp
in
diff_inputs
]
disconnected_dC_dinps_t
=
[
True
for
inp
in
diff_inputs
]
disconnected_dC_dinps_t
=
[
True
for
inp
in
diff_inputs
]
...
@@ -1727,7 +1727,7 @@ class Scan(PureOp):
...
@@ -1727,7 +1727,7 @@ class Scan(PureOp):
node
=
outs
[
0
]
.
owner
node
=
outs
[
0
]
.
owner
for
idx
in
xrange
(
self
.
n_shared_outs
):
for
idx
in
xrange
(
self
.
n_shared_outs
):
disconnected
=
True
disconnected
=
True
connected_flags
=
self
.
connection_pattern
(
node
)[
idx
+
start
]
connected_flags
=
self
.
connection_pattern
(
node
)[
idx
+
start
]
for
dC_dout
,
connected
in
zip
(
dC_douts
,
connected_flags
):
for
dC_dout
,
connected
in
zip
(
dC_douts
,
connected_flags
):
if
(
not
isinstance
(
dC_dout
.
type
,
DisconnectedType
)
and
if
(
not
isinstance
(
dC_dout
.
type
,
DisconnectedType
)
and
connected
):
connected
):
...
...
theano/scan_module/scan_opt.py
浏览文件 @
2e5fcea2
差异被折叠。
点击展开。
theano/scan_module/scan_utils.py
浏览文件 @
2e5fcea2
...
@@ -191,8 +191,6 @@ def get_updates_and_outputs(ls):
...
@@ -191,8 +191,6 @@ def get_updates_and_outputs(ls):
this function know how to put it in that order?
this function know how to put it in that order?
"""
"""
def
is_outputs
(
elem
):
def
is_outputs
(
elem
):
if
(
isinstance
(
elem
,
(
list
,
tuple
))
and
if
(
isinstance
(
elem
,
(
list
,
tuple
))
and
all
([
isinstance
(
x
,
theano
.
Variable
)
for
x
in
elem
])):
all
([
isinstance
(
x
,
theano
.
Variable
)
for
x
in
elem
])):
...
@@ -206,7 +204,7 @@ def get_updates_and_outputs(ls):
...
@@ -206,7 +204,7 @@ def get_updates_and_outputs(ls):
# Make sure the updates will be applied in a deterministic order
# Make sure the updates will be applied in a deterministic order
if
not
isinstance
(
elem
,
gof
.
python25
.
OrderedDict
):
if
not
isinstance
(
elem
,
gof
.
python25
.
OrderedDict
):
warnings
.
warn
(
"Expected OrderedDict or OrderedUpdates, got "
\
warnings
.
warn
(
"Expected OrderedDict or OrderedUpdates, got "
\
+
str
(
type
(
elem
))
+
". This can make your script non-"
+
str
(
type
(
elem
))
+
". This can make your script non-"
"deterministic."
)
"deterministic."
)
return
True
return
True
# Dictionaries can be given as lists of tuples
# Dictionaries can be given as lists of tuples
...
@@ -253,7 +251,6 @@ def get_updates_and_outputs(ls):
...
@@ -253,7 +251,6 @@ def get_updates_and_outputs(ls):
'values, you can use `tensor.constant` to turn them into '
'values, you can use `tensor.constant` to turn them into '
'Theano variables.'
)
'Theano variables.'
)
if
is_outputs
(
ls
):
if
is_outputs
(
ls
):
return
None
,
_list
(
ls
),
OrderedDict
()
return
None
,
_list
(
ls
),
OrderedDict
()
if
is_updates
(
ls
):
if
is_updates
(
ls
):
...
@@ -389,7 +386,7 @@ def equal_computations(xs, ys, in_xs=None, in_ys=None):
...
@@ -389,7 +386,7 @@ def equal_computations(xs, ys, in_xs=None, in_ys=None):
elif
(
isinstance
(
dx
,
tensor
.
Constant
)
and
elif
(
isinstance
(
dx
,
tensor
.
Constant
)
and
isinstance
(
dy
,
tensor
.
Constant
)):
isinstance
(
dy
,
tensor
.
Constant
)):
if
not
(
numpy
.
all
(
dx
.
data
==
dy
.
data
)
and
if
not
(
numpy
.
all
(
dx
.
data
==
dy
.
data
)
and
dx
.
dtype
==
dy
.
dtype
and
dx
.
type
.
dtype
==
dy
.
type
.
dtype
and
dx
.
data
.
shape
==
dy
.
data
.
shape
):
dx
.
data
.
shape
==
dy
.
data
.
shape
):
return
False
return
False
else
:
else
:
...
@@ -413,7 +410,7 @@ def equal_computations(xs, ys, in_xs=None, in_ys=None):
...
@@ -413,7 +410,7 @@ def equal_computations(xs, ys, in_xs=None, in_ys=None):
if
(
isinstance
(
dx
,
tensor
.
Constant
)
and
if
(
isinstance
(
dx
,
tensor
.
Constant
)
and
isinstance
(
dy
,
tensor
.
Constant
)):
isinstance
(
dy
,
tensor
.
Constant
)):
if
not
(
numpy
.
all
(
dx
.
data
==
dy
.
data
)
and
if
not
(
numpy
.
all
(
dx
.
data
==
dy
.
data
)
and
dx
.
dtype
==
dy
.
dtype
and
dx
.
type
.
dtype
==
dy
.
type
.
dtype
and
dx
.
data
.
shape
==
dy
.
data
.
shape
):
dx
.
data
.
shape
==
dy
.
data
.
shape
):
return
False
return
False
else
:
else
:
...
...
theano/scan_module/tests/test_scan.py
浏览文件 @
2e5fcea2
...
@@ -2346,9 +2346,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2346,9 +2346,7 @@ class T_Scan(unittest.TestCase):
# this new assert is here to test if scan_merging works ..
# this new assert is here to test if scan_merging works ..
nb_scan
=
len
([
n
for
n
in
topo
nb_scan
=
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)])
if
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)])
# For this to work we need an optimization that it will be pushed in
self
.
assertTrue
(
nb_scan
==
1
)
# a new pull request
self
.
assertTrue
(
nb_scan
==
2
)
nb_shape_i
=
len
([
n
for
n
in
topo
nb_shape_i
=
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
theano
.
tensor
.
opt
.
Shape_i
)])
if
isinstance
(
n
.
op
,
theano
.
tensor
.
opt
.
Shape_i
)])
if
theano
.
config
.
mode
!=
'FAST_COMPILE'
:
if
theano
.
config
.
mode
!=
'FAST_COMPILE'
:
...
@@ -2364,7 +2362,8 @@ class T_Scan(unittest.TestCase):
...
@@ -2364,7 +2362,8 @@ class T_Scan(unittest.TestCase):
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
y
])
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
y
])
f
=
theano
.
function
([
x
,
y
],
[
sx
,
sy
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
,
y
],
[
sx
,
sy
],
mode
=
mode_with_opt
.
excluding
(
'scanOp_pushout_seqs_ops'
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
scans
=
filter
(
lambda
n
:
isinstance
(
scans
=
filter
(
lambda
n
:
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
...
@@ -2373,7 +2372,8 @@ class T_Scan(unittest.TestCase):
...
@@ -2373,7 +2372,8 @@ class T_Scan(unittest.TestCase):
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
n_steps
=
2
)
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
n_steps
=
2
)
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
y
],
n_steps
=
3
)
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
y
],
n_steps
=
3
)
f
=
theano
.
function
([
x
,
y
],
[
sx
,
sy
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
,
y
],
[
sx
,
sy
],
mode
=
mode_with_opt
.
excluding
(
'scanOp_pushout_seqs_ops'
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
scans
=
filter
(
lambda
n
:
isinstance
(
scans
=
filter
(
lambda
n
:
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
...
@@ -2382,7 +2382,8 @@ class T_Scan(unittest.TestCase):
...
@@ -2382,7 +2382,8 @@ class T_Scan(unittest.TestCase):
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
n_steps
=
4
)
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
n_steps
=
4
)
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
y
],
n_steps
=
4
)
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
y
],
n_steps
=
4
)
f
=
theano
.
function
([
x
,
y
],
[
sx
,
sy
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
,
y
],
[
sx
,
sy
],
mode
=
mode_with_opt
.
excluding
(
'scanOp_pushout_seqs_ops'
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
scans
=
filter
(
lambda
n
:
isinstance
(
scans
=
filter
(
lambda
n
:
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
...
@@ -2391,7 +2392,8 @@ class T_Scan(unittest.TestCase):
...
@@ -2391,7 +2392,8 @@ class T_Scan(unittest.TestCase):
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
f
=
theano
.
function
([
x
],
[
sx
,
sy
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
],
[
sx
,
sy
],
mode
=
mode_with_opt
.
excluding
(
'scanOp_pushout_seqs_ops'
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
scans
=
filter
(
lambda
n
:
scans
=
filter
(
lambda
n
:
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
...
@@ -2401,7 +2403,7 @@ class T_Scan(unittest.TestCase):
...
@@ -2401,7 +2403,7 @@ class T_Scan(unittest.TestCase):
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
mode
=
'FAST_COMPILE'
)
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
mode
=
'FAST_COMPILE'
)
f
=
theano
.
function
([
x
],
[
sx
,
sy
],
f
=
theano
.
function
([
x
],
[
sx
,
sy
],
mode
=
mode_with_opt
)
mode
=
mode_with_opt
.
excluding
(
'scanOp_pushout_seqs_ops'
)
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
scans
=
filter
(
lambda
n
:
scans
=
filter
(
lambda
n
:
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
...
@@ -2410,7 +2412,8 @@ class T_Scan(unittest.TestCase):
...
@@ -2410,7 +2412,8 @@ class T_Scan(unittest.TestCase):
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
sx
,
upx
=
theano
.
scan
(
sum
,
sequences
=
[
x
])
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
truncate_gradient
=
1
)
sy
,
upy
=
theano
.
scan
(
sum
,
sequences
=
[
x
],
truncate_gradient
=
1
)
f
=
theano
.
function
([
x
],
[
sx
,
sy
],
mode
=
mode_with_opt
)
f
=
theano
.
function
([
x
],
[
sx
,
sy
],
mode
=
mode_with_opt
.
excluding
(
'scanOp_pushout_seqs_ops'
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
scans
=
filter
(
lambda
n
:
scans
=
filter
(
lambda
n
:
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
isinstance
(
n
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
),
topo
)
...
@@ -2820,12 +2823,12 @@ class T_Scan(unittest.TestCase):
...
@@ -2820,12 +2823,12 @@ class T_Scan(unittest.TestCase):
vx
=
numpy
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
=
numpy
.
zeros
((
50
,),
dtype
=
theano
.
config
.
floatX
)
vx
[
23
]
=
4
vx
[
23
]
=
4
out
,
out2
=
f
(
vx
)
out
,
out2
=
f
(
vx
)
print
'len_out'
,
len
(
out
)
assert
len
(
out
)
==
24
assert
len
(
out
)
==
24
assert
numpy
.
all
(
out2
==
vx
+
2
)
assert
numpy
.
all
(
out2
==
vx
+
2
)
lssc
=
[
x
for
x
in
f
.
maker
.
fgraph
.
toposort
()
lssc
=
[
x
for
x
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
x
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)]
if
isinstance
(
x
.
op
,
theano
.
scan_module
.
scan_op
.
Scan
)]
assert
len
(
lssc
)
==
2
# One scan node gets optimnized out
assert
len
(
lssc
)
==
1
@dec.knownfailureif
(
True
,
@dec.knownfailureif
(
True
,
(
"This test fails because not typed outputs_info "
(
"This test fails because not typed outputs_info "
...
@@ -3303,6 +3306,70 @@ class T_Scan(unittest.TestCase):
...
@@ -3303,6 +3306,70 @@ class T_Scan(unittest.TestCase):
theano
.
scan_module
.
scan_op
.
Scan
)]
theano
.
scan_module
.
scan_op
.
Scan
)]
assert
len
(
scan_nodes
)
==
1
assert
len
(
scan_nodes
)
==
1
def
test_eliminate_seqs
(
self
):
U
=
tensor
.
vector
(
'U'
)
sh
=
theano
.
shared
(
asarrayX
(
2.
))
x1
=
tensor
.
vector
(
'x1'
)
x2
=
tensor
.
scalar
(
'x2'
)
def
rec_fn
(
*
args
):
u_t
=
args
[
0
]
return
[(
u_t
+
1
,
# mitsot
u_t
+
2
,
# sitsot
u_t
+
3
),
# nitsot
{
sh
:
u_t
+
4
}]
# shared
[
X1
,
X2
,
X3
],
updates
=
theano
.
scan
(
rec_fn
,
U
,
[
dict
(
initial
=
x1
,
taps
=
[
-
1
,
-
3
]),
x2
,
None
],
n_steps
=
None
,
truncate_gradient
=-
1
,
go_backwards
=
False
)
f
=
theano
.
function
([
U
,
x1
,
x2
],
[
X1
,
X2
,
X3
],
updates
=
updates
,
mode
=
theano
.
Mode
(
linker
=
'py'
),
allow_input_downcast
=
True
)
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_u
=
asarrayX
(
rng
.
uniform
(
size
=
(
5
,)))
outs
=
f
(
v_u
,
[
0
,
0
,
0
],
0
)
assert
numpy
.
allclose
(
outs
[
0
],
v_u
+
1
)
assert
numpy
.
allclose
(
outs
[
1
],
v_u
+
2
)
assert
numpy
.
allclose
(
outs
[
2
],
v_u
+
3
)
assert
numpy
.
allclose
(
sh
.
get_value
(),
v_u
[
-
1
]
+
4
)
def
test_eliminate_nonseqs
(
self
):
W
=
tensor
.
scalar
(
'W'
)
sh
=
theano
.
shared
(
asarrayX
(
2.
))
x1
=
tensor
.
vector
(
'x1'
)
x2
=
tensor
.
scalar
(
'x2'
)
def
rec_fn
(
*
args
):
w
=
args
[
-
1
]
return
[(
w
+
1.
,
# mitsot
w
+
2.
,
# sitsot
w
+
3.
),
# nitsot
{
sh
:
w
+
4.
}]
# shared
[
X1
,
X2
,
X3
],
updates
=
theano
.
scan
(
rec_fn
,
[],
[
dict
(
initial
=
x1
,
taps
=
[
-
1
,
-
3
]),
x2
,
None
],
W
,
n_steps
=
5
,
truncate_gradient
=-
1
,
go_backwards
=
False
)
f
=
theano
.
function
([
W
,
x1
,
x2
],
[
X1
,
X2
,
X3
],
updates
=
updates
,
mode
=
theano
.
Mode
(
linker
=
'py'
),
allow_input_downcast
=
True
)
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
v_w
=
asarrayX
(
rng
.
uniform
())
outs
=
f
(
v_w
,
[
0
,
0
,
0
],
0
)
assert
numpy
.
allclose
(
outs
[
0
],
v_w
+
1
)
assert
numpy
.
allclose
(
outs
[
1
],
v_w
+
2
)
assert
numpy
.
allclose
(
outs
[
2
],
v_w
+
3
)
assert
numpy
.
allclose
(
sh
.
get_value
(),
v_w
+
4
)
def
test_speed
():
def
test_speed
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论