Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c74da33d
提交
c74da33d
authored
7月 18, 2015
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3151 from carriepl/local_sum_mul_by_scalar
[BUG] Fix invalid optimization for product of multiplication
上级
f8a4ee58
93437b1e
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
121 行增加
和
18 行删除
+121
-18
opt.py
theano/tensor/opt.py
+33
-18
test_opt.py
theano/tensor/tests/test_opt.py
+88
-0
没有找到文件。
theano/tensor/opt.py
浏览文件 @
c74da33d
...
@@ -3874,8 +3874,8 @@ def local_sum_prod_mul_by_scalar(node):
...
@@ -3874,8 +3874,8 @@ def local_sum_prod_mul_by_scalar(node):
or
or
prod(scalar * smth) -> scalar * prod(smth)
prod(scalar * smth) -> scalar *
* size(smth) *
prod(smth)
prod(-smth) -> -prod(smth)
prod(-smth) -> -
1 ** size(smth) *
prod(smth)
"""
"""
# TODO: if the the thing inside the Sum is a division,
# TODO: if the the thing inside the Sum is a division,
# we should get at the numerator....
# we should get at the numerator....
...
@@ -3886,24 +3886,39 @@ def local_sum_prod_mul_by_scalar(node):
...
@@ -3886,24 +3886,39 @@ def local_sum_prod_mul_by_scalar(node):
scalars
=
[
t
.
dimshuffle
()
for
t
in
terms
if
scalars
=
[
t
.
dimshuffle
()
for
t
in
terms
if
numpy
.
all
(
t
.
type
.
broadcastable
)]
numpy
.
all
(
t
.
type
.
broadcastable
)]
non_scalars
=
[
t
for
t
in
terms
if
not
numpy
.
all
(
t
.
broadcastable
)]
non_scalars
=
[
t
for
t
in
terms
if
not
numpy
.
all
(
t
.
broadcastable
)]
if
scalars
:
if
len
(
scalars
)
>
1
:
if
len
(
scalars
)
==
0
:
if
len
(
non_scalars
)
>
1
:
# Nothing to optimize here
return
[
T
.
mul
(
T
.
mul
(
*
scalars
),
return
node
.
op
(
T
.
mul
(
*
non_scalars
)))]
elif
len
(
non_scalars
)
==
1
:
# Perform the op only on the non-scalar inputs, if applicable
return
[
T
.
mul
(
T
.
mul
(
*
scalars
),
if
len
(
non_scalars
)
==
0
:
node
.
op
(
non_scalars
[
0
]))]
new_op_input_nb_elements
=
1
else
:
new_op_output
=
1
return
[
T
.
mul
(
*
scalars
)]
else
:
if
len
(
non_scalars
)
>
1
:
return
[
T
.
mul
(
scalars
[
0
],
node
.
op
(
T
.
mul
(
*
non_scalars
)))]
elif
len
(
non_scalars
)
==
1
:
elif
len
(
non_scalars
)
==
1
:
return
[
T
.
mul
(
scalars
[
0
],
node
.
op
(
non_scalars
[
0
]))]
new_op_input_nb_elements
=
T
.
prod
(
non_scalars
[
0
]
.
shape
)
new_op_output
=
node
.
op
(
non_scalars
[
0
])
else
:
else
:
return
[
scalars
[
0
]]
new_op_input
=
T
.
mul
(
*
non_scalars
)
new_op_input_nb_elements
=
T
.
prod
(
new_op_input
.
shape
)
new_op_output
=
node
.
op
(
new_op_input
)
# If node.op is a T.elemwise.Prod, then the scalars need to be
# raised to the power of the number of elements in the input
# to the Prod
if
(
isinstance
(
node
.
op
,
T
.
elemwise
.
Prod
)
and
new_op_input_nb_elements
!=
1
):
scalars
=
[
s
**
new_op_input_nb_elements
for
s
in
scalars
]
# Scale the output of the op by the scalars and return as
# replacement for the original output
mul_inputs
=
scalars
if
new_op_input_nb_elements
!=
1
:
mul_inputs
.
append
(
new_op_output
)
return
[
T
.
mul
(
*
mul_inputs
)]
if
isinstance
(
node
.
op
,
T
.
Sum
)
and
node_inps
.
owner
and
node_inps
.
owner
.
op
==
T
.
neg
:
if
isinstance
(
node
.
op
,
T
.
Sum
)
and
node_inps
.
owner
and
node_inps
.
owner
.
op
==
T
.
neg
:
return
[
T
.
neg
(
node
.
op
(
node_inps
.
owner
.
inputs
[
0
]))]
return
[
T
.
neg
(
node
.
op
(
node_inps
.
owner
.
inputs
[
0
]))]
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
c74da33d
...
@@ -4499,6 +4499,94 @@ class T_local_sum_prod(unittest.TestCase):
...
@@ -4499,6 +4499,94 @@ class T_local_sum_prod(unittest.TestCase):
self
.
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'canonicalize'
,
self
.
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'canonicalize'
,
'specialize'
)
'specialize'
)
def
test_local_sum_prod_mul_by_scalar
(
self
):
# Test the optimization local_sum_prod_mul_by_scalar for both Sum and
# Prod ops in six cases each :
# 1-the inputs to the mul contain a scalar and no non-scalar
# 2-the inputs to the mul contain a scalar and one non-scalar
# 3-the inputs to the mul contain a scalar and two non-scalars
# 4-the inputs to the mul contain two scalars and no non-scalar
# 5-the inputs to the mul contain two scalars and one non-scalar
# 6-the inputs to the mul contain two scalars and two non-scalars
vect
=
T
.
dvector
()
mat
=
T
.
dmatrix
()
scalar1
=
T
.
dscalar
()
scalar2
=
T
.
dscalar
()
v_val
=
numpy
.
random
.
rand
(
2
)
m_val
=
numpy
.
random
.
rand
(
2
,
2
)
s1_val
=
numpy
.
random
.
rand
()
s2_val
=
numpy
.
random
.
rand
()
def
test_reduction_opt
(
inputs
,
inputs_val
,
reduction_op
,
expected_output
,
nb_expected_sum_nodes
):
mul_out
=
T
.
mul
(
*
inputs
)
f
=
theano
.
function
(
inputs
,
reduction_op
()(
mul_out
),
mode
=
self
.
mode
)
out
=
f
(
*
inputs_val
)
utt
.
assert_allclose
(
out
,
expected_output
)
# Ensure that the optimization has been applied properly by
# ensuring that the optimized graph contains the expected number
# of apply nodes for the sum op
prod_nodes
=
[
n
for
n
in
f
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
n
.
op
,
reduction_op
)]
assert
len
(
prod_nodes
)
==
nb_expected_sum_nodes
# Test sum
# Case 1
test_reduction_opt
([
scalar1
],
[
s1_val
],
T
.
Sum
,
s1_val
,
0
)
# Case 2
test_reduction_opt
([
vect
,
scalar1
],
[
v_val
,
s1_val
],
T
.
Sum
,
s1_val
*
v_val
.
sum
(),
1
)
# Case 3
test_reduction_opt
([
vect
,
mat
,
scalar1
],
[
v_val
,
m_val
,
s1_val
],
T
.
Sum
,
s1_val
*
(
v_val
*
m_val
)
.
sum
(),
1
)
# Case 4
test_reduction_opt
([
scalar1
,
scalar2
],
[
s1_val
,
s2_val
],
T
.
Sum
,
s1_val
*
s2_val
,
0
)
# Case 5
test_reduction_opt
([
vect
,
scalar1
,
scalar2
],
[
v_val
,
s1_val
,
s2_val
],
T
.
Sum
,
s1_val
*
s2_val
*
v_val
.
sum
(),
1
)
# Case 6
test_reduction_opt
([
vect
,
mat
,
scalar1
,
scalar2
],
[
v_val
,
m_val
,
s1_val
,
s2_val
],
T
.
Sum
,
s1_val
*
s2_val
*
(
v_val
*
m_val
)
.
sum
(),
1
)
# Test prod
# Case 1
test_reduction_opt
([
scalar1
],
[
s1_val
],
T
.
elemwise
.
Prod
,
s1_val
,
0
)
# Case 2
test_reduction_opt
([
vect
,
scalar1
],
[
v_val
,
s1_val
],
T
.
elemwise
.
Prod
,
(
s1_val
*
v_val
)
.
prod
(),
2
)
# Case 3
test_reduction_opt
([
vect
,
mat
,
scalar1
],
[
v_val
,
m_val
,
s1_val
],
T
.
elemwise
.
Prod
,
(
s1_val
*
v_val
*
m_val
)
.
prod
(),
2
)
# Case 4
test_reduction_opt
([
scalar1
,
scalar2
],
[
s1_val
,
s2_val
],
T
.
elemwise
.
Prod
,
s1_val
*
s2_val
,
0
)
# Case 5
test_reduction_opt
([
vect
,
scalar1
,
scalar2
],
[
v_val
,
s1_val
,
s2_val
],
T
.
elemwise
.
Prod
,
(
s1_val
*
s2_val
*
v_val
)
.
prod
(),
2
)
# Case 6
test_reduction_opt
([
vect
,
mat
,
scalar1
,
scalar2
],
[
v_val
,
m_val
,
s1_val
,
s2_val
],
T
.
elemwise
.
Prod
,
(
s1_val
*
s2_val
*
v_val
*
m_val
)
.
prod
(),
2
)
def
test_local_sum_prod_all_to_none
(
self
):
def
test_local_sum_prod_all_to_none
(
self
):
a
=
T
.
tensor3
()
a
=
T
.
tensor3
()
input
=
numpy
.
arange
(
3
*
4
*
5
,
dtype
=
config
.
floatX
)
.
reshape
(
3
,
4
,
5
)
input
=
numpy
.
arange
(
3
*
4
*
5
,
dtype
=
config
.
floatX
)
.
reshape
(
3
,
4
,
5
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论