Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bfde042c
提交
bfde042c
authored
10月 24, 2016
作者:
Frédéric Bastien
提交者:
GitHub
10月 24, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5098 from olimastro/ccw4647
New optimization: DimShuffle(Alloc(...)) -> Alloc(..., use dims of 1 for new dimensions) #5024
上级
83d5709a
d9fe1b74
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
52 行增加
和
2 行删除
+52
-2
opt_uncanonicalize.py
theano/tensor/opt_uncanonicalize.py
+28
-1
test_opt_uncanonicalize.py
theano/tensor/tests/test_opt_uncanonicalize.py
+24
-1
没有找到文件。
theano/tensor/opt_uncanonicalize.py
浏览文件 @
bfde042c
...
@@ -110,7 +110,7 @@ def local_alloc_dimshuffle(node):
...
@@ -110,7 +110,7 @@ def local_alloc_dimshuffle(node):
Alloc(DimShuffle(x), ...) - > Alloc(x, ...)
Alloc(DimShuffle(x), ...) - > Alloc(x, ...)
"""
"""
if
node
.
op
==
T
.
alloc
:
if
isinstance
(
node
.
op
,
T
.
Alloc
)
:
input_
=
node
.
inputs
[
0
]
input_
=
node
.
inputs
[
0
]
if
input_
.
owner
and
isinstance
(
input_
.
owner
.
op
,
DimShuffle
):
if
input_
.
owner
and
isinstance
(
input_
.
owner
.
op
,
DimShuffle
):
# check if it only adds dimension to the left
# check if it only adds dimension to the left
...
@@ -146,3 +146,30 @@ def local_reshape_dimshuffle(node):
...
@@ -146,3 +146,30 @@ def local_reshape_dimshuffle(node):
offset
+=
1
offset
+=
1
return
[
T
.
reshape
(
input_
.
owner
.
inputs
[
0
],
node
.
inputs
[
1
])]
return
[
T
.
reshape
(
input_
.
owner
.
inputs
[
0
],
node
.
inputs
[
1
])]
return
False
return
False
@register_uncanonicalize
@gof.local_optimizer
([
T
.
DimShuffle
])
def
local_dimshuffle_alloc
(
node
):
"""
If an alloc is inside a dimshuffle which only adds dimension to the left,
scrap the dimshuffle and adds 1 into the alloc
dimshuffle{x, 0, 1}(alloc([3 4], 3, 2) => alloc([3 4], 1, 3, 2)
"""
if
isinstance
(
node
.
op
,
T
.
DimShuffle
)
and
node
.
inputs
[
0
]
.
owner
:
input_
=
node
.
inputs
[
0
]
if
isinstance
(
input_
.
owner
.
op
,
T
.
Alloc
):
# check if it only adds dimension to the left
new_order
=
node
.
op
.
new_order
expected_new_order
=
(
'x'
,)
*
(
len
(
new_order
)
-
input_
.
ndim
)
+
\
tuple
(
range
(
input_
.
ndim
))
if
new_order
!=
expected_new_order
:
return
False
# count numbers of 'x'
nb_new_dims
=
len
(
new_order
)
-
input_
.
ndim
new_shape_input
=
(
1
,)
*
nb_new_dims
+
tuple
(
input_
.
owner
.
inputs
[
1
:])
return
[
T
.
alloc
(
input_
.
owner
.
inputs
[
0
],
*
new_shape_input
)]
return
False
theano/tensor/tests/test_opt_uncanonicalize.py
浏览文件 @
bfde042c
...
@@ -10,7 +10,8 @@ from theano.gof import FunctionGraph
...
@@ -10,7 +10,8 @@ from theano.gof import FunctionGraph
from
theano.gof.opt
import
out2in
from
theano.gof.opt
import
out2in
from
theano.tensor.opt_uncanonicalize
import
(
from
theano.tensor.opt_uncanonicalize
import
(
local_alloc_dimshuffle
,
local_alloc_dimshuffle
,
local_reshape_dimshuffle
local_reshape_dimshuffle
,
local_dimshuffle_alloc
,
)
)
import
theano.tensor
as
tensor
import
theano.tensor
as
tensor
#from theano.tensor import matrix,max_and_argmax,MaaxAndArgmax,neg
#from theano.tensor import matrix,max_and_argmax,MaaxAndArgmax,neg
...
@@ -145,3 +146,25 @@ def test_local_reshape_dimshuffle():
...
@@ -145,3 +146,25 @@ def test_local_reshape_dimshuffle():
topo
=
g
.
toposort
()
topo
=
g
.
toposort
()
assert
any
([
not
isinstance
(
x
,
DimShuffle
)
for
x
in
topo
])
assert
any
([
not
isinstance
(
x
,
DimShuffle
)
for
x
in
topo
])
def
test_local_reshape_dimshuffle
():
reshape_dimshuffle
=
out2in
(
local_dimshuffle_alloc
)
x
=
tensor
.
vector
(
'x'
)
out
=
tensor
.
alloc
(
x
,
3
,
2
)
.
dimshuffle
(
'x'
,
'x'
,
0
,
1
)
g
=
FunctionGraph
([
x
],
[
out
])
reshape_dimshuffle
(
g
)
l
=
theano
.
gof
.
PerformLinker
()
l
.
accept
(
g
)
f
=
l
.
make_function
()
assert
f
([
3
,
4
])
.
ndim
==
4
topo
=
g
.
toposort
()
assert
any
([
not
isinstance
(
x
,
DimShuffle
)
for
x
in
topo
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论