Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
55ac73eb
提交
55ac73eb
authored
8月 16, 2011
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
adding 1d advanced indexing support to inc_subtensor and set_subtensor
上级
344af686
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
82 行增加
和
19 行删除
+82
-19
basic.py
theano/tensor/basic.py
+30
-19
test_basic.py
theano/tensor/tests/test_basic.py
+52
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
55ac73eb
...
@@ -3491,17 +3491,27 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
...
@@ -3491,17 +3491,27 @@ def inc_subtensor(x, y, inplace=False, set_instead_of_inc=False,
>>> new_r = inc_subtensor(r[10:], 5)
>>> new_r = inc_subtensor(r[10:], 5)
"""
"""
# retrieve idx_list from x.owner
# retrieve idx_list from x.owner
if
not
isinstance
(
x
.
owner
.
op
,
Subtensor
):
if
isinstance
(
x
.
owner
.
op
,
Subtensor
):
raise
TypeError
(
'x must be result of a subtensor operation'
)
if
tolerate_inplace_aliasing
:
if
tolerate_inplace_aliasing
:
destroyhandler_tolerate_aliased
=
[[
0
,
1
]]
destroyhandler_tolerate_aliased
=
[[
0
,
1
]]
else
:
destroyhandler_tolerate_aliased
=
[]
the_op
=
IncSubtensor
(
x
.
owner
.
op
.
idx_list
,
inplace
,
set_instead_of_inc
,
destroyhandler_tolerate_aliased
=
destroyhandler_tolerate_aliased
)
real_x
=
x
.
owner
.
inputs
[
0
]
real_idxargs
=
x
.
owner
.
inputs
[
1
:]
return
the_op
(
real_x
,
y
,
*
real_idxargs
)
elif
isinstance
(
x
.
owner
.
op
,
AdvancedSubtensor1
):
real_x
=
x
.
owner
.
inputs
[
0
]
ilist
=
x
.
owner
.
inputs
[
1
]
if
set_instead_of_inc
:
raise
NotImplementedError
()
else
:
return
AdvancedIncSubtensor1
(
inplace
)(
real_x
,
y
,
ilist
)
elif
isinstance
(
x
.
owner
.
op
,
AdvancedSubtensor
):
raise
NotImplementedError
()
else
:
else
:
destroyhandler_tolerate_aliased
=
[]
raise
TypeError
(
'x must be result of a subtensor operation'
)
the_op
=
IncSubtensor
(
x
.
owner
.
op
.
idx_list
,
inplace
,
set_instead_of_inc
,
destroyhandler_tolerate_aliased
=
destroyhandler_tolerate_aliased
)
real_x
=
x
.
owner
.
inputs
[
0
]
real_idxargs
=
x
.
owner
.
inputs
[
1
:]
return
the_op
(
real_x
,
y
,
*
real_idxargs
)
class
IncSubtensor
(
Op
):
class
IncSubtensor
(
Op
):
...
@@ -4897,18 +4907,14 @@ class AdvancedIncSubtensor1(Op):
...
@@ -4897,18 +4907,14 @@ class AdvancedIncSubtensor1(Op):
y_
=
as_tensor_variable
(
y
)
y_
=
as_tensor_variable
(
y
)
ilist_
=
as_tensor_variable
(
ilist
)
ilist_
=
as_tensor_variable
(
ilist
)
assert
x_
.
type
.
dtype
==
y_
.
type
.
dtype
assert
x_
.
type
.
ndim
==
y_
.
type
.
ndim
if
ilist_
.
type
.
dtype
[:
3
]
not
in
(
'int'
,
'uin'
):
if
ilist_
.
type
.
dtype
[:
3
]
not
in
(
'int'
,
'uin'
):
raise
TypeError
(
'index must be integers'
)
raise
TypeError
(
'index must be integers'
)
if
ilist_
.
type
.
broadcastable
!=
(
False
,)
:
if
ilist_
.
type
.
ndim
!=
1
:
raise
TypeError
(
'index must be vector'
)
raise
TypeError
(
'index must be vector'
)
if
x_
.
type
.
ndim
==
0
:
if
x_
.
type
.
ndim
==
0
:
raise
TypeError
(
'cannot index into a scalar'
)
raise
TypeError
(
'cannot index into a scalar'
)
if
x_
.
type
.
broadcastable
[
0
]:
if
y_
.
type
.
ndim
>
x_
.
type
.
ndim
:
# the caller should have made a copy of x len(ilist) times
raise
TypeError
(
'cannot reduce dimensionality of y'
)
raise
TypeError
(
'cannot index into a broadcastable dimension'
)
return
Apply
(
self
,
[
x_
,
y_
,
ilist_
],
[
x_
.
type
()])
return
Apply
(
self
,
[
x_
,
y_
,
ilist_
],
[
x_
.
type
()])
...
@@ -4920,8 +4926,13 @@ class AdvancedIncSubtensor1(Op):
...
@@ -4920,8 +4926,13 @@ class AdvancedIncSubtensor1(Op):
x
=
x
.
copy
()
x
=
x
.
copy
()
# x[idx] += y don't work if the same index is present many times.
# x[idx] += y don't work if the same index is present many times.
# It do it only once
# It do it only once
for
(
j
,
i
)
in
enumerate
(
idx
):
# -- Numpy also behaves this way, is it a bug in numpy?
x
[
i
]
+=
y
[
j
]
if
y
.
ndim
:
for
(
j
,
i
)
in
enumerate
(
idx
):
x
[
i
]
+=
y
[
j
]
else
:
for
i
in
idx
:
x
[
i
]
+=
y
out
[
0
]
=
x
out
[
0
]
=
x
def
infer_shape
(
self
,
node
,
ishapes
):
def
infer_shape
(
self
,
node
,
ishapes
):
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
55ac73eb
...
@@ -2376,6 +2376,58 @@ class T_subtensor(unittest.TestCase):
...
@@ -2376,6 +2376,58 @@ class T_subtensor(unittest.TestCase):
val
=
f
()
val
=
f
()
self
.
assertTrue
(
numpy
.
allclose
(
val
,
data
[
idx
]
.
shape
))
self
.
assertTrue
(
numpy
.
allclose
(
val
,
data
[
idx
]
.
shape
))
class
TestIncSubtensor1
(
unittest
.
TestCase
):
# test inc_subtensor
# also tests set_subtensor
def
setUp
(
self
):
self
.
s
=
iscalar
()
self
.
v
=
fvector
()
self
.
m
=
dmatrix
()
self
.
t
=
ctensor3
()
self
.
adv1q
=
lvector
()
# advanced 1d query
def
test_cant_adv_idx_into_scalar
(
self
):
self
.
assertRaises
(
TypeError
,
lambda
:
self
.
s
[
self
.
adv1q
])
def
test_index_into_vec_w_vec
(
self
):
a
=
self
.
v
[
self
.
adv1q
]
assert
a
.
type
==
self
.
v
.
type
def
test_1d_set_adv_selection
(
self
):
a
=
set_subtensor
(
self
.
v
[
self
.
adv1q
],
self
.
v
[
self
.
adv1q
])
assert
a
.
type
==
self
.
v
.
type
#TODO: compile a function and verify that the subtensor is removed
# completely, because the whole expression is redundant.
f
=
theano
.
function
([
self
.
v
,
self
.
adv1q
],
a
,
allow_input_downcast
=
True
)
aval
=
f
([
.
4
,
.
9
,
.
1
],
[
1
,
2
])
assert
numpy
.
allclose
(
aval
,
[
.
4
,
0.9
,
0.1
])
def
test_1d_inc_adv_selection
(
self
):
a
=
inc_subtensor
(
self
.
v
[
self
.
adv1q
],
self
.
v
[
self
.
adv1q
])
assert
a
.
type
==
self
.
v
.
type
f
=
theano
.
function
([
self
.
v
,
self
.
adv1q
],
a
,
allow_input_downcast
=
True
)
aval
=
f
([
.
4
,
.
9
,
.
1
],
[
1
,
2
])
assert
numpy
.
allclose
(
aval
,
[
.
4
,
1.8
,
0.2
])
def
test_1d_inc_adv_selection_w_broadcasting
(
self
):
a
=
inc_subtensor
(
self
.
v
[
self
.
adv1q
],
3.0
)
assert
a
.
type
==
self
.
v
.
type
f
=
theano
.
function
([
self
.
v
,
self
.
adv1q
],
a
,
allow_input_downcast
=
True
)
aval
=
f
([
.
4
,
.
9
,
.
1
],
[
1
,
2
])
assert
numpy
.
allclose
(
aval
,
[
.
4
,
3.9
,
3.1
])
class
T_Join_and_Split
(
unittest
.
TestCase
):
class
T_Join_and_Split
(
unittest
.
TestCase
):
"""
"""
Split is tested by each verify_grad method.
Split is tested by each verify_grad method.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论