Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
28c886fc
提交
28c886fc
authored
4月 10, 2017
作者:
Xavier Bouthillier
提交者:
GitHub
4月 10, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5785 from Faruk-Ahmed/inc_to_set_zeros
inc to set for zeros
上级
1d3c5b3a
f4b05e45
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
53 行增加
和
4 行删除
+53
-4
opt.py
theano/tensor/opt.py
+19
-2
subtensor.py
theano/tensor/subtensor.py
+3
-2
test_opt.py
theano/tensor/tests/test_opt.py
+31
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
28c886fc
from
__future__
import
absolute_import
,
print_function
,
division
"""
Tensor optimizations addressing the ops in basic.py.
""" Tensor optimizations addressing the ops in basic.py.
"""
# TODO: intelligent merge for mul/add
# TODO: 0*x -> 0
...
...
@@ -3411,6 +3410,24 @@ def local_incsubtensor_of_zeros(node):
return
@register_canonicalize
@register_specialize
@gof.local_optimizer
([
IncSubtensor
])
def
local_incsubtensor_of_zeros_to_setsubtensor
(
node
):
"""
IncSubtensor(zeros, x, ...) -> SetSubtensor(zeros, x, ...)
"""
if
(
isinstance
(
node
.
op
,
(
IncSubtensor
))
and
not
node
.
op
.
set_instead_of_inc
):
x
=
node
.
inputs
[
0
]
if
isinstance
(
x
,
T
.
Constant
)
and
not
np
.
any
(
x
.
data
):
return
[
IncSubtensor
(
node
.
op
.
idx_list
,
node
.
op
.
inplace
,
set_instead_of_inc
=
True
,
destroyhandler_tolerate_aliased
=
node
.
op
.
destroyhandler_tolerate_aliased
,
)(
*
node
.
inputs
)]
@register_canonicalize
(
'local_setsubtensor_of_allocs'
)
@register_stabilize
(
'local_setsubtensor_of_allocs'
)
@gof.local_optimizer
([
IncSubtensor
])
...
...
theano/tensor/subtensor.py
浏览文件 @
28c886fc
...
...
@@ -573,8 +573,9 @@ class Subtensor(Op):
else
:
# For best optimization, we let this as an inc.
# This allow the opt local_IncSubtensor_serialize to apply first.
# We need to implement an optimization that will convert this to a
# set subtensor.
# We have an optimization that will convert this to a
# set subtensor here at:
# theano/tensor/opt.py:local_incsubtensor_of_zeros_to_setsubtensor()
first
=
IncSubtensor
(
self
.
idx_list
)(
x
.
zeros_like
(),
gz
,
*
rest
)
return
([
first
]
+
[
DisconnectedType
()()]
*
len
(
rest
))
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
28c886fc
...
...
@@ -3001,6 +3001,37 @@ class Test_alloc_zero(unittest.TestCase):
assert
np
.
all
([
not
isinstance
(
n
.
op
,
tensor
.
IncSubtensor
)
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
def
test_incsubtensor_x_zeros
(
self
):
x
=
tensor
.
constant
(
np
.
asarray
(
np
.
zeros
((
4
,
4
)),
dtype
=
config
.
floatX
))
y
=
tensor
.
matrix
()
z
=
tensor
.
inc_subtensor
(
x
[:
4
],
y
)
f
=
theano
.
function
([
y
],
z
)
inc_nodes
=
[
n
for
n
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
n
.
op
,
tensor
.
IncSubtensor
)]
assert
(
len
(
inc_nodes
)
==
1
)
node_is_set_instead_of_inc
=
inc_nodes
[
0
]
.
op
.
set_instead_of_inc
mode
=
theano
.
config
.
mode
assert
((
mode
!=
"FAST_COMPILE"
and
node_is_set_instead_of_inc
)
or
(
mode
==
"FAST_COMPILE"
and
not
node_is_set_instead_of_inc
))
test_X
=
np
.
random
.
random
((
4
,
4
))
.
astype
(
config
.
floatX
)
utt
.
assert_allclose
(
f
(
test_X
),
test_X
)
# also check the flag doesn't get set if first input is not zeros:
not_all_zeros
=
np
.
zeros
((
4
,
4
))
not_all_zeros
[
1
,
0
]
=
0.001
x
=
tensor
.
constant
(
np
.
asarray
(
not_all_zeros
,
dtype
=
config
.
floatX
))
y
=
tensor
.
matrix
()
z
=
tensor
.
inc_subtensor
(
x
[:
4
],
y
)
f
=
theano
.
function
([
y
],
z
)
inc_nodes
=
[
n
for
n
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
n
.
op
,
tensor
.
IncSubtensor
)]
assert
(
len
(
inc_nodes
)
==
1
)
assert
(
inc_nodes
[
0
]
.
op
.
set_instead_of_inc
is
False
)
test_X
=
np
.
random
.
random
((
4
,
4
))
.
astype
(
config
.
floatX
)
utt
.
assert_allclose
(
f
(
test_X
),
test_X
+
not_all_zeros
)
def
test_advancedincsubtensor1_allocs0
(
self
):
x
=
tensor
.
matrix
()
y
=
tensor
.
matrix
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论