Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
262f59a2
提交
262f59a2
authored
1月 29, 2014
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1700 from MarcCote/cumsum
Added the cumsum and cumprod functions similar to numpy's ones.
上级
e02ce0d8
5c504099
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
304 行增加
和
4 行删除
+304
-4
__init__.py
theano/tensor/__init__.py
+1
-1
extra_ops.py
theano/tensor/extra_ops.py
+212
-0
test_extra_ops.py
theano/tensor/tests/test_extra_ops.py
+91
-3
没有找到文件。
theano/tensor/__init__.py
浏览文件 @
262f59a2
...
@@ -62,4 +62,4 @@ from theano.gradient import Rop, Lop, grad, numeric_grad, verify_grad, \
...
@@ -62,4 +62,4 @@ from theano.gradient import Rop, Lop, grad, numeric_grad, verify_grad, \
from
theano.tensor.sort
import
sort
,
argsort
from
theano.tensor.sort
import
sort
,
argsort
from
theano.tensor.extra_ops
import
(
DiffOp
,
bincount
,
squeeze
,
from
theano.tensor.extra_ops
import
(
DiffOp
,
bincount
,
squeeze
,
repeat
,
bartlett
,
fill_diagonal
)
repeat
,
bartlett
,
fill_diagonal
,
cumsum
,
cumprod
)
theano/tensor/extra_ops.py
浏览文件 @
262f59a2
...
@@ -8,6 +8,218 @@ tensor = basic
...
@@ -8,6 +8,218 @@ tensor = basic
from
theano.gradient
import
DisconnectedType
from
theano.gradient
import
DisconnectedType
class
CumsumOp
(
theano
.
Op
):
# See function cumsum for docstring
def
__init__
(
self
,
axis
=
None
):
self
.
axis
=
axis
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
axis
==
other
.
axis
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
axis
)
def
make_node
(
self
,
x
):
x
=
basic
.
as_tensor_variable
(
x
)
out_type
=
x
.
type
()
if
self
.
axis
is
None
:
out_type
=
theano
.
tensor
.
vector
(
dtype
=
x
.
dtype
)
# Flatten
return
theano
.
Apply
(
self
,
[
x
],
[
out_type
])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
x
=
inputs
[
0
]
z
=
output_storage
[
0
]
z
[
0
]
=
np
.
cumsum
(
x
,
axis
=
self
.
axis
)
def
grad
(
self
,
inputs
,
output_gradients
):
[
gi
]
=
output_gradients
if
self
.
axis
is
None
:
return
[
cumsum
(
gi
[::
-
1
])[::
-
1
]
.
reshape
(
inputs
[
0
]
.
shape
)]
# We need to reverse the gradients along ``self.axis``,
# compute cumsum, then reverse again
reverse_slicing
=
[
slice
(
None
,
None
,
None
)]
*
gi
.
ndim
reverse_slicing
[
self
.
axis
]
=
slice
(
None
,
None
,
-
1
)
reverse_slicing
=
tuple
(
reverse_slicing
)
return
[
cumsum
(
gi
[
reverse_slicing
],
self
.
axis
)[
reverse_slicing
]]
def
infer_shape
(
self
,
node
,
shapes
):
if
self
.
axis
is
None
:
return
[(
tensor
.
prod
(
shapes
[
0
]),)]
# Flatten
return
shapes
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
x
,
=
inames
z
,
=
onames
axis
=
self
.
axis
fail
=
sub
[
'fail'
]
if
self
.
axis
is
None
or
(
self
.
axis
==
0
and
node
.
inputs
[
0
]
.
ndim
==
1
):
code
=
"""
npy_intp shape[1] = { PyArray_SIZE(
%(x)
s) };
if(!(
%(z)
s && PyArray_DIMS(
%(z)
s)[0] == shape[0]))
{
Py_XDECREF(
%(z)
s);
%(z)
s = (PyArrayObject*) PyArray_SimpleNew(1, shape, type_num_
%(x)
s);
}
if (!
%(z)
s)
%(fail)
s;
{
PyArray_CumSum(
%(x)
s, NPY_MAXDIMS, type_num_
%(x)
s,
%(z)
s);
}
"""
%
locals
()
else
:
code
=
"""
if(!(
%(z)
s && PyArray_CompareLists(PyArray_DIMS(
%(z)
s), PyArray_DIMS(
%(x)
s), PyArray_NDIM(
%(x)
s)) ))
{
Py_XDECREF(
%(z)
s);
%(z)
s = (PyArrayObject*) PyArray_SimpleNew(PyArray_NDIM(
%(x)
s), PyArray_DIMS(
%(x)
s), type_num_
%(x)
s);
}
if (!
%(z)
s)
%(fail)
s;
{
PyArray_CumSum(
%(x)
s,
%(axis)
s, type_num_
%(x)
s,
%(z)
s);
}
"""
%
locals
()
return
code
def
c_code_cache_version
(
self
):
return
(
1
,)
def
__str__
(
self
):
return
"
%
s{
%
s}"
%
(
self
.
__class__
.
__name__
,
self
.
axis
)
def
cumsum
(
x
,
axis
=
None
):
"""Return the cumulative sum of the elements along a given axis.
Wraping of numpy.cumsum.
:param x: Input tensor variable.
:param axis: The axis along which the cumulative sum is computed.
The default (None) is to compute the cumsum over the flattened array.
.. versionadded:: 0.6.1
"""
return
CumsumOp
(
axis
=
axis
)(
x
)
class
CumprodOp
(
theano
.
Op
):
# See function cumprod for docstring
def
__init__
(
self
,
axis
=
None
):
self
.
axis
=
axis
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
axis
==
other
.
axis
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
axis
)
def
make_node
(
self
,
x
):
x
=
basic
.
as_tensor_variable
(
x
)
out_type
=
x
.
type
()
if
self
.
axis
is
None
:
out_type
=
theano
.
tensor
.
vector
(
dtype
=
x
.
dtype
)
# Flatten
return
theano
.
Apply
(
self
,
[
x
],
[
out_type
])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
x
=
inputs
[
0
]
z
=
output_storage
[
0
]
z
[
0
]
=
np
.
cumprod
(
x
,
axis
=
self
.
axis
)
def
grad
(
self
,
inputs
,
output_gradients
):
x
,
=
inputs
gi
,
=
output_gradients
fx
=
cumprod
(
x
,
axis
=
self
.
axis
)
if
self
.
axis
is
None
:
return
[
cumsum
((
fx
*
gi
)[::
-
1
])[::
-
1
]
.
reshape
(
inputs
[
0
]
.
shape
)
/
x
]
# We need to reverse the gradients along ``self.axis``,
# compute cumsum, then reverse again
reverse_slicing
=
[
slice
(
None
,
None
,
None
)]
*
gi
.
ndim
reverse_slicing
[
self
.
axis
]
=
slice
(
None
,
None
,
-
1
)
reverse_slicing
=
tuple
(
reverse_slicing
)
return
[
cumsum
((
fx
*
gi
)[
reverse_slicing
],
self
.
axis
)[
reverse_slicing
]
/
x
]
def
infer_shape
(
self
,
node
,
shapes
):
if
self
.
axis
is
None
:
return
[(
tensor
.
prod
(
shapes
[
0
]),)]
# Flatten
return
shapes
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
x
,
=
inames
z
,
=
onames
axis
=
self
.
axis
fail
=
sub
[
'fail'
]
if
self
.
axis
is
None
or
(
self
.
axis
==
0
and
node
.
inputs
[
0
]
.
ndim
==
1
):
code
=
"""
npy_intp shape[1] = { PyArray_SIZE(
%(x)
s) };
if(!(
%(z)
s && PyArray_DIMS(
%(z)
s)[0] == shape[0]))
{
Py_XDECREF(
%(z)
s);
%(z)
s = (PyArrayObject*) PyArray_SimpleNew(1, shape, type_num_
%(x)
s);
}
if (!
%(z)
s)
%(fail)
s;
{
PyArray_CumProd(
%(x)
s, NPY_MAXDIMS, type_num_
%(x)
s,
%(z)
s);
}
"""
%
locals
()
else
:
code
=
"""
if(!(
%(z)
s && PyArray_CompareLists(PyArray_DIMS(
%(z)
s), PyArray_DIMS(
%(x)
s), PyArray_NDIM(
%(x)
s)) ))
{
Py_XDECREF(
%(z)
s);
%(z)
s = (PyArrayObject*) PyArray_SimpleNew(PyArray_NDIM(
%(x)
s), PyArray_DIMS(
%(x)
s), type_num_
%(x)
s);
}
if (!
%(z)
s)
%(fail)
s;
{
PyArray_CumProd(
%(x)
s,
%(axis)
s, type_num_
%(x)
s,
%(z)
s);
}
"""
%
locals
()
return
code
def
c_code_cache_version
(
self
):
return
(
1
,)
def
__str__
(
self
):
return
"
%
s{
%
s}"
%
(
self
.
__class__
.
__name__
,
self
.
axis
)
def
cumprod
(
x
,
axis
=
None
):
"""Return the cumulative product of the elements along a given axis.
Wraping of numpy.cumprod.
:param x: Input tensor variable.
:param axis: The axis along which the cumulative product is computed.
The default (None) is to compute the cumprod over the flattened array.
.. versionadded:: 0.6.1
"""
return
CumprodOp
(
axis
=
axis
)(
x
)
class
DiffOp
(
theano
.
Op
):
class
DiffOp
(
theano
.
Op
):
# See function diff for docstring
# See function diff for docstring
def
__init__
(
self
,
n
=
1
,
axis
=-
1
):
def
__init__
(
self
,
n
=
1
,
axis
=-
1
):
...
...
theano/tensor/tests/test_extra_ops.py
浏览文件 @
262f59a2
...
@@ -3,9 +3,11 @@ import numpy
...
@@ -3,9 +3,11 @@ import numpy
import
theano
import
theano
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.extra_ops
import
(
BinCountOp
,
bincount
,
DiffOp
,
diff
,
squeeze
,
RepeatOp
,
repeat
,
Bartlett
,
bartlett
,
from
theano.tensor.extra_ops
import
(
CumsumOp
,
cumsum
,
CumprodOp
,
cumprod
,
FillDiagonal
,
fill_diagonal
)
BinCountOp
,
bincount
,
DiffOp
,
diff
,
squeeze
,
RepeatOp
,
repeat
,
Bartlett
,
bartlett
,
FillDiagonal
,
fill_diagonal
)
from
theano
import
tensor
as
T
from
theano
import
tensor
as
T
from
theano
import
config
,
tensor
,
function
from
theano
import
config
,
tensor
,
function
...
@@ -13,6 +15,92 @@ from theano import config, tensor, function
...
@@ -13,6 +15,92 @@ from theano import config, tensor, function
numpy_ver
=
[
int
(
n
)
for
n
in
numpy
.
__version__
.
split
(
'.'
)[:
2
]]
numpy_ver
=
[
int
(
n
)
for
n
in
numpy
.
__version__
.
split
(
'.'
)[:
2
]]
numpy_16
=
bool
(
numpy_ver
>=
[
1
,
6
])
numpy_16
=
bool
(
numpy_ver
>=
[
1
,
6
])
class
TestCumsumOp
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
super
(
TestCumsumOp
,
self
)
.
setUp
()
self
.
op_class
=
CumsumOp
self
.
op
=
CumsumOp
()
def
test_cumsumOp
(
self
):
x
=
T
.
tensor3
(
'x'
)
a
=
np
.
random
.
random
((
3
,
5
,
2
))
.
astype
(
config
.
floatX
)
f
=
theano
.
function
([
x
],
cumsum
(
x
))
assert
np
.
allclose
(
np
.
cumsum
(
a
),
f
(
a
))
# Test axis=None
for
axis
in
range
(
len
(
a
.
shape
)):
f
=
theano
.
function
([
x
],
cumsum
(
x
,
axis
=
axis
))
assert
np
.
allclose
(
np
.
cumsum
(
a
,
axis
=
axis
),
f
(
a
))
def
test_infer_shape
(
self
):
x
=
T
.
tensor3
(
'x'
)
a
=
np
.
random
.
random
((
3
,
5
,
2
))
.
astype
(
config
.
floatX
)
# Test axis=None
self
.
_compile_and_check
([
x
],
[
self
.
op
(
x
)],
[
a
],
self
.
op_class
)
for
axis
in
range
(
len
(
a
.
shape
)):
self
.
_compile_and_check
([
x
],
[
cumsum
(
x
,
axis
=
axis
)],
[
a
],
self
.
op_class
)
def
test_grad
(
self
):
a
=
np
.
random
.
random
((
3
,
5
,
2
))
.
astype
(
config
.
floatX
)
utt
.
verify_grad
(
self
.
op
,
[
a
])
# Test axis=None
for
axis
in
range
(
len
(
a
.
shape
)):
utt
.
verify_grad
(
self
.
op_class
(
axis
=
axis
),
[
a
])
class
TestCumprodOp
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
super
(
TestCumprodOp
,
self
)
.
setUp
()
self
.
op_class
=
CumprodOp
self
.
op
=
CumprodOp
()
def
test_CumprodOp
(
self
):
x
=
T
.
tensor3
(
'x'
)
a
=
np
.
random
.
random
((
3
,
5
,
2
))
.
astype
(
config
.
floatX
)
f
=
theano
.
function
([
x
],
cumprod
(
x
))
assert
np
.
allclose
(
np
.
cumprod
(
a
),
f
(
a
))
# Test axis=None
for
axis
in
range
(
len
(
a
.
shape
)):
f
=
theano
.
function
([
x
],
cumprod
(
x
,
axis
=
axis
))
assert
np
.
allclose
(
np
.
cumprod
(
a
,
axis
=
axis
),
f
(
a
))
def
test_infer_shape
(
self
):
x
=
T
.
tensor3
(
'x'
)
a
=
np
.
random
.
random
((
3
,
5
,
2
))
.
astype
(
config
.
floatX
)
# Test axis=None
self
.
_compile_and_check
([
x
],
[
self
.
op
(
x
)],
[
a
],
self
.
op_class
)
for
axis
in
range
(
len
(
a
.
shape
)):
self
.
_compile_and_check
([
x
],
[
cumprod
(
x
,
axis
=
axis
)],
[
a
],
self
.
op_class
)
def
test_grad
(
self
):
a
=
np
.
random
.
random
((
3
,
5
,
2
))
.
astype
(
config
.
floatX
)
utt
.
verify_grad
(
self
.
op
,
[
a
])
# Test axis=None
for
axis
in
range
(
len
(
a
.
shape
)):
utt
.
verify_grad
(
self
.
op_class
(
axis
=
axis
),
[
a
])
class
TestBinCountOp
(
utt
.
InferShapeTester
):
class
TestBinCountOp
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
def
setUp
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论