Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6496b0c0
提交
6496b0c0
authored
2月 22, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rewrite local_mul_specialize to use alloc instead of fill_chain
上级
b38ffe57
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
59 行增加
和
10 行删除
+59
-10
opt.py
theano/tensor/opt.py
+14
-10
test_opt.py
theano/tensor/tests/test_opt.py
+45
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
6496b0c0
...
@@ -1345,9 +1345,9 @@ register_specialize(local_pow_specialize)
...
@@ -1345,9 +1345,9 @@ register_specialize(local_pow_specialize)
@gof.local_optimizer
([
T
.
mul
])
@gof.local_optimizer
([
T
.
mul
])
def
local_mul_specialize
(
node
):
def
local_mul_specialize
(
node
):
def
fill_chain
(
v
):
return
_fill_chain
(
v
,
node
.
inputs
)
#here, we are past the point of canonicalization, so we don't want to put in un-necessary fills.
#here, we are past the point of canonicalization, so we don't want to put in un-necessary fills.
#
# at this point [post canonicalize], mul() may have many inputs.
if
node
.
op
==
T
.
mul
:
if
node
.
op
==
T
.
mul
:
#the idea here is that we have pow(x, y)
#the idea here is that we have pow(x, y)
neg
=
False
neg
=
False
...
@@ -1365,6 +1365,7 @@ def local_mul_specialize(node):
...
@@ -1365,6 +1365,7 @@ def local_mul_specialize(node):
elif
N
.
all
(
y
==
-
1.0
):
elif
N
.
all
(
y
==
-
1.0
):
neg
^=
True
#toggles
neg
^=
True
#toggles
elif
N
.
all
(
y
==
0.0
):
elif
N
.
all
(
y
==
0.0
):
# if we find any zero, we just return right away
return
[
T
.
alloc
(
numpy
.
asarray
(
0
,
dtype
=
node
.
outputs
[
0
]
.
dtype
),
return
[
T
.
alloc
(
numpy
.
asarray
(
0
,
dtype
=
node
.
outputs
[
0
]
.
dtype
),
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
else
:
else
:
...
@@ -1374,26 +1375,29 @@ def local_mul_specialize(node):
...
@@ -1374,26 +1375,29 @@ def local_mul_specialize(node):
if
new_inputs
:
if
new_inputs
:
if
len
(
new_inputs
)
==
1
:
if
len
(
new_inputs
)
==
1
:
if
neg
:
if
neg
:
msg
=
-
new_inputs
[
0
]
rval
=
-
new_inputs
[
0
]
else
:
else
:
msg
=
new_inputs
[
0
]
rval
=
new_inputs
[
0
]
return
fill_chain
(
msg
)
else
:
else
:
if
neg
:
if
neg
:
msg
=
-
T
.
mul
(
*
new_inputs
)
rval
=
-
T
.
mul
(
*
new_inputs
)
else
:
else
:
msg
=
T
.
mul
(
*
new_inputs
)
rval
=
T
.
mul
(
*
new_inputs
)
return
[
T
.
alloc
(
T
.
cast
(
msg
,
node
.
outputs
[
0
]
.
dtype
),
return
[
T
.
alloc
(
T
.
cast
(
rval
,
node
.
outputs
[
0
]
.
dtype
),
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
else
:
else
:
# there are no variable inputs to mul
# N.B. this could have been constant-folded...
if
neg
:
if
neg
:
# return output's worth of -1
# return output's worth of -1
return
[
T
.
alloc
(
numpy
.
asarray
(
-
1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
),
return
[
T
.
alloc
(
numpy
.
asarray
(
-
1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
),
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
else
:
else
:
# return output's worth of 1
# return output's worth of 1
return
[
T
.
alloc
(
numpy
.
asarray
(
1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
),
return
[
T
.
alloc
(
numpy
.
asarray
(
1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
),
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
*
node
.
env
.
shape_feature
.
shape_of
[
node
.
outputs
[
0
]])]
register_specialize
(
local_mul_specialize
)
register_specialize
(
local_mul_specialize
)
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
6496b0c0
...
@@ -1013,6 +1013,51 @@ class test_shapeoptimizer(unittest.TestCase):
...
@@ -1013,6 +1013,51 @@ class test_shapeoptimizer(unittest.TestCase):
print
f
.
maker
.
env
.
toposort
()
print
f
.
maker
.
env
.
toposort
()
assert
[]
==
f
.
maker
.
env
.
toposort
()
assert
[]
==
f
.
maker
.
env
.
toposort
()
def
test_local_mul_specialize
():
# test a few cases to make sure that the basics are covered
#
mode
=
theano
.
config
.
mode
if
mode
==
'FAST_COMPILE'
:
mode
=
'FAST_RUN'
v
=
T
.
vector
()
m
=
T
.
vector
()
f
=
function
([
v
,
m
],
v
*
1
,
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
print
nodes
assert
nodes
==
[]
f
=
function
([
v
,
m
],
v
*
0
,
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
print
nodes
assert
nodes
==
[
Shape_i
(
0
),
T
.
alloc
]
f
=
function
([
v
,
m
],
v
*
(
-
1
),
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
print
nodes
assert
nodes
==
[
T
.
neg
]
f
=
function
([
v
,
m
],
v
*
1
*
(
-
m
),
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
print
nodes
theano
.
printing
.
debugprint
(
f
)
assert
nodes
==
[
T
.
mul
,
inplace
.
neg_inplace
]
f
=
function
([
v
,
m
],
v
*
0
*
(
-
m
),
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
print
nodes
theano
.
printing
.
debugprint
(
f
)
assert
nodes
==
[
Shape_i
(
0
),
T
.
alloc
]
f
=
function
([
v
,
m
],
v
*
(
-
1
)
*
(
-
m
),
mode
=
mode
)
nodes
=
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
print
nodes
theano
.
printing
.
debugprint
(
f
)
assert
nodes
==
[
T
.
mul
]
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
# unittest.main()
# unittest.main()
test_fusion
()
.
tes_memory_leak
()
test_fusion
()
.
tes_memory_leak
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论