Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9ec45737
提交
9ec45737
authored
1月 29, 2014
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
local_mul_specialize now don't introcude neg node when this will add more node then needed.
上级
ea3dcffc
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
35 行增加
和
10 行删除
+35
-10
opt.py
theano/tensor/opt.py
+28
-9
test_opt.py
theano/tensor/tests/test_opt.py
+7
-1
没有找到文件。
theano/tensor/opt.py
浏览文件 @
9ec45737
...
@@ -3600,7 +3600,15 @@ def local_pow_specialize_device(node):
...
@@ -3600,7 +3600,15 @@ def local_pow_specialize_device(node):
@gof.local_optimizer
([
T
.
mul
])
@gof.local_optimizer
([
T
.
mul
])
def
local_mul_specialize
(
node
):
def
local_mul_specialize
(
node
):
"""Remove special-case constants from mul arguments
"""Remove special-case constants from mul arguments and useless neg in inputs.
mul(-1, x) -> neg(x)
mul(1, x, y) -> mul(x, y)
mul(0, ...) -> alloc(0, shapes...)
This is not done if we would add more nodes in the graph, like with:
mul(-1, x, y) -/-> neg(mul(x, y))
"""
"""
# here, we are past the point of canonicalization, so we don't
# here, we are past the point of canonicalization, so we don't
# want to put in un-necessary fills.
# want to put in un-necessary fills.
...
@@ -3610,19 +3618,23 @@ def local_mul_specialize(node):
...
@@ -3610,19 +3618,23 @@ def local_mul_specialize(node):
#the idea here is that we have pow(x, y)
#the idea here is that we have pow(x, y)
neg
=
False
neg
=
False
new_inputs
=
[]
new_inputs
=
[]
nb_neg_node
=
0
nb_cst
=
0
for
input
in
node
.
inputs
:
for
input
in
node
.
inputs
:
# remove any neg arguments
# remove any neg arguments
while
input
.
owner
and
input
.
owner
.
op
==
T
.
neg
:
while
input
.
owner
and
input
.
owner
.
op
==
T
.
neg
:
neg
^=
True
neg
^=
True
input
=
input
.
owner
.
inputs
[
0
]
input
=
input
.
owner
.
inputs
[
0
]
nb_neg_node
+=
1
# remove special case arguments of 1, -1 or 0
# remove special case arguments of 1, -1 or 0
y
=
local_mul_canonizer
.
get_constant
(
input
)
y
=
local_mul_canonizer
.
get_constant
(
input
)
if
N
.
all
(
y
==
1.0
):
if
y
==
1.0
:
continue
nb_cst
+=
1
elif
N
.
all
(
y
==
-
1.0
):
elif
y
==
-
1.0
:
nb_cst
+=
1
neg
^=
True
# toggles
neg
^=
True
# toggles
elif
N
.
all
(
y
==
0.0
)
:
elif
y
==
0.0
:
# if we find any zero, we just return right away
# if we find any zero, we just return right away
return
[
broadcast_like
(
0
,
node
.
outputs
[
0
],
node
.
fgraph
)]
return
[
broadcast_like
(
0
,
node
.
outputs
[
0
],
node
.
fgraph
)]
else
:
else
:
...
@@ -3636,10 +3648,17 @@ def local_mul_specialize(node):
...
@@ -3636,10 +3648,17 @@ def local_mul_specialize(node):
else
:
else
:
rval
=
new_inputs
[
0
]
rval
=
new_inputs
[
0
]
else
:
else
:
if
neg
:
# The next case would cause a replace by an equivalent case.
rval
=
-
T
.
mul
(
*
new_inputs
)
if
(
neg
and
else
:
nb_neg_node
==
0
and
rval
=
T
.
mul
(
*
new_inputs
)
nb_cst
==
1
):
return
elif
neg
:
# Don't add an extra neg node as we can't
# fully replace this mul by a neg.
m1
=
numpy
.
asarray
(
-
1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
new_inputs
=
[
m1
]
+
new_inputs
rval
=
T
.
mul
(
*
new_inputs
)
return
[
broadcast_like
(
rval
,
node
.
outputs
[
0
],
node
.
fgraph
)]
return
[
broadcast_like
(
rval
,
node
.
outputs
[
0
],
node
.
fgraph
)]
else
:
else
:
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
9ec45737
...
@@ -2838,7 +2838,7 @@ def test_local_mul_specialize():
...
@@ -2838,7 +2838,7 @@ def test_local_mul_specialize():
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
print
nodes
print
nodes
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
assert
nodes
==
[
T
.
mul
,
inplace
.
neg_inplace
]
assert
nodes
==
[
T
.
mul
]
f
=
function
([
v
,
m
],
v
*
0
*
(
-
m
),
mode
=
mode
)
f
=
function
([
v
,
m
],
v
*
0
*
(
-
m
),
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
...
@@ -2852,6 +2852,12 @@ def test_local_mul_specialize():
...
@@ -2852,6 +2852,12 @@ def test_local_mul_specialize():
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
assert
nodes
==
[
T
.
mul
]
assert
nodes
==
[
T
.
mul
]
f
=
function
([
v
,
m
],
v
*
(
-
1
)
*
m
,
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
print
nodes
theano
.
printing
.
debugprint
(
f
)
assert
nodes
==
[
T
.
mul
]
def
speed_local_pow_specialize_range
():
def
speed_local_pow_specialize_range
():
val
=
numpy
.
random
.
rand
(
1e7
)
val
=
numpy
.
random
.
rand
(
1e7
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论