Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5ad8bbc2
提交
5ad8bbc2
authored
7月 20, 2017
作者:
notoraptor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Wrap op params for theano.tensor.elemwise.DimShuffle.
上级
1fe27f0f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
120 行增加
和
92 行删除
+120
-92
elemwise.py
theano/tensor/elemwise.py
+120
-92
没有找到文件。
theano/tensor/elemwise.py
浏览文件 @
5ad8bbc2
...
@@ -9,7 +9,7 @@ import theano
...
@@ -9,7 +9,7 @@ import theano
from
theano
import
gof
from
theano
import
gof
from
theano.compat
import
izip
from
theano.compat
import
izip
from
theano.configparser
import
change_flags
from
theano.configparser
import
change_flags
from
theano.gof
import
Apply
,
Op
,
OpenMPOp
from
theano.gof
import
Apply
,
Op
,
OpenMPOp
,
ParamsType
from
theano
import
scalar
from
theano
import
scalar
from
theano.scalar
import
get_scalar_type
from
theano.scalar
import
get_scalar_type
from
theano.printing
import
pprint
from
theano.printing
import
pprint
...
@@ -131,11 +131,24 @@ class DimShuffle(Op):
...
@@ -131,11 +131,24 @@ class DimShuffle(Op):
check_input
=
False
check_input
=
False
__props__
=
(
"input_broadcastable"
,
"new_order"
,
"inplace"
)
__props__
=
(
"input_broadcastable"
,
"new_order"
,
"inplace"
)
@property
def
params_type
(
self
):
# We can't directly create `params_type` as class attribute
# because of importation issues related to TensorType.
return
ParamsType
(
input_broadcastable
=
TensorType
(
dtype
=
'bool'
,
broadcastable
=
(
False
,)),
_new_order
=
theano
.
tensor
.
lvector
,
inplace
=
theano
.
scalar
.
bool
)
@property
def
_new_order
(
self
):
# Param for C code.
# self.new_order may contain 'x', which is not a valid integer value.
# We replace it with -1.
return
[(
-
1
if
x
==
'x'
else
x
)
for
x
in
self
.
new_order
]
def
__init__
(
self
,
input_broadcastable
,
new_order
,
inplace
=
True
):
def
__init__
(
self
,
input_broadcastable
,
new_order
,
inplace
=
True
):
input_broadcastable
=
tuple
(
input_broadcastable
)
self
.
input_broadcastable
=
tuple
(
input_broadcastable
)
self
.
input_broadcastable
=
input_broadcastable
self
.
new_order
=
tuple
(
new_order
)
new_order
=
tuple
(
new_order
)
self
.
new_order
=
new_order
if
inplace
is
True
:
if
inplace
is
True
:
self
.
inplace
=
inplace
self
.
inplace
=
inplace
else
:
else
:
...
@@ -222,7 +235,7 @@ class DimShuffle(Op):
...
@@ -222,7 +235,7 @@ class DimShuffle(Op):
else
:
else
:
return
"DimShuffle{
%
s}"
%
","
.
join
(
str
(
x
)
for
x
in
self
.
new_order
)
return
"DimShuffle{
%
s}"
%
","
.
join
(
str
(
x
)
for
x
in
self
.
new_order
)
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
,
params
):
input
,
=
inp
input
,
=
inp
storage
,
=
out
storage
,
=
out
# drop
# drop
...
@@ -265,98 +278,113 @@ class DimShuffle(Op):
...
@@ -265,98 +278,113 @@ class DimShuffle(Op):
res
,
=
out
res
,
=
out
basename
=
input
+
'__view_or_copy'
basename
=
input
+
'__view_or_copy'
def
statements
(
lst
):
return
"""{
return
';
\n
'
.
join
(
lst
)
+
';'
npy_bool* input_broadcastable;
npy_int64* new_order;
npy_intp nd_in;
npy_intp nd_out;
PyArrayObject*
%(basename)
s;
npy_intp* dimensions;
npy_intp* strides;
if (!PyArray_IS_C_CONTIGUOUS(
%(params)
s->input_broadcastable)) {
PyErr_SetString(PyExc_RuntimeError, "DimShuffle: param input_broadcastable must be C-contiguous.");
%(just_fail)
s
}
if (!PyArray_IS_F_CONTIGUOUS(
%(params)
s->_new_order)) {
PyErr_SetString(PyExc_RuntimeError, "DimShuffle: param _new_order must be C-contiguous.");
%(just_fail)
s
}
input_broadcastable = (npy_bool*) PyArray_DATA(
%(params)
s->input_broadcastable);
new_order = (npy_int64*) PyArray_DATA(
%(params)
s->_new_order);
nd_in = PyArray_SIZE(
%(params)
s->input_broadcastable);
nd_out = PyArray_SIZE(
%(params)
s->_new_order);
/* check_input_nd */
if (PyArray_NDIM(
%(input)
s) != nd_in) {
PyErr_SetString(PyExc_NotImplementedError, "input nd");
%(just_fail)
s
}
/* clear_output */
if (
%(res)
s)
Py_XDECREF(
%(res)
s);
/* get_base */
if (
%(params)
s->inplace) {
%(basename)
s =
%(input)
s;
Py_INCREF((PyObject*)
%(basename)
s);
} else {
%(basename)
s =
(PyArrayObject*)PyArray_FromAny((PyObject*)
%(input)
s,
NULL, 0, 0, NPY_ARRAY_ALIGNED|NPY_ARRAY_ENSURECOPY, NULL);
}
/* shape_statements and strides_statements */
dimensions = (npy_intp*) malloc(nd_out * sizeof(npy_intp));
strides = (npy_intp*) malloc(nd_out * sizeof(npy_intp));
if (dimensions == NULL || strides == NULL) {
PyErr_NoMemory();
%(fail)
s
};
for (npy_intp i = 0; i < nd_out; ++i) {
if (new_order[i] != -1) {
dimensions[i] = PyArray_DIMS(
%(basename)
s)[new_order[i]];
strides[i] = PyArray_DIMS(
%(basename)
s)[new_order[i]] == 1 ?
0 : PyArray_STRIDES(
%(basename)
s)[new_order[i]];
} else {
dimensions[i] = 1;
strides[i] = 0;
}
}
/* set the strides of the broadcasted dimensions.
* This algorithm is from numpy: PyArray_Newshape() in
* cvs/numpy/numpy/core/src/multiarraymodule.c */
if (nd_out > 0) {
if (strides[nd_out - 1] == 0)
strides[nd_out - 1] = PyArray_DESCR(
%(basename)
s)->elsize;
for (npy_intp i = nd_out - 2; i > -1; --i) {
if (strides[i] == 0)
strides[i] = strides[i + 1] * dimensions[i + 1];
}
}
nd_in
=
len
(
self
.
input_broadcastable
)
/* close_bracket */
nd_out
=
len
(
self
.
new_order
)
// create a new array.
%(res)
s = (PyArrayObject*)PyArray_New(&PyArray_Type, nd_out, dimensions,
PyArray_TYPE(
%(basename)
s), strides,
PyArray_DATA(
%(basename)
s), PyArray_ITEMSIZE(
%(basename)
s),
// borrow only the writable flag from the base
// the NPY_OWNDATA flag will default to 0.
(NPY_ARRAY_WRITEABLE * PyArray_ISWRITEABLE(
%(basename)
s)),
NULL);
if (
%(res)
s == NULL) {
%(fail)
s
}
check_input_nd
=
[(
'if (PyArray_NDIM(
%(input)
s) != '
+
str
(
nd_in
)
+
')'
// recalculate flags: CONTIGUOUS, FORTRAN, ALIGNED
'{PyErr_SetString(PyExc_NotImplementedError, '
PyArray_UpdateFlags(
%(res)
s, NPY_ARRAY_UPDATE_ALL);
'"input nd");
%(fail)
s;}'
)]
clear_output
=
[
'if (
%(res)
s) {Py_XDECREF(
%(res)
s);}'
]
// we are making a view in both inplace and non-inplace cases
PyArray_SetBaseObject(
%(res)
s, (PyObject*)
%(basename)
s);
# get the copy / view of the input depending on whether we're doingi
free(strides);
# things inplace or not.
free(dimensions);
if
self
.
inplace
:
}"""
%
dict
(
input
=
input
,
res
=
res
,
get_base
=
[
'{ PyArrayObject *
%(basename)
s =
%(input)
s'
,
basename
=
basename
,
'Py_INCREF((PyObject*)
%(basename)
s)'
]
params
=
sub
[
'params'
],
else
:
just_fail
=
sub
[
'fail'
],
get_base
=
[
fail
=
"""
(
'{ PyArrayObject *
%(basename)
s = '
free(strides);
'(PyArrayObject*)PyArray_FromAny((PyObject*)
%(input)
s,'
free(dimensions);
' NULL, 0, 0, NPY_ARRAY_ALIGNED|NPY_ARRAY_ENSURECOPY,'
%(fail)
s
' NULL)'
)]
"""
%
dict
(
fail
=
sub
[
'fail'
]))
shape_statements
=
[
'npy_intp dimensions[
%
i]'
%
nd_out
]
for
i
,
o
in
enumerate
(
self
.
new_order
):
if
o
!=
'x'
:
shape_statements
+=
[(
'dimensions['
+
str
(
i
)
+
'] = PyArray_DIMS(
%(basename)
s)['
+
str
(
o
)
+
']'
)]
else
:
shape_statements
+=
[(
'dimensions['
+
str
(
i
)
+
'] = 1'
)]
strides_statements
=
[
'npy_intp strides[
%
i]'
%
nd_out
]
# set the strides of the non-broadcasted dimensions
for
i
,
o
in
enumerate
(
self
.
new_order
):
if
o
!=
'x'
:
strides_statements
+=
[(
'strides['
+
str
(
i
)
+
'] = PyArray_DIMS(
%(basename)
s)['
+
str
(
o
)
+
'] == 1? 0 : '
'PyArray_STRIDES(
%(basename)
s)['
+
str
(
o
)
+
']'
)]
else
:
strides_statements
+=
[(
'strides['
+
str
(
i
)
+
'] = 0'
)]
# set the strides of the broadcasted dimensions
# this algorithm is from numpy: PyArray_Newshape() in
# cvs/numpy/numpy/core/src/multiarraymodule.c
if
nd_out
>
0
:
strides_statements
.
append
(
'if (strides['
+
str
(
nd_out
)
+
'-1] == 0) strides['
+
str
(
nd_out
)
+
'-1] = PyArray_DESCR(
%(basename)
s)->elsize'
)
for
i
in
xrange
(
nd_out
-
2
,
-
1
,
-
1
):
strides_statements
.
append
(
"if (strides[
%(i)
s] == 0) strides[
%(i)
s] = strides[
%(i)
s+1] * "
"dimensions[
%(i)
s+1]"
%
dict
(
i
=
str
(
i
)))
close_bracket
=
[
# create a new array,
(
'
%(res)
s = (PyArrayObject*)PyArray_New(&PyArray_Type, '
''
+
str
(
nd_out
)
+
', dimensions, '
'PyArray_TYPE(
%(basename)
s), strides, '
'PyArray_DATA(
%(basename)
s), PyArray_ITEMSIZE(
%(basename)
s), '
# borrow only the writable flag from the base
# the NPY_OWNDATA flag will default to 0.
'(NPY_ARRAY_WRITEABLE*PyArray_ISWRITEABLE(
%(basename)
s)), '
'NULL)'
),
'if (
%(res)
s == NULL)
%(fail)
s;'
,
# recalculate flags: CONTIGUOUS, FORTRAN, ALIGNED
'PyArray_UpdateFlags(
%(res)
s, NPY_ARRAY_UPDATE_ALL)'
,
# we are making a view in both inplace and non-inplace cases
"""
PyArray_SetBaseObject(
%(res)
s, (PyObject*)
%(basename)
s);
"""
'}'
]
full_code
=
statements
(
check_input_nd
+
clear_output
+
get_base
+
shape_statements
+
strides_statements
+
close_bracket
)
return
full_code
%
dict
(
locals
(),
**
sub
)
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
3
,)
return
(
4
,)
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
x
,
=
inp
x
,
=
inp
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论