Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
cdaa8834
提交
cdaa8834
authored
3月 11, 2013
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1274 from lamblin/fix_inc_set_subtensor1
Make inc/set_subtensor work on output of take.
上级
f62a038d
265fcdff
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
3 行删除
+81
-3
basic.py
theano/tensor/basic.py
+33
-2
test_basic.py
theano/tensor/tests/test_basic.py
+48
-1
没有找到文件。
theano/tensor/basic.py
浏览文件 @
cdaa8834
...
...
@@ -5079,6 +5079,7 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
# nor have non-broadcastable dimensions where x is broadcastable.
x
=
as_tensor_variable
(
x
)
y
=
as_tensor_variable
(
y
)
if
y
.
ndim
>
x
.
ndim
:
raise
TypeError
((
"Trying to increment a
%
d-dimensional "
"subtensor with a
%
d-dimensional value."
)
%
(
x
.
ndim
,
y
.
ndim
))
...
...
@@ -5094,7 +5095,7 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
y
=
addbroadcast
(
y
,
dim
)
if
not
x
.
owner
:
raise
TypeError
(
'x must be result of a subtensor operation'
)
raise
TypeError
(
'x must be
the
result of a subtensor operation'
)
# retrieve idx_list from x.owner
if
isinstance
(
x
.
owner
.
op
,
Subtensor
):
...
...
@@ -5121,8 +5122,38 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
the_op
=
AdvancedIncSubtensor
(
inplace
,
set_instead_of_inc
=
set_instead_of_inc
)
return
the_op
(
real_x
,
y
,
coordvec_0
,
coordvec_1
)
elif
isinstance
(
x
.
owner
.
op
,
DimShuffle
):
inner_x
=
x
.
owner
.
inputs
[
0
]
# In the dimshuffle case, there are in fact two dimshuffles:
# one to make the indexed dimension the last one,
# and one to put it back where it was. So, in the case where we have
# inc_subtensor(x[:,i], y), the graph is actually
# inc_subtensor((x.T)[i].T, y).
# We could get all the way to x, and then get rid of the dimshuffles
# completely, but the problem is that advanced_inc_subtensor1 can only
# work on the first (outer-most, left-most) dimension of x,
# just like advanced_subtensor1.
# So we call advanced_inc_subtensor1(x.T, i, y), but then we need to
# 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,
# and return advanced_inc_subtensor1(x.T, i, y).T.
inner_incsubtensor
=
inc_subtensor
(
inner_x
,
y
,
inplace
=
inplace
,
set_instead_of_inc
=
set_instead_of_inc
,
tolerate_inplace_aliasing
=
tolerate_inplace_aliasing
)
return
x
.
owner
.
op
(
inner_incsubtensor
,
*
x
.
owner
.
inputs
[
1
:])
elif
isinstance
(
x
.
owner
.
op
,
Reshape
):
inner_x
=
x
.
owner
.
inputs
[
0
]
# Try to apply inc_subtensor on inner_x.
# 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.
inner_incsubtensor
=
inc_subtensor
(
inner_x
,
y
,
inplace
=
inplace
,
set_instead_of_inc
=
set_instead_of_inc
,
tolerate_inplace_aliasing
=
tolerate_inplace_aliasing
)
return
inner_incsubtensor
else
:
raise
TypeError
(
'x must be result of a subtensor operation'
)
raise
TypeError
(
'x must be
the
result of a subtensor operation'
)
class
IncSubtensor
(
Op
):
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
cdaa8834
...
...
@@ -38,7 +38,7 @@ from theano.tensor import (_shared, wvector, bvector, autocast_float_as,
var
,
Join
,
shape
,
MaxAndArgmax
,
lscalar
,
zvector
,
exp
,
get_scalar_constant_value
,
ivector
,
reshape
,
scalar_from_tensor
,
scal
,
iscalars
,
arange
,
dscalars
,
fvector
,
imatrix
,
numeric_grad
,
opt
,
ComplexError
,
lvector
,
true_div
,
max
,
min
,
Split
,
roll
,
opt
,
ComplexError
,
lvector
,
lmatrix
,
true_div
,
max
,
min
,
Split
,
roll
,
tile
,
patternbroadcast
,
Eye
,
Shape
,
Dot
,
PermuteRowElements
,
ScalarFromTensor
,
TensorFromScalar
,
dtensor4
,
Rebroadcast
,
Alloc
,
dtensor3
,
SpecifyShape
,
Mean
,
IncSubtensor
,
AdvancedIncSubtensor1
,
...
...
@@ -3626,6 +3626,53 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
assert
gof
.
graph
.
is_same_graph
(
s1
,
s2
)
def
test_adv1_inc_sub_notlastdim
(
self
):
# Test that taking 1-dimensional advanced indexing
# over a dimension that's not the first (outer-most) works.
m
=
matrix
(
'm'
)
i
=
lvector
(
'i'
)
m1
=
set_subtensor
(
m
[:,
i
],
0
)
m2
=
inc_subtensor
(
m
[:,
i
],
1
)
f
=
theano
.
function
([
m
,
i
],
[
m1
,
m2
])
m_val
=
rand
(
3
,
5
)
i_val
=
randint_ranged
(
min
=
0
,
max
=
4
,
shape
=
(
4
,))
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
)
def
test_adv1_inc_sub_notlastdim_2didx
(
self
):
# Test that taking 1-dimensional advanced indexing
# over a dimension that's not the first (outer-most) works,
# if the index is a matrix.
m
=
matrix
(
'm'
)
i
=
lmatrix
(
'i'
)
m1
=
set_subtensor
(
m
[:,
i
],
0
)
m2
=
inc_subtensor
(
m
[:,
i
],
1
)
f
=
theano
.
function
([
m
,
i
],
[
m1
,
m2
])
m_val
=
rand
(
5
,
7
)
i_val
=
randint_ranged
(
min
=
0
,
max
=
6
,
shape
=
(
4
,
2
))
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
)
class
TestIncSubtensor1
(
unittest
.
TestCase
):
# test inc_subtensor
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论