Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a9553206
提交
a9553206
authored
7月 15, 2014
作者:
abergeron
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1965 from nouiz/tile
Opt that remove useless tile
上级
51964e4e
05314d67
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
72 行增加
和
1 行删除
+72
-1
basic.py
theano/tensor/basic.py
+4
-1
opt.py
theano/tensor/opt.py
+38
-0
test_opt.py
theano/tensor/tests/test_opt.py
+30
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
a9553206
...
...
@@ -3999,6 +3999,9 @@ class Tile(Op):
def
__hash__
(
self
):
return
hash
(
Tile
)
^
hash
(
self
.
ndim
)
def
__str__
(
self
):
return
self
.
__class__
.
__name__
+
"{ndim=
%
d}"
%
self
.
ndim
def
make_node
(
self
,
x
,
reps
):
x
=
as_tensor_variable
(
x
)
reps
=
as_tensor_variable
(
reps
)
...
...
@@ -4054,7 +4057,7 @@ def tile(x, reps, ndim=None):
assert
python_all
([
int
(
i
)
==
i
for
i
in
iter
(
reps
)])
except
(
TypeError
,
AssertionError
):
raise
ValueError
(
"reps argument to tile must be a constant (e.g. "
"tuple, list of integers)"
)
"tuple, list of integers)"
)
if
len
(
reps
)
!=
x
.
ndim
:
raise
ValueError
(
"len(reps) != x.ndim not currently supported"
)
elif
(
ndim
is
not
None
)
and
ndim
!=
x
.
ndim
:
...
...
theano/tensor/opt.py
浏览文件 @
a9553206
...
...
@@ -2463,6 +2463,44 @@ def local_div_switch_sink(node):
return
False
#############
# Tile Opts #
#############
@register_canonicalize
@register_stabilize
@gof.local_optimizer
([
T
.
Tile
])
def
local_useless_tile
(
node
):
"""Tile(x, (1,)*N) -> x
This is useless tile. (1,)*N, just mean a vector with all element
being 1.
"""
if
isinstance
(
node
.
op
,
T
.
Tile
):
try
:
a
=
T
.
get_scalar_constant_value
(
node
.
inputs
[
1
])
if
a
==
1
:
try
:
l
=
T
.
get_vector_length
(
node
.
inputs
[
1
])
if
l
==
node
.
inputs
[
0
]
.
ndim
:
return
[
node
.
inputs
[
0
]]
elif
l
<
node
.
inputs
[
0
]
.
ndim
:
# The Op don't support that case, so we can't
# implement the opt and test it.
return
return
[
node
.
inputs
[
0
]]
else
:
# The Op don't support that case, so we can't
# implement the opt and test it.
return
x_nd
=
node
.
inputs
[
0
]
.
ndim
broad
=
[
'x'
]
*
(
l
-
x_nd
)
+
range
(
x_nd
)
return
[
node
.
inputs
[
0
]
.
dimshuffle
(
broad
)]
except
ValueError
:
return
except
NotScalarConstantError
:
return
################
# Flatten Opts #
################
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
a9553206
...
...
@@ -2883,6 +2883,36 @@ def test_local_mul_specialize():
assert
nodes
==
[
T
.
mul
]
class
T_Tile
(
unittest
.
TestCase
):
def
test_local_useless_tile
(
self
):
v
=
T
.
vector
()
m
=
T
.
matrix
()
for
var
,
data
in
[(
v
,
[
1
,
2
,
3
]),
(
m
,
[[
1
,
2
],
[
3
,
4
]])]:
# Currently, only a repeat patter == ndim is supported.
for
ndim
in
[
var
.
ndim
]:
# range(1, var.ndim):
f
=
theano
.
function
([
var
],
T
.
tile
(
var
,
(
1
,)
*
ndim
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
compile
.
DeepCopyOp
)
# If the repeat parameter is longer then v.ndim, we must
# replace it with a DimShuffle to add the extra parameter.
# But it isn't supported for now, so assert that we raise an
# error.
self
.
assertRaises
(
ValueError
,
T
.
tile
,
v
,
(
1
,)
*
(
v
.
ndim
+
1
))
# If the repeat parameter is shorter then m.ndim, it should
# pad tot he left the repeat patter with 1. It is not supported for now.
#f = theano.function([var], T.tile(v, (1,)*(v.ndim+1)))
#topo = f.maker.fgraph.toposort()
#assert len(topo) == 1
#assert isinstance(topo[0].op, DimShuffe)
self
.
assertRaises
(
ValueError
,
T
.
tile
,
m
,
(
1
,)
*
(
m
.
ndim
-
1
))
#f = theano.function([var], T.tile(m, (1,)*(m.ndim-1)))
#topo = f.maker.fgraph.toposort()
#assert len(topo) == 1
#assert isinstance(topo[0].op, compile.DeepCopyOp)
def
speed_local_pow_specialize_range
():
val
=
numpy
.
random
.
rand
(
1e7
)
v
=
T
.
vector
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论