Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3e574bba
提交
3e574bba
authored
8月 30, 2014
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add local_useless_inc_subtensor.
Remove the IncSubtensor when we can infer all the shapes are the same.
上级
09a4d5ed
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
77 行增加
和
0 行删除
+77
-0
opt.py
theano/tensor/opt.py
+42
-0
test_opt.py
theano/tensor/tests/test_opt.py
+35
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
3e574bba
...
@@ -1688,6 +1688,48 @@ def local_upcast_elemwise_constant_inputs(node):
...
@@ -1688,6 +1688,48 @@ def local_upcast_elemwise_constant_inputs(node):
##################
##################
@register_canonicalize
@register_specialize
@gof.local_optimizer
([
IncSubtensor
])
def
local_useless_inc_subtensor
(
node
):
"""Remove IncSubtensor, when we overwrite the full inputs with the
new value.
"""
if
not
isinstance
(
node
.
op
,
IncSubtensor
):
return
if
node
.
op
.
set_instead_of_inc
is
False
:
# This is an IncSubtensor, so the init value must be zeros
try
:
c
=
get_scalar_constant_value
(
node
.
inputs
[
0
])
if
c
!=
0
:
return
except
NotScalarConstantError
:
return
if
(
node
.
inputs
[
0
]
.
ndim
!=
node
.
inputs
[
1
]
.
ndim
or
node
.
inputs
[
0
]
.
broadcastable
!=
node
.
inputs
[
1
]
.
broadcastable
):
# FB: I didn't check if this case can happen, but this opt
# don't support it.
return
# We have a SetSubtensor or an IncSubtensor on zeros
# If is this IncSubtensor useful?
# Check that we keep all the original data.
if
all
(
isinstance
(
e
,
slice
)
and
e
.
start
is
None
and
e
.
stop
is
None
and
e
.
step
is
None
for
e
in
node
.
op
.
idx_list
):
assert
len
(
node
.
inputs
)
==
2
# IncSubtensor broadcast node.inputs[1] on node.inputs[0]
# based on run time shapes, so we must check they are the same.
if
not
hasattr
(
node
.
fgraph
,
'shape_feature'
):
return
if
not
node
.
fgraph
.
shape_feature
.
same_shape
(
node
.
inputs
[
0
],
node
.
inputs
[
1
]):
return
# They are the same shape, so we can remore this IncSubtensor
return
node
.
inputs
[
1
]
@register_canonicalize
@register_canonicalize
@register_specialize
@register_specialize
@gof.local_optimizer
([
Subtensor
])
@gof.local_optimizer
([
Subtensor
])
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
3e574bba
...
@@ -1571,6 +1571,41 @@ def test_log_add():
...
@@ -1571,6 +1571,41 @@ 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_inc_subtensor
():
x
=
tensor
.
matrix
(
'x'
)
y
=
tensor
.
matrix
(
'y'
)
o
=
tensor
.
set_subtensor
(
x
[::,
::],
y
)
o_shape
=
tensor
.
set_subtensor
(
x
[::,
::],
tensor
.
specify_shape
(
y
,
x
.
shape
))
f_shape
=
theano
.
function
([
x
,
y
],
o_shape
)
f
=
theano
.
function
([
x
,
y
],
o
)
# Test with shape info
topo
=
f_shape
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
5
,
topo
assert
not
isinstance
(
topo
[
-
1
]
.
op
,
tensor
.
IncSubtensor
)
out
=
f_shape
([[
2
,
3
]],
[[
3
,
4
]])
assert
(
out
==
[[
3
,
4
]])
.
all
()
# Test that without shape info, we don't apply the opt.
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
tensor
.
IncSubtensor
)
out
=
f
([[
2
,
3
]],
[[
3
,
4
]])
assert
(
out
==
[[
3
,
4
]])
.
all
()
# Test that we don't remove shape error
try
:
f
([[
2
,
3
]],
[[
3
,
4
],
[
4
,
5
]])
assert
False
except
(
ValueError
,
AssertionError
):
pass
# Test that we don't remove broadcastability
out
=
f
([[
2
,
3
],
[
3
,
4
]],
[[
5
,
6
]])
assert
(
out
==
[[
5
,
6
],
[
5
,
6
]])
.
all
()
def
test_local_useless_subtensor
():
def
test_local_useless_subtensor
():
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论