Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
68d72ec3
提交
68d72ec3
authored
12月 07, 2014
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix bug in {inc,set}_subtensor in advanced1 case
Reported by Stanislas Lauly
上级
dbb03761
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
144 行增加
和
8 行删除
+144
-8
config.txt
doc/library/config.txt
+1
-1
configdefaults.py
theano/configdefaults.py
+8
-0
subtensor.py
theano/tensor/subtensor.py
+51
-5
test_subtensor.py
theano/tensor/tests/test_subtensor.py
+84
-2
没有找到文件。
doc/library/config.txt
浏览文件 @
68d72ec3
...
@@ -398,7 +398,7 @@ import theano and print the config variable, as in:
...
@@ -398,7 +398,7 @@ import theano and print the config variable, as in:
.. attribute:: config.warn.ignore_bug_before
.. attribute:: config.warn.ignore_bug_before
String value: 'None', 'all', '0.3', '0.4', '0.4.1', '0.5', '0.6'
String value: 'None', 'all', '0.3', '0.4', '0.4.1', '0.5', '0.6'
, '0.7'
Default: 'None'
Default: 'None'
...
...
theano/configdefaults.py
浏览文件 @
68d72ec3
...
@@ -462,6 +462,14 @@ AddConfigVar('warn.reduce_join',
...
@@ -462,6 +462,14 @@ AddConfigVar('warn.reduce_join',
BoolParam
(
warn_default
(
'0.7'
)),
BoolParam
(
warn_default
(
'0.7'
)),
in_c_key
=
False
)
in_c_key
=
False
)
AddConfigVar
(
'warn.inc_set_subtensor1'
,
(
'Warn if previous versions of Theano (before 0.7) could have '
'given incorrect results for inc_subtensor and set_subtensor '
'when using some patterns of advanced indexing (indexing with '
'one vector or matrix of ints).'
),
BoolParam
(
warn_default
(
'0.7'
)),
in_c_key
=
False
)
AddConfigVar
(
'compute_test_value'
,
AddConfigVar
(
'compute_test_value'
,
(
"If 'True', Theano will run each op at graph build time, using "
(
"If 'True', Theano will run each op at graph build time, using "
"Constants, SharedVariables and the tag 'test_value' as inputs "
"Constants, SharedVariables and the tag 'test_value' as inputs "
...
...
theano/tensor/subtensor.py
浏览文件 @
68d72ec3
...
@@ -15,6 +15,7 @@ from theano.gof import Apply, Constant, hashtype, Op, Type, MethodNotDefined
...
@@ -15,6 +15,7 @@ from theano.gof import Apply, Constant, hashtype, Op, Type, MethodNotDefined
from
theano.gof.python25
import
maxsize
from
theano.gof.python25
import
maxsize
from
theano.printing
import
pprint
from
theano.printing
import
pprint
from
theano
import
scalar
as
scal
from
theano
import
scalar
as
scal
from
theano.tensor.basic
import
alloc
from
theano.tensor.basic
import
(
addbroadcast
,
clip
,
get_scalar_constant_value
,
from
theano.tensor.basic
import
(
addbroadcast
,
clip
,
get_scalar_constant_value
,
ARange
,
TensorType
,
NotScalarConstantError
)
ARange
,
TensorType
,
NotScalarConstantError
)
from
theano.tensor.elemwise
import
DimShuffle
from
theano.tensor.elemwise
import
DimShuffle
...
@@ -1022,8 +1023,8 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
...
@@ -1022,8 +1023,8 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
"subtensor with a
%
d-dimensional value."
)
%
(
x
.
ndim
,
"subtensor with a
%
d-dimensional value."
)
%
(
x
.
ndim
,
y
.
ndim
))
y
.
ndim
))
dim_offset
=
x
.
ndim
-
y
.
ndim
for
dim
in
range
(
y
.
ndim
):
for
dim
in
range
(
y
.
ndim
):
dim_offset
=
x
.
ndim
-
y
.
ndim
if
(
x
.
broadcastable
[
dim
+
dim_offset
]
if
(
x
.
broadcastable
[
dim
+
dim_offset
]
and
not
y
.
broadcastable
[
dim
]):
and
not
y
.
broadcastable
[
dim
]):
# It is acceptable to try to increment a subtensor with a
# It is acceptable to try to increment a subtensor with a
...
@@ -1071,12 +1072,37 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
...
@@ -1071,12 +1072,37 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
# completely, but the problem is that advanced_inc_subtensor1 can only
# completely, but the problem is that advanced_inc_subtensor1 can only
# work on the first (outer-most, left-most) dimension of x,
# work on the first (outer-most, left-most) dimension of x,
# just like advanced_subtensor1.
# just like advanced_subtensor1.
# So we call advanced_inc_subtensor1(x.T, i, y), but then we need to
# So we call advanced_inc_subtensor1(x.T, i, y.T) (as we also need to
# transpose y if it is not a scalar or a vector), but then we need to
# return something that has the same shape as x, not as x.T (inner_x).
# return something that has the same shape as x, not as x.T (inner_x).
# So re-apply the outer dimshuffle on the new inc_subtensor,
# So re-apply the outer dimshuffle on the new inc_subtensor,
# and return advanced_inc_subtensor1(x.T, i, y).T.
# and return advanced_inc_subtensor1(x.T, i, y.T).T.
# Get the dimshuffle pattern to apply to y.
x_order
=
x
.
owner
.
op
.
new_order
y_order
=
[
'x'
]
*
x
.
ndim
for
i
,
v
in
enumerate
(
x_order
):
if
v
!=
'x'
and
(
v
-
dim_offset
)
>=
0
:
y_order
[
v
-
dim_offset
]
=
i
# Warn if this code path would have produced wrong results in the past
if
config
.
warn
.
inc_set_subtensor1
:
# Dimshuffle pattern for y that would be equivalent to past code
prev_y_order
=
[
'x'
]
*
(
dim_offset
)
+
list
(
range
(
y
.
ndim
))
if
y_order
!=
prev_y_order
:
warnings
.
warn
(
'Although your current code is fine, please note that '
'earlier versions prior to 0.7 (or this development '
'version) may have yielded an incorrect result in '
'this `inc_subtensor` or `set_subtensor` operation. '
'To remove this warning, you can either set the '
'`warn.inc_set_subtensor1` config option to `False`, '
'or `warn.ignore_bug_before` to at least "0.7".'
,
stacklevel
=
2
)
inner_incsubtensor
=
inc_subtensor
(
inner_incsubtensor
=
inc_subtensor
(
inner_x
,
y
,
inner_x
,
y
.
dimshuffle
(
y_order
),
inplace
=
inplace
,
inplace
=
inplace
,
set_instead_of_inc
=
set_instead_of_inc
,
set_instead_of_inc
=
set_instead_of_inc
,
tolerate_inplace_aliasing
=
tolerate_inplace_aliasing
)
tolerate_inplace_aliasing
=
tolerate_inplace_aliasing
)
...
@@ -1086,8 +1112,28 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
...
@@ -1086,8 +1112,28 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
# Try to apply inc_subtensor on inner_x.
# Try to apply inc_subtensor on inner_x.
# If it works, there is no need to reshape, as the inc_subtensor
# If it works, there is no need to reshape, as the inc_subtensor
# will have the same shape as inner_x, which is what we want.
# will have the same shape as inner_x, which is what we want.
# We also explicitly duplicate y to its broadcasted shape
# before we partially flatten it to inner_x dimension. This is
# not strictly needed in all cases, but it is easier this way.
expanded_y
=
alloc
(
y
,
*
[
x
.
shape
[
i
]
for
i
in
range
(
x
.
ndim
)])
flattened_y
=
expanded_y
.
flatten
(
inner_x
.
ndim
)
# Warn if this code path would have produced wrong results in the past
if
config
.
warn
.
inc_set_subtensor1
:
if
inner_x
.
ndim
>
1
and
sum
(
y
.
broadcastable
)
>
0
:
warnings
.
warn
(
'Although your current code is fine, please note that '
'earlier versions prior to 0.7 (or this development '
'version) may have yielded an incorrect result in '
'this `inc_subtensor` or `set_subtensor` operation. '
'To remove this warning, you can either set the '
'`warn.inc_set_subtensor1` config option to `False`, '
'or `warn.ignore_bug_before` to at least "0.7".'
,
stacklevel
=
2
)
inner_incsubtensor
=
inc_subtensor
(
inner_incsubtensor
=
inc_subtensor
(
inner_x
,
y
.
flatten
(),
inner_x
,
flattened_y
,
inplace
=
inplace
,
inplace
=
inplace
,
set_instead_of_inc
=
set_instead_of_inc
,
set_instead_of_inc
=
set_instead_of_inc
,
tolerate_inplace_aliasing
=
tolerate_inplace_aliasing
)
tolerate_inplace_aliasing
=
tolerate_inplace_aliasing
)
...
...
theano/tensor/tests/test_subtensor.py
浏览文件 @
68d72ec3
...
@@ -1035,8 +1035,16 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
...
@@ -1035,8 +1035,16 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m
=
matrix
(
'm'
)
m
=
matrix
(
'm'
)
i
=
lmatrix
(
'i'
)
i
=
lmatrix
(
'i'
)
m1
=
set_subtensor
(
m
[:,
i
],
0
)
# That test actually gave correct results, the warning is
m2
=
inc_subtensor
(
m
[:,
i
],
1
)
# a bit too broad
orig_warn
=
config
.
warn
.
inc_set_subtensor1
try
:
config
.
warn
.
inc_set_subtensor1
=
False
m1
=
set_subtensor
(
m
[:,
i
],
0
)
m2
=
inc_subtensor
(
m
[:,
i
],
1
)
finally
:
config
.
warn
.
inc_set_subtensor1
=
orig_warn
f
=
theano
.
function
([
m
,
i
],
[
m1
,
m2
])
f
=
theano
.
function
([
m
,
i
],
[
m1
,
m2
])
m_val
=
rand
(
5
,
7
)
m_val
=
rand
(
5
,
7
)
...
@@ -1052,6 +1060,80 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
...
@@ -1052,6 +1060,80 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
assert
numpy
.
allclose
(
m1_val
,
m1_ref
),
(
m1_val
,
m1_ref
)
assert
numpy
.
allclose
(
m1_val
,
m1_ref
),
(
m1_val
,
m1_ref
)
assert
numpy
.
allclose
(
m2_val
,
m2_ref
),
(
m2_val
,
m2_ref
)
assert
numpy
.
allclose
(
m2_val
,
m2_ref
),
(
m2_val
,
m2_ref
)
def
test_adv1_inc_sub_notlastdim_1dval
(
self
):
# Test that taking 1-dimensional advanced indexing
# over a dimension that's not the first (outer-most),
# and incrementing/setting a 1D value works.
m
=
matrix
(
'm'
)
# Test for both vector and matrix as index
sym_i
=
(
lvector
(
'i'
),
lmatrix
(
'i'
))
shape_i
=
((
4
,),
(
4
,
2
))
shape_val
=
((
3
,
1
),
(
3
,
1
,
1
))
# Disable the warning emitted for that case
orig_warn
=
config
.
warn
.
inc_set_subtensor1
try
:
config
.
warn
.
inc_set_subtensor1
=
False
for
i
,
shp_i
,
shp_v
in
zip
(
sym_i
,
shape_i
,
shape_val
):
sub_m
=
m
[:,
i
]
m1
=
set_subtensor
(
sub_m
,
numpy
.
zeros
(
shp_v
))
m2
=
inc_subtensor
(
sub_m
,
numpy
.
ones
(
shp_v
))
f
=
theano
.
function
([
m
,
i
],
[
m1
,
m2
])
m_val
=
rand
(
3
,
5
)
i_val
=
randint_ranged
(
min
=
0
,
max
=
4
,
shape
=
shp_i
)
m1_ref
=
m_val
.
copy
()
m2_ref
=
m_val
.
copy
()
m1_val
,
m2_val
=
f
(
m_val
,
i_val
)
for
idx
in
i_val
.
ravel
():
m1_ref
[:,
idx
]
=
0
m2_ref
[:,
idx
]
+=
1
assert
numpy
.
allclose
(
m1_val
,
m1_ref
),
(
m1_val
,
m1_ref
)
assert
numpy
.
allclose
(
m2_val
,
m2_ref
),
(
m2_val
,
m2_ref
)
finally
:
config
.
warn
.
inc_set_subtensor1
=
orig_warn
def
test_adv1_inc_sub_notlastdim_2dval
(
self
):
# Test that taking 1-dimensional advanced indexing
# over a dimension that's not the first (outer-most),
# and incrementing/setting a 2D value works.
m
=
matrix
(
'm'
)
# Test for both vector and matrix as index
sym_i
=
(
lvector
(
'i'
),
lmatrix
(
'i'
))
shape_i
=
((
4
,),
(
4
,
2
))
shape_val
=
((
3
,
1
),
(
3
,
1
,
1
))
# Disable the warning emitted for that case
orig_warn
=
config
.
warn
.
inc_set_subtensor1
try
:
config
.
warn
.
inc_set_subtensor1
=
False
for
i
,
shp_i
,
shp_v
in
zip
(
sym_i
,
shape_i
,
shape_val
):
sub_m
=
m
[:,
i
]
m1
=
set_subtensor
(
sub_m
,
numpy
.
zeros
(
shp_v
))
m2
=
inc_subtensor
(
sub_m
,
numpy
.
ones
(
shp_v
))
f
=
theano
.
function
([
m
,
i
],
[
m1
,
m2
])
m_val
=
rand
(
3
,
5
)
i_val
=
randint_ranged
(
min
=
0
,
max
=
4
,
shape
=
shp_i
)
m1_ref
=
m_val
.
copy
()
m2_ref
=
m_val
.
copy
()
m1_val
,
m2_val
=
f
(
m_val
,
i_val
)
for
idx
in
i_val
:
m1_ref
[:,
idx
]
=
0
m2_ref
[:,
idx
]
+=
1
assert
numpy
.
allclose
(
m1_val
,
m1_ref
),
(
m1_val
,
m1_ref
)
assert
numpy
.
allclose
(
m2_val
,
m2_ref
),
(
m2_val
,
m2_ref
)
finally
:
config
.
warn
.
inc_set_subtensor1
=
orig_warn
class
TestIncSubtensor1
(
unittest
.
TestCase
):
class
TestIncSubtensor1
(
unittest
.
TestCase
):
# test inc_subtensor
# test inc_subtensor
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论