Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a51c2b88
提交
a51c2b88
authored
2月 08, 2011
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
lift subtensor taked on elemwise in more case. Test it. Don't lift if the…
lift subtensor taked on elemwise in more case. Test it. Don't lift if the elemwise is used by other computation.
上级
f8927951
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
103 行增加
和
4 行删除
+103
-4
opt.py
theano/tensor/opt.py
+31
-3
test_opt.py
theano/tensor/tests/test_opt.py
+72
-1
没有找到文件。
theano/tensor/opt.py
浏览文件 @
a51c2b88
...
...
@@ -1035,17 +1035,45 @@ def local_upcast_elemwise_constant_inputs(node):
@register_canonicalize
@gof.local_optimizer
([])
def
local_subtensor_
unary
(
node
):
def
local_subtensor_
lift
(
node
):
"""
unary(x)[idx] -> unary(x[idx])
unary(x)[idx] -> unary(x[idx])#any broadcast pattern.
elemwise(x,...)[idx] -> elemwise(x[idx],...)
when x,... are broadcasted scalar or not broadcasted at all
"""
if
isinstance
(
node
.
op
,
T
.
Subtensor
):
u
=
node
.
inputs
[
0
]
if
u
.
owner
and
isinstance
(
u
.
owner
.
op
,
T
.
Elemwise
)
and
len
(
u
.
owner
.
inputs
)
==
1
:
if
not
u
.
owner
or
len
(
u
.
clients
)
>
1
:
return
False
if
isinstance
(
u
.
owner
.
op
,
T
.
Elemwise
)
and
len
(
u
.
owner
.
inputs
)
==
1
:
idx
=
node
.
inputs
[
1
:]
x_idx
=
node
.
op
(
u
.
owner
.
inputs
[
0
],
*
idx
)
return
[
u
.
owner
.
op
(
x_idx
)]
if
isinstance
(
u
.
owner
.
op
,
T
.
Elemwise
):
new_inputs
=
[]
if
all
([
sum
(
i
.
type
.
broadcastable
)
==
0
for
i
in
u
.
owner
.
inputs
]):
# There is no broadcastable in the inputs
idx
=
node
.
inputs
[
1
:]
new_inputs
=
[
node
.
op
(
i
,
*
idx
)
for
i
in
u
.
owner
.
inputs
]
return
[
u
.
owner
.
op
(
*
new_inputs
)]
elif
all
([
sum
(
i
.
type
.
broadcastable
)
in
[
i
.
ndim
,
0
]
for
i
in
u
.
owner
.
inputs
]):
# There is no broadcastable in the inputs or it is scalar
idx
=
node
.
inputs
[
1
:]
new_inputs
=
[]
for
i
in
u
.
owner
.
inputs
:
if
sum
(
i
.
type
.
broadcastable
)
==
0
:
new_inputs
.
append
(
node
.
op
(
i
,
*
idx
))
else
:
# If the subtensor remove some dims, we must
# lower the number of dimensions of this scalar.
if
node
.
outputs
[
0
]
.
ndim
==
i
.
ndim
:
new_inputs
.
append
(
i
)
else
:
new_inputs
.
append
(
i
.
dimshuffle
(
'x'
*
node
.
outputs
[
0
]
.
ndim
))
return
[
u
.
owner
.
op
(
*
new_inputs
)]
@register_canonicalize
@gof.local_optimizer
([
None
])
def
local_IncSubtensor_serialize
(
node
):
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
a51c2b88
...
...
@@ -1130,7 +1130,7 @@ def test_log_add():
#TODO: (write and) test that the optimization works with Sum in addition to working with Add.
class
test_local_subtensor_
unary
(
unittest
.
TestCase
):
class
test_local_subtensor_
lift
(
unittest
.
TestCase
):
def
test0
(
self
):
# basic test that the Op works
...
...
@@ -1143,10 +1143,66 @@ class test_local_subtensor_unary(unittest.TestCase):
prog
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
prog
[
0
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
prog
[
1
]
.
op
==
TT
.
exp
assert
len
(
prog
)
==
2
f
([[
0
,
1
],[
2
,
3
]])
# let debugmode test something
def
test0b
(
self
):
# as test0, but we reuse the output of the elemwise
# So we should not lift the subtensor
x
=
TT
.
matrix
(
'x'
)
f
=
function
([
x
],
[
TT
.
exp
(
x
)[
0
],
TT
.
exp
(
x
)],
mode
=
mode_opt
)
prog
=
f
.
maker
.
env
.
toposort
()
assert
prog
[
0
]
.
op
==
TT
.
exp
assert
isinstance
(
prog
[
1
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
isinstance
(
prog
[
2
]
.
op
,
theano
.
compile
.
function_module
.
DeepCopyOp
)
assert
len
(
prog
)
==
3
f
([[
0
,
1
],[
2
,
3
]])
# let debugmode test something
def
test1
(
self
):
# basic test that the optimization work with scalar broadcasted
x
=
TT
.
matrix
(
'x'
)
y
=
TT
.
scalar
(
'y'
)
z
=
TT
.
matrix
(
'z'
)
f
=
function
([
x
,
y
,
z
],
TT
.
exp
(
x
+
y
+
z
)[
0
],
mode
=
mode_opt
)
prog
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
prog
[
1
]
.
op
,
TT
.
DimShuffle
)
assert
isinstance
(
prog
[
0
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
isinstance
(
prog
[
2
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
isinstance
(
prog
[
3
]
.
op
.
scalar_op
,
theano
.
scalar
.
Composite
)
#Composite{add,add}
assert
len
(
prog
)
==
4
f
([[
0
,
1
],[
2
,
3
]],
4
,
[[
4
,
5
],[
6
,
7
]])
# let debugmode test something
def
test2
(
self
):
# as 1, but take a slice
x
=
TT
.
matrix
(
'x'
)
y
=
TT
.
scalar
(
'y'
)
z
=
TT
.
matrix
(
'z'
)
f
=
function
([
x
,
y
,
z
],
TT
.
exp
(
x
+
y
+
z
)[
0
:
2
],
mode
=
mode_opt
)
prog
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
prog
[
1
]
.
op
,
TT
.
DimShuffle
)
assert
isinstance
(
prog
[
0
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
isinstance
(
prog
[
2
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
isinstance
(
prog
[
3
]
.
op
.
scalar_op
,
theano
.
scalar
.
Composite
)
#Composite{add,add}
assert
len
(
prog
)
==
4
f
([[
0
,
1
],[
2
,
3
]],
4
,
[[
4
,
5
],[
6
,
7
]])
# let debugmode test something
def
test3
(
self
):
# basic test that the optimization does work with broadcasting
# for unary elemwise.
y
=
TT
.
vector
(
'y'
)
f
=
function
([
y
],
TT
.
exp
(
y
.
dimshuffle
(
0
,
'x'
))[
0
],
mode
=
mode_opt
)
prog
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
prog
[
0
]
.
op
,
TT
.
DimShuffle
)
assert
isinstance
(
prog
[
1
]
.
op
,
TT
.
Subtensor
)
assert
prog
[
2
]
.
op
==
TT
.
exp
assert
len
(
prog
)
==
3
f
([
4
,
5
])
# let debugmode test something
def
test4
(
self
):
# basic test that the optimization doesn't work with broadcasting
# ... It *could* be extended to,
# ... but right now it doesn't, so it shouldn't try.
...
...
@@ -1163,7 +1219,22 @@ class test_local_subtensor_unary(unittest.TestCase):
assert
prog
[
1
]
.
op
==
TT
.
add
assert
isinstance
(
prog
[
2
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
prog
[
3
]
.
op
==
inplace
.
exp_inplace
assert
len
(
prog
)
==
4
f
([[
0
,
1
],[
2
,
3
]],
[
4
,
5
])
# let debugmode test something
def
test5
(
self
):
# test that we don't lift when we reuse the output of the
# elemwise for other computation.
x
=
TT
.
matrix
(
'x'
)
y
=
TT
.
vector
(
'y'
)
f
=
function
([
x
,
y
],
[
TT
.
exp
(
x
+
y
)[
0
],
TT
.
exp
(
x
+
y
)
+
x
],
mode
=
mode_opt
)
prog
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
prog
[
0
]
.
op
,
TT
.
DimShuffle
)
assert
isinstance
(
prog
[
1
]
.
op
.
scalar_op
,
theano
.
scalar
.
Composite
)
#Composite{add,exp}
assert
prog
[
2
]
.
op
==
TT
.
add
assert
isinstance
(
prog
[
3
]
.
op
,
TT
.
Subtensor
)
#first subtensor
assert
len
(
prog
)
==
4
f
([[
0
,
1
],[
2
,
3
]],
[
4
,
5
])
# let debugmode test something
def
test_local_fill_useless
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论