Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4b875a0d
提交
4b875a0d
authored
6月 13, 2012
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
adding support for numpy.newaxis in basic indexing
上级
501d5338
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
68 行增加
和
3 行删除
+68
-3
basic.py
theano/tensor/basic.py
+25
-3
test_basic.py
theano/tensor/tests/test_basic.py
+43
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
4b875a0d
...
@@ -1543,7 +1543,7 @@ class _tensor_py_operators:
...
@@ -1543,7 +1543,7 @@ class _tensor_py_operators:
advanced
=
False
advanced
=
False
for
arg
in
args
:
for
arg
in
args
:
try
:
try
:
Subtensor
.
convert
(
arg
)
arg
==
numpy
.
newaxis
or
Subtensor
.
convert
(
arg
)
except
AdvancedIndexingError
:
except
AdvancedIndexingError
:
advanced
=
True
advanced
=
True
break
break
...
@@ -1559,8 +1559,30 @@ class _tensor_py_operators:
...
@@ -1559,8 +1559,30 @@ class _tensor_py_operators:
else
:
else
:
return
AdvancedSubtensor
()(
self
,
*
args
)
return
AdvancedSubtensor
()(
self
,
*
args
)
else
:
else
:
return
Subtensor
(
args
)(
self
,
*
Subtensor
.
collapse
(
args
,
if
numpy
.
newaxis
in
args
:
lambda
entry
:
isinstance
(
entry
,
Variable
)))
# None (aka np.newaxis) in numpy indexing means to add a
# broadcastable dimension, which theano traditionally did with
# the dimshuffle op. The following code converts numpy-style
# indexing on self to traditional [read: implemented] theano
# indexing on a dimshuffled view of self.
counter
=
0
pattern
=
[]
new_args
=
[]
for
arg
in
args
:
if
arg
==
numpy
.
newaxis
:
pattern
.
append
(
'x'
)
new_args
.
append
(
slice
(
None
,
None
,
None
))
else
:
pattern
.
append
(
counter
)
counter
+=
1
new_args
.
append
(
arg
)
view
=
self
.
dimshuffle
(
pattern
)
rval
=
view
.
__getitem__
(
tuple
(
new_args
))
return
rval
else
:
return
Subtensor
(
args
)(
self
,
*
Subtensor
.
collapse
(
args
,
lambda
entry
:
isinstance
(
entry
,
Variable
)))
#COPYING
#COPYING
def
copy
(
self
):
def
copy
(
self
):
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
4b875a0d
...
@@ -2358,6 +2358,49 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
...
@@ -2358,6 +2358,49 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self
.
assertTrue
(
tval
.
shape
==
())
self
.
assertTrue
(
tval
.
shape
==
())
self
.
assertTrue
(
numpy
.
all
(
tval
==
0
))
self
.
assertTrue
(
numpy
.
all
(
tval
==
0
))
def
test_newaxis
(
self
):
"""
newaxis support comes from logic in the __getitem__ of TensorType
Variables, which currently inserts dimshuffle to get the right number
of dimensions, and adjusts the slice tuple accordingly.
So testing is done via square-bracket notation rather than direct
interaction with the Subtensor Op (which has no support of its own for
newaxis).
"""
newaxis
=
numpy
.
newaxis
n
=
self
.
shared
(
numpy
.
asarray
(
range
(
24
),
dtype
=
self
.
dtype
)
.
reshape
((
2
,
3
,
4
)))
assert
n
.
ndim
==
3
n4
=
n
[
newaxis
,
:,
:,
:]
assert
n4
.
broadcastable
==
(
True
,
False
,
False
,
False
),
n4
n4
=
n
[:,
newaxis
,
:,
:]
assert
n4
.
broadcastable
==
(
False
,
True
,
False
,
False
),
n4
n4
=
n
[:,
:,
newaxis
,
:]
assert
n4
.
broadcastable
==
(
False
,
False
,
True
,
False
),
n4
n4
=
n
[:,
:,
:,
newaxis
]
assert
n4
.
broadcastable
==
(
False
,
False
,
False
,
True
),
n4
n3
=
n
.
flatten
()[
newaxis
,
:,
newaxis
]
assert
n3
.
broadcastable
==
(
True
,
False
,
True
),
n3
s
=
cscalar
()
s1
=
s
[
newaxis
]
assert
s1
.
broadcastable
==
(
True
,),
s1
vs1
,
vn3
,
vn4
=
theano
.
function
([
s
],
[
s1
,
n3
,
n4
])(
-
2.0
)
assert
numpy
.
all
(
vs1
==
[
-
2.0
])
assert
numpy
.
all
(
vn3
==
numpy
.
arange
(
24
)[
newaxis
,
:,
newaxis
])
assert
numpy
.
all
(
vn4
==
numpy
.
arange
(
24
)
.
reshape
((
2
,
3
,
4
))[:,
:,
:,
newaxis
])
def
test_grad_1d
(
self
):
def
test_grad_1d
(
self
):
subi
=
0
subi
=
0
data
=
numpy
.
asarray
(
rand
(
2
,
3
),
dtype
=
self
.
dtype
)
data
=
numpy
.
asarray
(
rand
(
2
,
3
),
dtype
=
self
.
dtype
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论