Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4940bb33
提交
4940bb33
authored
8月 21, 2015
作者:
Cesar Laurent
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Numpy-like interface for stack.
上级
00f184d5
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
89 行增加
和
1 行删除
+89
-1
basic.txt
doc/library/tensor/basic.txt
+34
-0
basic.py
theano/tensor/basic.py
+38
-1
test_basic.py
theano/tensor/tests/test_basic.py
+17
-0
没有找到文件。
doc/library/tensor/basic.txt
浏览文件 @
4940bb33
...
@@ -583,6 +583,20 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
...
@@ -583,6 +583,20 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
:type n_ones: int
:type n_ones: int
:type n_ones: number of dimension to be added to `x`
:type n_ones: number of dimension to be added to `x`
.. function:: shape_padaxis(t, axis)
Reshape `t` by adding 1 at the dimension `axis`. Note that this new
dimension will be broadcastable. To make it non-broadcastable
see the :func:`unbroadcast`.
:type x: any TensorVariable (or compatible)
:param x: variable to be reshaped
:type axis: int
:param axis: axis where to add the new dimension to `x`
.. autofunction:: unbroadcast(x, *axes)
.. autofunction:: unbroadcast(x, *axes)
.. autofunction:: addbroadcast(x, *axes)
.. autofunction:: addbroadcast(x, *axes)
...
@@ -678,6 +692,26 @@ Creating Tensor
...
@@ -678,6 +692,26 @@ Creating Tensor
except for the main diagonal, whose values are equal to one. The output
except for the main diagonal, whose values are equal to one. The output
will have same dtype as `x`.
will have same dtype as `x`.
.. function:: stack(tensors, axis=0)
Warning: The interface stack(*tensors) is deprecated!
Return a Tensor representing for the arguments all stacked up into a single Tensor.
(of 1 rank greater).
:param tensors: a list or a tuple of one or more tensors of the same rank.
:param axis: the axis along which the tensors will be stacked.
:returns: A tensor such that rval[0] == tensors[0], rval[1] == tensors[1], etc.
>>> x0 = T.scalar()
>>> x1 = T.scalar()
>>> x2 = T.scalar()
>>> x = T.stack(x0, x1, x2)
>>> x.ndim # x is a vector of length 3.
1
.. function:: stack(*tensors)
.. function:: stack(*tensors)
Return a Tensor representing for the arguments all stacked up into a single Tensor.
Return a Tensor representing for the arguments all stacked up into a single Tensor.
...
...
theano/tensor/basic.py
浏览文件 @
4940bb33
...
@@ -3962,6 +3962,19 @@ def shape_padright(t, n_ones=1):
...
@@ -3962,6 +3962,19 @@ def shape_padright(t, n_ones=1):
return
DimShuffle
(
_t
.
broadcastable
,
pattern
)(
_t
)
return
DimShuffle
(
_t
.
broadcastable
,
pattern
)(
_t
)
@constructor
def
shape_padaxis
(
t
,
axis
):
"""Reshape `t` by adding 1 at the dimension `axis`.
See also: `shape_padleft`, `shape_padright` and `Dimshuffle`
"""
_t
=
as_tensor_variable
(
t
)
pattern
=
[
i
for
i
in
xrange
(
_t
.
type
.
ndim
)]
pattern
.
insert
(
axis
,
'x'
)
return
DimShuffle
(
_t
.
broadcastable
,
pattern
)(
_t
)
@constructor
@constructor
def
stack
(
*
tensors
):
def
stack
(
*
tensors
):
"""Insert the arguments as slices into a tensor of 1 rank greater.
"""Insert the arguments as slices into a tensor of 1 rank greater.
...
@@ -3969,10 +3982,34 @@ def stack(*tensors):
...
@@ -3969,10 +3982,34 @@ def stack(*tensors):
The size in dimension 0 of the result will be equal to the number
The size in dimension 0 of the result will be equal to the number
of tensors passed.
of tensors passed.
Note: The interface stack(*tensors) is deprecated, you should use
stack(tensors, axis=0) insted.
:Parameters:
- `tensors` : list or tuple of tensors
A list of tensors to be stacked.
- `axis` : int
The index of the new axis.
"""
"""
if
len
(
tensors
)
==
0
:
if
len
(
tensors
)
==
0
:
raise
Exception
(
'theano.tensor.stack(*tensors) must have at least'
raise
Exception
(
'theano.tensor.stack(*tensors) must have at least'
' one parameter'
)
' one parameter'
)
# Remove this when moving to the new interface: stack(tensors, axis=0)
# New numpy-like interface:
if
isinstance
(
tensors
[
0
],
(
list
,
tuple
)):
if
len
(
tensors
)
==
1
:
axis
=
0
else
:
axis
=
tensors
[
1
]
tensors
=
tensors
[
0
]
# Deprecated interface:
else
:
warnings
.
warn
(
'stack(*tensors) interface is deprecated, use'
' stack(tensors, axis=0) instead.'
,
stacklevel
=
3
)
axis
=
0
# If all tensors are scalars of the same type, call make_vector.
# If all tensors are scalars of the same type, call make_vector.
# It makes the graph simpler, by not adding DimShuffles and Rebroadcasts
# It makes the graph simpler, by not adding DimShuffles and Rebroadcasts
...
@@ -3994,7 +4031,7 @@ def stack(*tensors):
...
@@ -3994,7 +4031,7 @@ def stack(*tensors):
tensors
=
list
(
map
(
as_tensor_variable
,
tensors
))
tensors
=
list
(
map
(
as_tensor_variable
,
tensors
))
dtype
=
scal
.
upcast
(
*
[
i
.
dtype
for
i
in
tensors
])
dtype
=
scal
.
upcast
(
*
[
i
.
dtype
for
i
in
tensors
])
return
theano
.
tensor
.
opt
.
MakeVector
(
dtype
)(
*
tensors
)
return
theano
.
tensor
.
opt
.
MakeVector
(
dtype
)(
*
tensors
)
return
join
(
0
,
*
[
shape_padleft
(
t
,
1
)
for
t
in
tensors
])
return
join
(
axis
,
*
[
shape_padaxis
(
t
,
axis
)
for
t
in
tensors
])
@constructor
@constructor
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
4940bb33
...
@@ -3441,6 +3441,23 @@ class T_Join_and_Split(unittest.TestCase):
...
@@ -3441,6 +3441,23 @@ class T_Join_and_Split(unittest.TestCase):
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
,
type
(
self
.
join_op
))])
==
0
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
,
type
(
self
.
join_op
))])
==
0
assert
f
.
maker
.
fgraph
.
outputs
[
0
]
.
dtype
==
'int64'
assert
f
.
maker
.
fgraph
.
outputs
[
0
]
.
dtype
==
'int64'
def
test_stack_new_interface
(
self
):
"""Test the new numpy-like interface: stack(tensors, axis=0)."""
a
=
tensor
.
imatrix
(
'a'
)
b
=
tensor
.
imatrix
(
'b'
)
s1
=
stack
(
a
,
b
)
s2
=
stack
([
a
,
b
])
f
=
function
([
a
,
b
],
[
s1
,
s2
],
mode
=
self
.
mode
)
v1
,
v2
=
f
([[
1
,
2
]],
[[
3
,
4
]])
self
.
assertTrue
(
v1
.
shape
==
v2
.
shape
)
self
.
assertTrue
(
numpy
.
all
(
v1
==
v2
))
s3
=
stack
([
a
,
b
],
1
)
f
=
function
([
a
,
b
],
s3
,
mode
=
self
.
mode
)
v3
=
f
([[
1
,
2
]],
[[
3
,
4
]])
v4
=
numpy
.
array
([[[
1
,
2
],
[
3
,
4
]]])
self
.
assertTrue
(
v3
.
shape
==
v4
.
shape
)
self
.
assertTrue
(
numpy
.
all
(
v3
==
v4
))
def
test_stack_hessian
(
self
):
def
test_stack_hessian
(
self
):
# Test the gradient of stack when used in hessian, see gh-1589
# Test the gradient of stack when used in hessian, see gh-1589
a
=
tensor
.
dvector
(
'a'
)
a
=
tensor
.
dvector
(
'a'
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论