Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6aa9d88e
提交
6aa9d88e
authored
8月 18, 2015
作者:
Mohammad Pezeshki
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimization for prod added
上级
856aa0b6
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
80 行增加
和
52 行删除
+80
-52
opt.py
theano/tensor/opt.py
+80
-52
没有找到文件。
theano/tensor/opt.py
浏览文件 @
6aa9d88e
...
...
@@ -4130,39 +4130,40 @@ def local_elemwise_sub_zeros(node):
@register_canonicalize
@register_specialize
@gof.local_optimizer
([
T
.
Sum
])
def
local_sum_div_dimshuffle
(
node
):
@gof.local_optimizer
([
T
.
Sum
,
T
.
elemwise
.
Prod
])
def
local_sum_
prod_
div_dimshuffle
(
node
):
"""
sum(a / dimshuffle{...}(b), axis=l) -> sum(a, axis={...}) / b,
if dimension l of the DimShuffle is 'x'.
if dimension l of the DimShuffle is 'x'
or
prod(a / dimshuffle{...}(b), axis=l) ->
prod(a, axis={...}) / b ** a.shape[l],
if dimension l of the DimShuffle is 'x'
"""
# TODO: extend it to product, and quotient of products
# It does not make much sense now to extend it to the case where the
# dimshuffle is in the numerator, since elemwise inversion of the
# denominator would still be needed before the summation.
# denominator would still be needed before the summation
or production
.
if
isinstance
(
node
.
op
,
T
.
Sum
):
if
isinstance
(
node
.
op
,
(
T
.
Sum
,
T
.
elemwise
.
Prod
)
):
axis
=
node
.
op
.
axis
if
axis
is
None
:
axis
=
list
(
range
(
node
.
inputs
[
0
]
.
ndim
))
# print 'axis =', axis
thing_summed
=
node
.
inputs
[
0
]
if
thing_summed
.
owner
and
thing_summed
.
owner
.
op
==
T
.
true_div
:
numerator
,
denominator
=
thing_summed
.
owner
.
inputs
node_input
=
node
.
inputs
[
0
]
if
node_input
.
owner
and
node_input
.
owner
.
op
==
T
.
true_div
:
numerator
,
denominator
=
node_input
.
owner
.
inputs
# Old, bugged logic, reproduced here only to warn users
if
config
.
warn
.
sum_div_dimshuffle_bug
:
if
numerator
.
owner
and
isinstance
(
numerator
.
owner
.
op
,
T
.
DimShuffle
):
new_order
=
numerator
.
owner
.
op
.
new_order
compatible_dims
=
True
for
ax
in
axis
:
if
len
(
new_order
)
<=
ax
or
new_order
[
ax
]
!=
'x'
:
compatible_dims
=
False
break
if
compatible_dims
:
if
(
config
.
warn
.
sum_div_dimshuffle_bug
and
isinstance
(
node
.
op
,
T
.
Sum
)
and
numerator
.
owner
and
isinstance
(
numerator
.
owner
.
op
,
T
.
DimShuffle
)):
# Check compatibility
new_order
=
numerator
.
owner
.
op
.
new_order
for
ax
in
axis
:
if
ax
<
len
(
new_order
)
and
new_order
[
ax
]
==
'x'
:
_logger
.
warn
(
'WARNING: Your current code is fine, but'
' Theano versions between '
'rev. 3bd9b789f5e8 (2010-06-16) and'
...
...
@@ -4171,37 +4172,49 @@ def local_sum_div_dimshuffle(node):
'To disable this warning, set the Theano'
' flag warn.sum_div_dimshuffle_bug to'
' False.'
)
break
if
denominator
.
owner
and
isinstance
(
denominator
.
owner
.
op
,
T
.
DimShuffle
):
thing_dimshuffled
=
denominator
.
owner
.
inputs
[
0
]
new_order
=
denominator
.
owner
.
op
.
new_order
# print 'new_order =', new_order
# check compatibility
compatible_dims
=
True
for
ax
in
axis
:
# print 'ax =', ax
# print 'len(new_order) =', len(new_order)
# print 'new_order[ax] =', new_order[ax]
if
len
(
new_order
)
<=
ax
or
new_order
[
ax
]
!=
'x'
:
compatible_dims
=
False
break
dimshuffle_input
=
denominator
.
owner
.
inputs
[
0
]
dimshuffle_order
=
denominator
.
owner
.
op
.
new_order
if
compatible_dims
:
# print 'getting denom out'
# Keep needed dimensions for new dimshuffle
new_new_order
=
list
(
ax
for
i
,
ax
in
enumerate
(
new_order
)
if
i
not
in
axis
or
ax
!=
'x'
)
# print 'new_new_order =', new_new_order
# Remove useless rebroadcast axes
while
len
(
new_new_order
)
>
0
and
new_new_order
[
0
]
==
'x'
:
del
new_new_order
[
0
]
# print 'new_new_order =', new_new_order
if
all
(
i
==
e
for
i
,
e
in
enumerate
(
new_new_order
)):
new_denom
=
thing_dimshuffled
compatible_dims
=
[]
incompatible_dims
=
[]
for
ax
in
axis
:
if
(
ax
<
len
(
dimshuffle_order
)
and
dimshuffle_order
[
ax
]
==
'x'
):
compatible_dims
.
append
(
ax
)
else
:
incompatible_dims
.
append
(
ax
)
reordered_incompatible_dims
=
[]
for
ic_ax
in
incompatible_dims
:
reordered_incompatible_dims
.
append
(
ic_ax
-
sum
(
[
1
for
c_ax
in
compatible_dims
if
c_ax
<
ic_ax
]))
if
len
(
compatible_dims
)
>
0
:
optimized_dimshuffle_order
=
list
(
ax
for
i
,
ax
in
enumerate
(
dimshuffle_order
)
if
(
i
not
in
axis
)
or
(
ax
!=
'x'
))
# Removing leading 'x' (since it will be done automatically)
while
(
len
(
optimized_dimshuffle_order
)
>
0
and
optimized_dimshuffle_order
[
0
]
==
'x'
):
del
optimized_dimshuffle_order
[
0
]
# if optimized_dimshuffle_order is sorted with
# not 'x', then dimshuffle is useless.
if
all
(
i
==
e
for
i
,
e
in
enumerate
(
optimized_dimshuffle_order
)):
optimized_dimshuffle
=
dimshuffle_input
else
:
if
config
.
warn
.
sum_div_dimshuffle_bug
:
optimized_dimshuffle
=
T
.
DimShuffle
(
dimshuffle_input
.
type
.
broadcastable
,
optimized_dimshuffle_order
)(
dimshuffle_input
)
if
(
config
.
warn
.
sum_div_dimshuffle_bug
and
isinstance
(
node
.
op
,
T
.
Sum
)):
_logger
.
warn
(
'WARNING: Your current code is fine,'
' but Theano versions between '
'rev. 3bd9b789f5e8 (2010-06-16) and'
...
...
@@ -4212,12 +4225,27 @@ def local_sum_div_dimshuffle(node):
'warn.sum_div_dimshuffle_bug'
' to False.'
)
new_denom
=
T
.
DimShuffle
(
thing_dimshuffled
.
type
.
broadcastable
,
new_new_order
)(
thing_dimshuffled
)
return
[
T
.
true_div
(
node
.
op
(
numerator
),
new_denom
)]
# else:
# print 'incompatible dims:', axis, new_order
if
isinstance
(
node
.
op
,
T
.
Sum
):
op_on_compatible_dims
=
T
.
sum
(
numerator
,
axis
=
compatible_dims
)
div_op
=
T
.
true_div
(
op_on_compatible_dims
,
optimized_dimshuffle
)
op_on_incompatible_dims
=
T
.
sum
(
div_op
,
axis
=
reordered_incompatible_dims
)
elif
isinstance
(
node
.
op
,
T
.
elemwise
.
Prod
):
op_on_compatible_dims
=
T
.
prod
(
numerator
,
axis
=
compatible_dims
)
div_op
=
T
.
true_div
(
op_on_compatible_dims
,
(
optimized_dimshuffle
**
T
.
prod
([
numerator
.
shape
[
ax
]
for
ax
in
compatible_dims
])))
op_on_incompatible_dims
=
T
.
prod
(
div_op
,
axis
=
reordered_incompatible_dims
)
return
[
op_on_incompatible_dims
]
@register_canonicalize
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论