Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e846ef90
提交
e846ef90
authored
3月 15, 2011
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
'optimize var[int1::][:int2] to only 1 subtensor.'
上级
addbc07c
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
2 行删除
+81
-2
opt.py
theano/tensor/opt.py
+30
-2
test_opt.py
theano/tensor/tests/test_opt.py
+51
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
e846ef90
...
...
@@ -1113,9 +1113,13 @@ def local_subtensor_lift(node):
@gof.local_optimizer
([])
def
local_subtensor_merge
(
node
):
"""
1) var[int:][-1] -> var[-1]
1) var[int:][-1] -> var[-1]
# a little different for when the first subtensor is empty.
2) var[::-1][int] -> var[-int-1]
3) var[::-1][:int] -> var[:-int-1:-1]
4) var[int1::][:int2] -> var[int1:switch(idx1>=0,
idx1,
maximum(u.owner.inputs[0].shape[0]+idx1, 0)
) + idx2]
"""
if
(
isinstance
(
node
.
op
,
T
.
Subtensor
)
and
...
...
@@ -1188,7 +1192,7 @@ def local_subtensor_merge(node):
len
(
u
.
owner
.
op
.
idx_list
)
==
1
and
isinstance
(
node
.
op
.
idx_list
[
0
],
slice
)
and
node
.
op
.
idx_list
[
0
]
.
start
in
[
0
,
None
]
and
#node.op.idx_list[0].stop is None
and
isinstance
(
node
.
op
.
idx_list
[
0
]
.
stop
,
(
int
,
scalar
.
basic
.
Scalar
))
and
node
.
op
.
idx_list
[
0
]
.
step
is
None
and
isinstance
(
u
.
owner
.
op
.
idx_list
[
0
],
slice
)
and
u
.
owner
.
op
.
idx_list
[
0
]
.
start
is
None
and
...
...
@@ -1207,6 +1211,30 @@ def local_subtensor_merge(node):
return
[
u
.
owner
.
inputs
[
0
][:
-
idx
-
1
:
-
1
]]
# var[int1::][:int2]
if
(
len
(
node
.
inputs
)
in
[
1
,
2
]
and
isinstance
(
node
.
op
.
idx_list
[
0
],
slice
)
and
node
.
op
.
idx_list
[
0
]
.
start
in
[
0
,
None
]
and
isinstance
(
node
.
op
.
idx_list
[
0
]
.
stop
,(
int
,
scalar
.
basic
.
Scalar
))
and
node
.
op
.
idx_list
[
0
]
.
step
is
None
and
len
(
u
.
owner
.
op
.
idx_list
)
==
1
and
isinstance
(
u
.
owner
.
op
.
idx_list
[
0
],
slice
)
and
isinstance
(
u
.
owner
.
op
.
idx_list
[
0
]
.
start
,(
int
,
scalar
.
basic
.
Scalar
))
and
u
.
owner
.
op
.
idx_list
[
0
]
.
stop
in
[
sys
.
maxint
,
None
]
and
u
.
owner
.
op
.
idx_list
[
0
]
.
step
is
None
):
idx1
=
u
.
owner
.
op
.
idx_list
[
0
]
.
start
idx2
=
node
.
op
.
idx_list
[
0
]
.
stop
if
isinstance
(
idx1
,
scalar
.
basic
.
Scalar
):
idx1
=
T
.
tensor_from_scalar
(
u
.
owner
.
inputs
[
1
])
if
isinstance
(
idx2
,
scalar
.
basic
.
Scalar
):
idx2
=
T
.
tensor_from_scalar
(
node
.
inputs
[
1
])
# The maximum is needed to don't have shape[0] - idx1 < 0
idx2_neg
=
T
.
maximum
(
u
.
owner
.
inputs
[
0
]
.
shape
[
0
]
+
idx1
,
0
)
new_idx2
=
T
.
switch
(
idx1
>=
0
,
idx1
,
idx2_neg
)
+
idx2
return
[
u
.
owner
.
inputs
[
0
][
idx1
:
new_idx2
]]
@register_canonicalize
@gof.local_optimizer
([
None
])
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
e846ef90
...
...
@@ -1429,6 +1429,57 @@ class test_local_subtensor_merge(unittest.TestCase):
assert
isinstance
(
topo
[
2
]
.
op
,
theano
.
compile
.
function_module
.
DeepCopyOp
)
f
([[
0
,
1
],[
2
,
3
]])
# let debugmode test something
def
test_const4
(
self
):
# var[const1::][:const2]
x
=
TT
.
matrix
(
'x'
)
x_val
=
[[
0
,
1
],[
2
,
3
]]
for
idx1
in
range
(
-
3
,
3
):
for
idx2
in
range
(
-
3
,
3
):
f
=
function
([
x
],
x
[
idx1
:][:
idx2
],
mode
=
mode_opt
)
#theano.printing.debugprint(f, print_type=True)
topo
=
f
.
maker
.
env
.
toposort
()
#print [t for t in topo if isinstance(t.op, TT.Subtensor)]
assert
len
([
t
for
t
in
topo
if
isinstance
(
t
.
op
,
TT
.
Subtensor
)])
==
1
#print topo[-1].op
assert
isinstance
(
topo
[
-
1
]
.
op
,
theano
.
compile
.
function_module
.
DeepCopyOp
)
f
(
x_val
)
# let debugmode test something
def
test_scalar4
(
self
):
# var[int1:][:int2]
x
=
TT
.
matrix
(
'x'
)
y
=
TT
.
iscalar
(
'y'
)
z
=
TT
.
iscalar
(
'y'
)
f
=
function
([
x
,
y
,
z
],
x
[
y
:][:
z
],
mode
=
mode_opt
)
#theano.printing.debugprint(f, print_type=True)
topo
=
f
.
maker
.
env
.
toposort
()
#print [t for t in topo if isinstance(t.op, TT.Subtensor)]
assert
len
([
t
for
t
in
topo
if
isinstance
(
t
.
op
,
TT
.
Subtensor
)])
==
1
#print topo[-1].op
assert
isinstance
(
topo
[
-
1
]
.
op
,
theano
.
compile
.
function_module
.
DeepCopyOp
)
x_val
=
[[
0
,
1
],[
2
,
3
]]
for
idx1
in
range
(
-
5
,
5
):
for
idx2
in
range
(
-
5
,
5
):
f
(
x_val
,
idx1
,
idx2
)
# let debugmode test something
def
test_dont_opt4
(
self
):
# Test that we don't optimize some case
# var[int1:][:int2] should be optimized but not
# x[::other int][const]
x
=
TT
.
matrix
(
'x'
)
f
=
function
([
x
],
x
[
-
2
:
0
][:
0
],
mode
=
mode_opt
)
theano
.
printing
.
debugprint
(
f
)
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
3
assert
isinstance
(
topo
[
0
]
.
op
,
TT
.
Subtensor
)
assert
isinstance
(
topo
[
1
]
.
op
,
TT
.
Subtensor
)
assert
isinstance
(
topo
[
2
]
.
op
,
theano
.
compile
.
function_module
.
DeepCopyOp
)
f
([[
0
,
1
],[
2
,
3
]])
# let debugmode test something
def
test_local_fill_useless
():
m
=
theano
.
config
.
mode
if
m
==
'FAST_COMPILE'
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论