Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f5207388
提交
f5207388
authored
8月 15, 2016
作者:
Frédéric Bastien
提交者:
GitHub
8月 15, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4853 from lamblin/Ellipsis
Allow indexing using Ellipsis.
上级
0c53299e
9c22f61e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
31 行删除
+81
-31
test_subtensor.py
theano/tensor/tests/test_subtensor.py
+63
-31
var.py
theano/tensor/var.py
+18
-0
没有找到文件。
theano/tensor/tests/test_subtensor.py
浏览文件 @
f5207388
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
division
,
print_function
import
logging
import
sys
import
unittest
from
nose.plugins.skip
import
SkipTest
import
numpy
from
nose.plugins.skip
import
SkipTest
from
nose.tools
import
assert_equal
from
numpy.testing
import
assert_array_equal
from
six
import
StringIO
from
six.moves
import
xrange
import
theano
from
theano.compat
import
exc_message
,
izip
,
PY3
from
theano.compile
import
DeepCopyOp
from
theano
import
config
from
theano
import
gof
import
theano.scalar
as
scal
import
theano.tensor
as
tensor
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.subtensor
import
(
inc_subtensor
,
set_subtensor
,
from
theano
import
config
,
gof
from
theano.compat
import
PY3
,
exc_message
,
izip
from
theano.compile
import
DeepCopyOp
from
theano.tensor
import
(
MakeSlice
,
NotScalarConstantError
,
_shared
,
as_tensor_variable
,
cscalar
,
ctensor3
,
dmatrix
,
dscalar
,
dtensor4
,
dvector
,
fmatrix
,
fscalar
,
fvector
,
iscalar
,
lmatrix
,
lrow
,
lvector
,
matrix
,
vector
)
from
theano.tensor.basic
import
DimShuffle
from
theano.tensor.subtensor
import
(
AdvancedIncSubtensor
,
AdvancedIncSubtensor1
,
AdvancedSubtensor
,
AdvancedSubtensor1
,
IncSubtensor
,
Subtensor
,
advanced_inc_subtensor
,
advanced_inc_subtensor1
,
advanced_set_subtensor1
,
advanced_inc_subtensor
,
advanced_set_subtensor
,
Subtensor
,
IncSubtensor
,
AdvancedSubtensor1
,
AdvancedSubtensor
,
advanced_subtensor1
,
inplace_increment
,
AdvancedIncSubtensor1
,
AdvancedIncSubtensor
,
get_canonical_form_slice
)
from
theano.tensor
import
(
as_tensor_variable
,
_shared
,
NotScalarConstantError
,
fscalar
,
iscalar
,
dscalar
,
cscalar
,
vector
,
dvector
,
fvector
,
lvector
,
lrow
,
fmatrix
,
dmatrix
,
lmatrix
,
matrix
,
ctensor3
,
dtensor4
)
from
theano.tensor.tests.test_basic
import
rand
,
randint_ranged
,
inplace_func
advanced_set_subtensor1
,
advanced_subtensor1
,
get_canonical_form_slice
,
inc_subtensor
,
inplace_increment
,
set_subtensor
)
from
theano.tensor.tests.test_basic
import
inplace_func
,
rand
,
randint_ranged
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests.unittest_tools
import
attr
if
PY3
:
...
...
@@ -65,6 +66,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self
.
adv_incsub1
=
adv_incsub1
if
mode
is
None
:
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
mode
.
including
(
"local_useless_subtensor"
)
self
.
mode
=
mode
self
.
dtype
=
dtype
self
.
type
=
type
...
...
@@ -97,18 +99,18 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
Subtensor
.
debug
=
False
utt
.
seed_rng
()
def
eval_output_and_check
(
self
,
t
,
list
=
False
,
mode
=
None
):
def
eval_output_and_check
(
self
,
t
,
op_type
=
None
,
mode
=
None
,
length
=
1
):
if
op_type
is
None
:
op_type
=
self
.
sub
if
mode
is
None
:
mode
=
self
.
mode
f
=
inplace_func
([],
t
,
mode
=
mode
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo_
=
[
node
for
node
in
topo
if
not
isinstance
(
node
.
op
,
self
.
ignore_topo
)]
assert
len
(
topo_
)
==
1
if
not
list
:
assert
isinstance
(
topo_
[
0
]
.
op
,
self
.
sub
)
else
:
assert
isinstance
(
topo_
[
0
]
.
op
,
self
.
adv_sub1
)
assert_equal
(
len
(
topo_
),
length
)
if
length
==
1
:
assert
isinstance
(
topo_
[
0
]
.
op
,
op_type
)
tval
=
f
()
return
tval
...
...
@@ -337,6 +339,36 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
ret
=
f
()
assert
ret
.
shape
==
(
1
,
1
,
4
)
def
test_ellipsis
(
self
):
numpy_n
=
numpy
.
arange
(
24
,
dtype
=
self
.
dtype
)
.
reshape
((
2
,
3
,
4
))
n
=
self
.
shared
(
numpy_n
)
test_cases
=
[
(
0
,
self
.
sub
,
numpy
.
index_exp
[
...
]),
(
1
,
self
.
sub
,
numpy
.
index_exp
[
...
,
1
]),
(
1
,
self
.
sub
,
numpy
.
index_exp
[
1
,
...
]),
(
1
,
self
.
sub
,
numpy
.
index_exp
[
...
,
1
,
2
,
3
]),
(
1
,
self
.
sub
,
numpy
.
index_exp
[
1
,
...
,
2
,
3
]),
(
1
,
self
.
sub
,
numpy
.
index_exp
[
1
,
2
,
3
,
...
]),
(
3
,
DimShuffle
,
numpy
.
index_exp
[
...
,
[
0
,
2
,
3
]]),
(
1
,
DimShuffle
,
numpy
.
index_exp
[
numpy
.
newaxis
,
...
])]
# The following test case is not supported by numpy before 1.9
numpy_version
=
[
int
(
v
)
for
v
in
numpy
.
version
.
version
.
split
(
'.'
)[
0
:
2
]]
if
numpy_version
>=
[
1
,
9
]:
test_cases
.
append
(
(
1
,
AdvancedSubtensor
,
numpy
.
index_exp
[
...
,
numpy
.
newaxis
,
[
1
,
2
]]))
for
length
,
op_type
,
slice_
in
test_cases
:
numpy_tval
=
numpy_n
[
slice_
]
t
=
n
[
slice_
]
self
.
assertTrue
(
isinstance
(
t
.
owner
.
op
,
op_type
))
tval
=
self
.
eval_output_and_check
(
t
,
op_type
=
op_type
,
length
=
length
)
assert_equal
(
tval
.
shape
,
numpy_tval
.
shape
)
assert_array_equal
(
tval
,
numpy_tval
)
def
test_newaxis
(
self
):
"""
newaxis support comes from logic in the __getitem__ of TensorType
...
...
@@ -433,7 +465,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
topo_
=
[
node
for
node
in
topo
if
not
isinstance
(
node
.
op
,
self
.
ignore_topo
)]
if
not
self
.
fast_compile
:
assert
len
(
topo_
)
==
6
assert
_equal
(
len
(
topo_
),
6
)
assert
numpy
.
sum
([
isinstance
(
node
.
op
,
self
.
inc_sub
)
for
node
in
topo_
])
==
1
assert
numpy
.
sum
([
isinstance
(
node
.
op
,
self
.
sub
)
...
...
@@ -467,7 +499,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# We test again AdvancedSubtensor1 as we transfer data to the cpu.
self
.
assertTrue
(
isinstance
(
t
.
owner
.
op
,
tensor
.
AdvancedSubtensor1
))
val
=
self
.
eval_output_and_check
(
t
,
list
=
True
)
val
=
self
.
eval_output_and_check
(
t
,
op_type
=
self
.
adv_sub1
)
if
isinstance
(
idx
,
list
):
good
=
data
[
idx
]
else
:
...
...
theano/tensor/var.py
浏览文件 @
f5207388
...
...
@@ -471,6 +471,24 @@ class _tensor_py_operators(object):
pass
elif
not
isinstance
(
args
,
tuple
):
args
=
args
,
# Convert an Ellipsis if provided into an appropriate number of
# slice(None).
ellipses
=
[
i
for
i
,
index
in
enumerate
(
args
)
if
index
is
Ellipsis
]
if
len
(
ellipses
)
>
1
:
raise
IndexError
(
"an index can only have a single Ellipsis (`...`)"
)
elif
len
(
ellipses
)
==
1
:
new_axes
=
sum
(
1
for
index
in
args
if
index
is
numpy
.
newaxis
)
# numpy.newaxis is None
ellipsis_at
=
ellipses
[
0
]
args
=
list
(
args
)
args
[
ellipsis_at
:
ellipsis_at
+
1
]
=
(
[
slice
(
None
)]
*
(
self
.
ndim
-
(
len
(
args
)
-
1
-
new_axes
)))
# Convert python literals to theano constants
args
=
theano
.
tensor
.
subtensor
.
make_constant
(
args
)
# Determine if advanced indexing is needed or not
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论