Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
65c61c15
提交
65c61c15
authored
3月 27, 2015
作者:
abergeron
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2647 from kelvinxu/remove_useless_slice
CCW remove useless slice
上级
9c4ed643
941ca01d
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
61 行增加
和
0 行删除
+61
-0
opt.py
theano/tensor/opt.py
+27
-0
test_opt.py
theano/tensor/tests/test_opt.py
+34
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
65c61c15
...
@@ -1919,6 +1919,33 @@ def local_set_to_inc_subtensor(node):
...
@@ -1919,6 +1919,33 @@ def local_set_to_inc_subtensor(node):
return
[
advanced_inc_subtensor1
(
node
.
inputs
[
0
],
other
,
node
.
inputs
[
2
])]
return
[
advanced_inc_subtensor1
(
node
.
inputs
[
0
],
other
,
node
.
inputs
[
2
])]
@register_canonicalize
@register_specialize
@gof.local_optimizer
([
Subtensor
])
def
local_useless_slice
(
node
):
"""
Remove Subtensor of the form X[0, :] -> X[0]
"""
if
isinstance
(
node
.
op
,
Subtensor
):
slices
=
get_idx_list
(
node
.
inputs
,
node
.
op
.
idx_list
)
last_slice
=
len
(
slices
)
for
s
in
slices
[::
-
1
]:
# check if slice and then check slice indices
if
(
isinstance
(
s
,
slice
)
and
s
.
start
is
None
and
s
.
stop
is
None
and
(
s
.
step
is
None
or
T
.
extract_constant
(
s
.
step
)
==
1
)):
last_slice
-=
1
else
:
break
# check if we removed something
if
last_slice
<
len
(
slices
):
subtens
=
Subtensor
(
slices
[:
last_slice
])
sl_ins
=
Subtensor
.
collapse
(
slices
[:
last_slice
],
lambda
x
:
isinstance
(
x
,
T
.
Variable
))
out
=
subtens
(
node
.
inputs
[
0
],
*
sl_ins
)
return
[
out
]
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
@gof.local_optimizer
([
Subtensor
,
AdvancedSubtensor1
])
@gof.local_optimizer
([
Subtensor
,
AdvancedSubtensor1
])
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
65c61c15
...
@@ -1576,6 +1576,40 @@ def test_log_add():
...
@@ -1576,6 +1576,40 @@ def test_log_add():
#TODO: (write and) test that the optimization works with Sum in addition to working with Add.
#TODO: (write and) test that the optimization works with Sum in addition to working with Add.
def
test_local_useless_slice
():
# test a simple matrix
x
=
tensor
.
matrix
(
'x'
)
mode_unopt
=
compile
.
get_default_mode
()
.
excluding
(
"local_useless_slice"
)
mode_opt
=
compile
.
get_default_mode
()
.
including
(
"local_useless_slice"
)
# test with and without the useless slice
o
=
2
*
x
[
0
,
:]
f_unopt
=
theano
.
function
([
x
],
o
,
mode
=
mode_unopt
)
f_opt
=
theano
.
function
([
x
],
o
,
mode
=
mode_opt
)
test_inp
=
numpy
.
random
.
randint
(
-
10
,
10
,
(
4
,
4
))
.
astype
(
'float32'
)
assert
all
(
f_opt
(
test_inp
)
==
f_unopt
(
test_inp
)),
\
"The optimization caused a mismatch in the result"
# test to see if the slice is truely gone
apply_node
=
f_opt
.
maker
.
fgraph
.
toposort
()[
0
]
subtens
=
apply_node
.
op
assert
not
any
(
isinstance
(
idx
,
slice
)
for
idx
in
subtens
.
idx_list
),
"Slice should be gone"
# test a 4d tensor
z
=
tensor
.
tensor4
(
'z'
)
o2
=
z
[
1
,
:,
:,
1
]
o3
=
z
[
0
,
:,
:,
:]
f_opt_check
=
theano
.
function
([
z
],
o2
,
mode
=
mode_opt
)
f_opt_check_apply
=
theano
.
function
([
z
],
o3
,
mode
=
mode_opt
)
# The optimization shouldn't apply here
apply_node
=
f_opt_check
.
maker
.
fgraph
.
toposort
()[
0
]
subtens
=
apply_node
.
op
assert
[
isinstance
(
idx
,
slice
)
for
idx
in
subtens
.
idx_list
]
.
count
(
True
)
==
2
# But it should here
apply_node
=
f_opt_check_apply
.
maker
.
fgraph
.
toposort
()[
0
]
subtens
=
apply_node
.
op
assert
not
any
(
isinstance
(
idx
,
slice
)
for
idx
in
subtens
.
idx_list
)
def
test_local_useless_inc_subtensor
():
def
test_local_useless_inc_subtensor
():
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
y
=
tensor
.
matrix
(
'y'
)
y
=
tensor
.
matrix
(
'y'
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论