Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
73d798ab
提交
73d798ab
authored
9月 16, 2020
作者:
Brandon T. Willard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add general shape inference for all types of non-boolean indexing
上级
b210efbc
全部展开
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
115 行增加
和
15 行删除
+115
-15
test_subtensor.py
tests/tensor/test_subtensor.py
+115
-15
subtensor.py
theano/tensor/subtensor.py
+0
-0
没有找到文件。
tests/tensor/test_subtensor.py
浏览文件 @
73d798ab
import
logging
import
logging
import
sys
import
sys
import
pytest
import
pytest
import
numpy
as
np
import
numpy
as
np
import
theano
import
theano
import
theano.scalar
as
scal
import
theano.scalar
as
scal
import
theano.tensor
as
tensor
import
theano.tensor
as
tensor
from
six
import
StringIO
from
numpy.testing
import
assert_array_equal
from
numpy.testing
import
assert_array_equal
from
theano
import
config
from
six
import
StringIO
from
theano
import
config
,
change_flags
from
theano.compile
import
DeepCopyOp
from
theano.compile
import
DeepCopyOp
from
theano.gof.op
import
get_test_value
from
theano.gof.toolbox
import
is_same_graph
from
theano.gof.toolbox
import
is_same_graph
from
theano.tensor
import
(
from
theano.tensor
import
(
_shared
,
_shared
,
...
@@ -32,6 +38,8 @@ from theano.tensor import (
...
@@ -32,6 +38,8 @@ from theano.tensor import (
)
)
from
theano.tensor.basic
import
DimShuffle
from
theano.tensor.basic
import
DimShuffle
from
theano.tensor.subtensor
import
(
from
theano.tensor.subtensor
import
(
basic_shape
,
indexed_result_shape
,
AdvancedIncSubtensor
,
AdvancedIncSubtensor
,
AdvancedIncSubtensor1
,
AdvancedIncSubtensor1
,
AdvancedSubtensor
,
AdvancedSubtensor
,
...
@@ -45,7 +53,7 @@ from theano.tensor.subtensor import (
...
@@ -45,7 +53,7 @@ from theano.tensor.subtensor import (
inc_subtensor
,
inc_subtensor
,
set_subtensor
,
set_subtensor
,
)
)
from
theano
import
change_flags
from
theano
.tensor.type_other
import
make_slice
from
tests
import
unittest_tools
as
utt
from
tests
import
unittest_tools
as
utt
from
tests.tensor.test_basic
import
inplace_func
,
rand
,
randint_ranged
from
tests.tensor.test_basic
import
inplace_func
,
rand
,
randint_ranged
...
@@ -1815,8 +1823,7 @@ class TestAdvancedSubtensor:
...
@@ -1815,8 +1823,7 @@ class TestAdvancedSubtensor:
],
],
),
aval
),
aval
def
test_advanced_indexing
(
self
):
def
test_2d_3d_tensors
(
self
):
# tests advanced indexing in Theano for 2D and 3D tensors
rng
=
np
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
np
.
random
.
RandomState
(
utt
.
fetch_seed
())
a
=
rng
.
uniform
(
size
=
(
3
,
3
))
a
=
rng
.
uniform
(
size
=
(
3
,
3
))
b
=
theano
.
shared
(
a
)
b
=
theano
.
shared
(
a
)
...
@@ -1920,9 +1927,7 @@ class TestAdvancedSubtensor:
...
@@ -1920,9 +1927,7 @@ class TestAdvancedSubtensor:
class
TestInferShape
(
utt
.
InferShapeTester
):
class
TestInferShape
(
utt
.
InferShapeTester
):
@pytest.mark.slow
def
test_IncSubtensor
(
self
):
def
test_infer_shape
(
self
):
# IncSubtensor
admat
=
dmatrix
()
admat
=
dmatrix
()
bdmat
=
dmatrix
()
bdmat
=
dmatrix
()
advec
=
dvector
()
advec
=
dvector
()
...
@@ -2044,7 +2049,7 @@ class TestInferShape(utt.InferShapeTester):
...
@@ -2044,7 +2049,7 @@ class TestInferShape(utt.InferShapeTester):
IncSubtensor
,
IncSubtensor
,
)
)
# AdvancedIncSubtensor1
def
test_AdvancedIncSubtensor1
(
self
):
admat
=
dmatrix
()
admat
=
dmatrix
()
bdmat
=
dmatrix
()
bdmat
=
dmatrix
()
advec
=
dvector
()
advec
=
dvector
()
...
@@ -2074,6 +2079,7 @@ class TestInferShape(utt.InferShapeTester):
...
@@ -2074,6 +2079,7 @@ class TestInferShape(utt.InferShapeTester):
AdvancedIncSubtensor1
,
AdvancedIncSubtensor1
,
)
)
adtens4
=
dtensor4
()
bdtens4
=
dtensor4
()
bdtens4
=
dtensor4
()
adtens4_val
=
rand
(
4
,
3
,
2
,
5
)
adtens4_val
=
rand
(
4
,
3
,
2
,
5
)
aivec_val
=
[
2
,
3
]
aivec_val
=
[
2
,
3
]
...
@@ -2152,7 +2158,10 @@ class TestInferShape(utt.InferShapeTester):
...
@@ -2152,7 +2158,10 @@ class TestInferShape(utt.InferShapeTester):
AdvancedIncSubtensor1
,
AdvancedIncSubtensor1
,
)
)
# AdvancedIncSubtensor
def
test_AdvancedIncSubtensor
(
self
):
admat
=
dmatrix
()
advec
=
dvector
()
admat_val
=
rand
(
5
,
4
)
aivec_val
=
[
1
,
3
,
2
]
aivec_val
=
[
1
,
3
,
2
]
bivec_val
=
[
0
,
3
,
3
]
bivec_val
=
[
0
,
3
,
3
]
advec_val
=
[
23
,
24
,
25
]
advec_val
=
[
23
,
24
,
25
]
...
@@ -2163,7 +2172,7 @@ class TestInferShape(utt.InferShapeTester):
...
@@ -2163,7 +2172,7 @@ class TestInferShape(utt.InferShapeTester):
AdvancedIncSubtensor
,
AdvancedIncSubtensor
,
)
)
def
test_
adv_sub
(
self
):
def
test_
AdvancedSubtensor
(
self
):
admat
=
dmatrix
()
admat
=
dmatrix
()
aivec
=
lvector
()
aivec
=
lvector
()
bivec
=
lvector
()
bivec
=
lvector
()
...
@@ -2177,23 +2186,20 @@ class TestInferShape(utt.InferShapeTester):
...
@@ -2177,23 +2186,20 @@ class TestInferShape(utt.InferShapeTester):
[
admat_val
,
aivec_val
,
bivec_val
],
[
admat_val
,
aivec_val
,
bivec_val
],
AdvancedSubtensor
,
AdvancedSubtensor
,
)
)
# Test case that aren't implemented, but make sure they do not crash.
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
admat
,
aivec
],
[
admat
,
aivec
],
[
admat
[
aivec
,
1
:
3
]],
[
admat
[
aivec
,
1
:
3
]],
[
admat_val
,
aivec_val
],
[
admat_val
,
aivec_val
],
AdvancedSubtensor
,
AdvancedSubtensor
,
check_topo
=
False
,
)
)
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
admat
,
aivec
],
[
admat
,
aivec
],
[
admat
[
1
:
3
,
aivec
]],
[
admat
[
1
:
3
,
aivec
]],
[
admat_val
,
aivec_val
],
[
admat_val
,
aivec_val
],
AdvancedSubtensor
,
AdvancedSubtensor
,
check_topo
=
False
,
)
)
def
test_
boolean
(
self
):
def
test_
AdvancedBooleanSubtensor
(
self
):
n
=
dmatrix
()
n
=
dmatrix
()
n_val
=
np
.
arange
(
6
)
.
reshape
((
2
,
3
))
n_val
=
np
.
arange
(
6
)
.
reshape
((
2
,
3
))
...
@@ -2212,3 +2218,97 @@ class TestInferShape(utt.InferShapeTester):
...
@@ -2212,3 +2218,97 @@ class TestInferShape(utt.InferShapeTester):
tensor
.
AdvancedBooleanSubtensor
,
tensor
.
AdvancedBooleanSubtensor
,
check_topo
=
False
,
check_topo
=
False
,
)
)
@change_flags
(
compute_test_value
=
"raise"
)
def
test_basic_shape
():
test_shape
=
(
5
,
4
)
test_indices
=
(
make_slice
(
1
,
3
,
None
),)
res
=
basic_shape
(
test_shape
,
test_indices
)
assert
get_test_value
(
res
)
==
(
2
,)
@change_flags
(
compute_test_value
=
"raise"
)
def
test_indexed_result_shape
():
_test_idx
=
np
.
ix_
(
np
.
array
([
True
,
True
]),
np
.
array
([
True
]),
np
.
array
([
True
,
True
]))
test_shape
=
(
5
,
6
,
7
,
8
)
test_array
=
np
.
arange
(
np
.
prod
(
test_shape
))
.
reshape
(
test_shape
)
def
idx_as_tensor
(
x
):
if
isinstance
(
x
,
(
slice
,
type
(
None
))):
return
x
else
:
return
tensor
.
as_tensor
(
x
)
def
bcast_shape_tuple
(
x
):
if
not
hasattr
(
x
,
"shape"
):
return
x
return
tuple
(
s
if
not
bcast
else
1
for
s
,
bcast
in
zip
(
tuple
(
x
.
shape
),
x
.
broadcastable
)
)
def
compare_index_shapes
(
test_array
,
test_idx
):
res
=
indexed_result_shape
(
tensor
.
as_tensor
(
test_array
)
.
shape
,
[
idx_as_tensor
(
i
)
for
i
in
test_idx
]
)
exp_res
=
test_array
[
test_idx
]
.
shape
assert
np
.
array_equal
(
tuple
(
get_test_value
(
r
)
for
r
in
res
),
exp_res
)
# Test shape-only version
res
=
indexed_result_shape
(
tensor
.
as_tensor
(
test_array
)
.
shape
,
[
bcast_shape_tuple
(
idx_as_tensor
(
i
))
for
i
in
test_idx
],
indices_are_shapes
=
True
,
)
exp_res
=
test_array
[
test_idx
]
.
shape
assert
np
.
array_equal
(
tuple
(
get_test_value
(
r
)
for
r
in
res
),
exp_res
)
# Simple basic indices
test_idx
=
(
slice
(
None
,
None
),)
compare_index_shapes
(
test_array
,
test_idx
)
# Advanced indices
test_idx
=
(
2
,)
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
_test_idx
[:
1
]
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
_test_idx
[:
2
]
compare_index_shapes
(
test_array
,
test_idx
)
# A Mix of advanced and basic indices
test_idx
=
_test_idx
[:
2
]
+
(
slice
(
None
,
None
),)
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
(
slice
(
None
,
None
),)
+
_test_idx
[
1
:]
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
(
slice
(
None
,
None
),
None
)
+
_test_idx
[
1
:
2
]
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
(
np
.
array
(
1
),
slice
(
None
,
None
),
None
)
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
(
slice
(
None
,
None
),
None
,
np
.
array
(
1
))
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
_test_idx
[:
1
]
+
(
slice
(
None
,
None
),)
+
_test_idx
[
1
:
2
]
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
(
_test_idx
[:
1
]
+
(
slice
(
None
,
None
),)
+
_test_idx
[
1
:
2
]
+
(
slice
(
None
,
None
),)
)
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
_test_idx
[:
1
]
+
(
None
,)
+
_test_idx
[
1
:
2
]
compare_index_shapes
(
test_array
,
test_idx
)
test_shape
=
(
5
,
4
)
test_array
=
np
.
arange
(
np
.
prod
(
test_shape
))
.
reshape
(
test_shape
)
test_idx
=
([
1
,
3
,
2
],
slice
(
1
,
3
))
compare_index_shapes
(
test_array
,
test_idx
)
test_idx
=
(
slice
(
1
,
3
),
[
1
,
3
,
2
])
compare_index_shapes
(
test_array
,
test_idx
)
theano/tensor/subtensor.py
浏览文件 @
73d798ab
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论