Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0de42ea0
提交
0de42ea0
authored
3月 14, 2013
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
More PEP8 / Pyflakes
上级
2b1796cc
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
77 行增加
和
69 行删除
+77
-69
cutils.py
theano/gof/cutils.py
+40
-37
basic.py
theano/scalar/basic.py
+3
-2
basic.py
theano/tensor/basic.py
+34
-30
没有找到文件。
theano/gof/cutils.py
浏览文件 @
0de42ea0
import
os
,
sys
import
os
import
sys
from
theano.compat
import
PY3
from
theano.compat
import
PY3
from
theano.gof.compilelock
import
get_lock
,
release_lock
from
theano.gof.compilelock
import
get_lock
,
release_lock
...
@@ -13,11 +14,13 @@ if os.path.exists(os.path.join(config.compiledir, 'cutils_ext.so')):
...
@@ -13,11 +14,13 @@ if os.path.exists(os.path.join(config.compiledir, 'cutils_ext.so')):
def
compile_cutils
():
def
compile_cutils
():
"""Do just the compilation of cutils_ext"""
"""Do just the compilation of cutils_ext"""
types
=
[
'npy_'
+
t
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'int128'
,
'int256'
,
'uint8'
,
'uint16'
,
'uint32'
,
'uint64'
,
'uint128'
,
'uint256'
,
'float16'
,
'float32'
,
'float64'
,
'float80'
,
'float96'
,
'float128'
,
'float256'
]]
complex_types
=
[
'npy_'
+
t
for
t
in
[
'complex32'
,
'complex64'
,
types
=
[
'npy_'
+
t
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'int128'
,
'int256'
,
'uint8'
,
'uint16'
,
'uint32'
,
'uint64'
,
'uint128'
,
'uint256'
,
'float16'
,
'float32'
,
'float64'
,
'float80'
,
'float96'
,
'float128'
,
'float256'
]
]
'complex128'
,
'complex160'
,
'complex192'
,
'complex512'
]]
complex_types
=
[
'npy_'
+
t
for
t
in
[
'complex32'
,
'complex64'
,
'complex128'
,
'complex160'
,
'complex192'
,
'complex512'
]
]
inplace_map_template
=
"""
inplace_map_template
=
"""
#if defined(
%(typen)
s)
#if defined(
%(typen)
s)
...
@@ -36,33 +39,36 @@ def compile_cutils():
...
@@ -36,33 +39,36 @@ def compile_cutils():
floatadd
=
"((
%(type)
s*)mit->dataptr)[0] = ((
%(type)
s*)mit->dataptr)[0] + ((
%(type)
s*)it->dataptr)[0];"
floatadd
=
"((
%(type)
s*)mit->dataptr)[0] = ((
%(type)
s*)mit->dataptr)[0] + ((
%(type)
s*)it->dataptr)[0];"
complexadd
=
"""
complexadd
=
"""
((
%(type)
s*)mit->dataptr)[0].real = ((
%(type)
s*)mit->dataptr)[0].real + ((
%(type)
s*)it->dataptr)[0].real;
((
%(type)
s*)mit->dataptr)[0].real = ((
%(type)
s*)mit->dataptr)[0].real + ((
%(type)
s*)it->dataptr)[0].real;
((
%(type)
s*)mit->dataptr)[0].imag = ((
%(type)
s*)mit->dataptr)[0].imag + ((
%(type)
s*)it->dataptr)[0].imag;
((
%(type)
s*)mit->dataptr)[0].imag = ((
%(type)
s*)mit->dataptr)[0].imag + ((
%(type)
s*)it->dataptr)[0].imag;
"""
"""
fns
=
''
.
join
([
inplace_map_template
%
{
'type'
:
t
,
'typen'
:
t
.
upper
(),
'op'
:
floatadd
%
{
'type'
:
t
}}
for
t
in
types
]
+
[
inplace_map_template
%
{
'type'
:
t
,
'typen'
:
t
.
upper
(),
'op'
:
complexadd
%
{
'type'
:
t
}}
for
t
in
complex_types
])
fns
=
''
.
join
([
inplace_map_template
%
{
'type'
:
t
,
'typen'
:
t
.
upper
(),
'op'
:
floatadd
%
{
'type'
:
t
}
}
for
t
in
types
]
+
fn_array
=
(
"inplace_map_binop addition_funcs[] = {"
+
[
inplace_map_template
%
{
'type'
:
t
,
'typen'
:
t
.
upper
(),
'op'
:
complexadd
%
{
'type'
:
t
}
}
for
t
in
complex_types
])
fn_array
=
(
"inplace_map_binop addition_funcs[] = {"
+
''
.
join
([
"""
''
.
join
([
"""
#if defined(
%(typen)
s)
#if defined(
%(typen)
s)
%(type)
s_inplace_add,
%(type)
s_inplace_add,
#endif
#endif
"""
%
{
'type'
:
t
,
'typen'
:
t
.
upper
()}
for
t
in
types
+
complex_types
])
+
"""
%
{
'type'
:
t
,
'typen'
:
t
.
upper
()}
for
t
in
types
+
complex_types
])
+
"""NULL};
"""NULL};
"""
)
"""
)
type_number_array
=
(
"int type_numbers[] = {"
+
type_number_array
=
(
"int type_numbers[] = {"
+
''
.
join
([
"""
''
.
join
([
"""
#if defined(
%(typen)
s)
#if defined(
%(typen)
s)
%(typen)
s,
%(typen)
s,
#endif
#endif
"""
%
{
'type'
:
t
,
'typen'
:
t
.
upper
()}
for
t
in
types
+
complex_types
])
+
"""
%
{
'type'
:
t
,
'typen'
:
t
.
upper
()}
for
t
in
types
+
complex_types
])
+
"-1000};"
)
"-1000};"
)
code
=
(
"""
code
=
(
"""
#include <Python.h>
#include <Python.h>
#include "numpy/arrayobject.h"
#include "numpy/arrayobject.h"
...
@@ -90,7 +96,7 @@ def compile_cutils():
...
@@ -90,7 +96,7 @@ def compile_cutils():
#if NPY_API_VERSION >= 0x00000008
#if NPY_API_VERSION >= 0x00000008
typedef void (*inplace_map_binop)(PyArrayMapIterObject *, PyArrayIterObject *);
typedef void (*inplace_map_binop)(PyArrayMapIterObject *, PyArrayIterObject *);
"""
+
fns
+
fn_array
+
type_number_array
+
"""
+
fns
+
fn_array
+
type_number_array
+
"""
"""
static int
static int
...
@@ -110,7 +116,7 @@ map_increment(PyArrayMapIterObject *mit, PyObject *op, inplace_map_binop add_inp
...
@@ -110,7 +116,7 @@ map_increment(PyArrayMapIterObject *mit, PyObject *op, inplace_map_binop add_inp
return -1;
return -1;
}
}
if ((mit->subspace != NULL) && (mit->consec)) {
if ((mit->subspace != NULL) && (mit->consec)) {
if (mit->iteraxes[0] > 0) {
if (mit->iteraxes[0] > 0) {
PyArray_MapIterSwapAxes(mit, (PyArrayObject **)&arr, 0);
PyArray_MapIterSwapAxes(mit, (PyArrayObject **)&arr, 0);
if (arr == NULL) {
if (arr == NULL) {
return -1;
return -1;
...
@@ -120,8 +126,7 @@ map_increment(PyArrayMapIterObject *mit, PyObject *op, inplace_map_binop add_inp
...
@@ -120,8 +126,7 @@ map_increment(PyArrayMapIterObject *mit, PyObject *op, inplace_map_binop add_inp
it = (PyArrayIterObject*)
it = (PyArrayIterObject*)
PyArray_BroadcastToShape((PyObject*)arr, mit->dimensions, mit->nd);
PyArray_BroadcastToShape((PyObject*)arr, mit->dimensions, mit->nd);
if (it == NULL) {
if (it == NULL) {
Py_DECREF(arr);
Py_DECREF(arr);
return -1;
return -1;
}
}
...
@@ -138,13 +143,13 @@ inplace_increment(PyObject *dummy, PyObject *args)
...
@@ -138,13 +143,13 @@ inplace_increment(PyObject *dummy, PyObject *args)
{
{
PyObject *arg_a = NULL, *index=NULL, *inc=NULL;
PyObject *arg_a = NULL, *index=NULL, *inc=NULL;
PyArrayObject *a;
PyArrayObject *a;
inplace_map_binop add_inplace = NULL;
inplace_map_binop add_inplace = NULL;
int type_number = -1;
int type_number = -1;
int i =0;
int i =0;
PyArrayMapIterObject * mit;
PyArrayMapIterObject * mit;
if (!PyArg_ParseTuple(args, "OOO", &arg_a, &index,
if (!PyArg_ParseTuple(args, "OOO", &arg_a, &index,
&inc)) {
&inc)) {
return NULL;
return NULL;
}
}
if (!PyArray_Check(arg_a)) {
if (!PyArray_Check(arg_a)) {
...
@@ -153,29 +158,29 @@ inplace_increment(PyObject *dummy, PyObject *args)
...
@@ -153,29 +158,29 @@ inplace_increment(PyObject *dummy, PyObject *args)
}
}
a = (PyArrayObject *) arg_a;
a = (PyArrayObject *) arg_a;
if (PyArray_FailUnlessWriteable(a, "input/output array") < 0) {
if (PyArray_FailUnlessWriteable(a, "input/output array") < 0) {
return NULL;
return NULL;
}
}
if (PyArray_NDIM(a) == 0) {
if (PyArray_NDIM(a) == 0) {
PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed.");
PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed.");
return NULL;
return NULL;
}
}
type_number = PyArray_TYPE(a);
type_number = PyArray_TYPE(a);
while (type_numbers[i] >= 0 && addition_funcs[i] != NULL){
while (type_numbers[i] >= 0 && addition_funcs[i] != NULL){
if (type_number == type_numbers[i]) {
if (type_number == type_numbers[i]) {
add_inplace = addition_funcs[i];
add_inplace = addition_funcs[i];
break;
break;
}
}
i++ ;
i++ ;
}
}
if (add_inplace == NULL) {
if (add_inplace == NULL) {
PyErr_SetString(PyExc_TypeError, "unsupported type for a");
PyErr_SetString(PyExc_TypeError, "unsupported type for a");
return NULL;
return NULL;
}
}
mit = (PyArrayMapIterObject *) PyArray_MapIterArray(a, index);
mit = (PyArrayMapIterObject *) PyArray_MapIterArray(a, index);
...
@@ -185,9 +190,9 @@ inplace_increment(PyObject *dummy, PyObject *args)
...
@@ -185,9 +190,9 @@ inplace_increment(PyObject *dummy, PyObject *args)
if (map_increment(mit, inc, add_inplace) != 0) {
if (map_increment(mit, inc, add_inplace) != 0) {
goto fail;
goto fail;
}
}
Py_DECREF(mit);
Py_DECREF(mit);
Py_INCREF(Py_None);
Py_INCREF(Py_None);
return Py_None;
return Py_None;
...
@@ -203,17 +208,16 @@ fail:
...
@@ -203,17 +208,16 @@ fail:
{"run_cthunk", run_cthunk, METH_VARARGS|METH_KEYWORDS,
{"run_cthunk", run_cthunk, METH_VARARGS|METH_KEYWORDS,
"Run a theano cthunk."},
"Run a theano cthunk."},
#if NPY_API_VERSION >= 0x00000008
#if NPY_API_VERSION >= 0x00000008
{"inplace_increment", inplace_increment,
{"inplace_increment", inplace_increment,
METH_VARARGS,
METH_VARARGS,
"increments a numpy array inplace at the passed indexes."},
"increments a numpy array inplace at the passed indexes."},
#endif
#endif
{NULL, NULL, 0, NULL} /* Sentinel */
{NULL, NULL, 0, NULL} /* Sentinel */
};"""
)
};"""
)
if
PY3
:
if
PY3
:
# This is not the most efficient code, but it is written this way to
highlight
# This is not the most efficient code, but it is written this way to
# the changes needed to make 2.x code compile under python 3.
#
highlight
the changes needed to make 2.x code compile under python 3.
code
=
code
.
replace
(
"<Python.h>"
,
'"numpy/npy_3kcompat.h"'
,
1
)
code
=
code
.
replace
(
"<Python.h>"
,
'"numpy/npy_3kcompat.h"'
,
1
)
code
=
code
.
replace
(
"PyCObject"
,
"NpyCapsule"
)
code
=
code
.
replace
(
"PyCObject"
,
"NpyCapsule"
)
code
+=
"""
code
+=
"""
...
@@ -242,8 +246,7 @@ fail:
...
@@ -242,8 +246,7 @@ fail:
} //extern C
} //extern C
"""
"""
import
cmodule
import
cmodule
loc
=
os
.
path
.
join
(
config
.
compiledir
,
'cutils_ext'
)
loc
=
os
.
path
.
join
(
config
.
compiledir
,
'cutils_ext'
)
if
not
os
.
path
.
exists
(
loc
):
if
not
os
.
path
.
exists
(
loc
):
os
.
mkdir
(
loc
)
os
.
mkdir
(
loc
)
...
...
theano/scalar/basic.py
浏览文件 @
0de42ea0
...
@@ -23,7 +23,8 @@ import numpy
...
@@ -23,7 +23,8 @@ import numpy
import
theano
import
theano
from
theano.compat
import
PY3
from
theano.compat
import
PY3
from
theano
import
gof
from
theano
import
gof
from
theano.gof
import
Op
,
utils
,
Variable
,
Constant
,
Type
,
Apply
,
FunctionGraph
from
theano.gof
import
(
Op
,
utils
,
Variable
,
Constant
,
Type
,
Apply
,
FunctionGraph
)
from
theano.gof.python25
import
partial
,
all
,
any
from
theano.gof.python25
import
partial
,
all
,
any
from
theano.configparser
import
config
from
theano.configparser
import
config
...
@@ -2680,7 +2681,7 @@ class Composite(ScalarOp):
...
@@ -2680,7 +2681,7 @@ class Composite(ScalarOp):
except
AttributeError
:
except
AttributeError
:
if
0
:
if
0
:
l
=
[]
l
=
[]
for
n
in
fgraph
.
toposort
():
for
n
in
self
.
fgraph
.
toposort
():
if
hasattr
(
n
.
op
,
"name"
)
and
n
.
op
.
name
is
not
None
:
if
hasattr
(
n
.
op
,
"name"
)
and
n
.
op
.
name
is
not
None
:
v
=
n
.
op
.
name
v
=
n
.
op
.
name
if
v
.
startswith
(
"Composite"
):
if
v
.
startswith
(
"Composite"
):
...
...
theano/tensor/basic.py
浏览文件 @
0de42ea0
...
@@ -24,9 +24,9 @@ from theano import compile, printing
...
@@ -24,9 +24,9 @@ from theano import compile, printing
from
theano.printing
import
pprint
,
min_informative_str
from
theano.printing
import
pprint
,
min_informative_str
from
theano.tensor.utils
import
hash_from_ndarray
from
theano.tensor.utils
import
hash_from_ndarray
import
theano.gof.cutils
#
needed to import cutils_ext
import
theano.gof.cutils
#
needed to import cutils_ext
try
:
try
:
from
cutils_ext.cutils_ext
import
inplace_increment
from
cutils_ext.cutils_ext
import
inplace_increment
except
ImportError
:
except
ImportError
:
inplace_increment
=
None
inplace_increment
=
None
...
@@ -4439,7 +4439,8 @@ class Subtensor(Op):
...
@@ -4439,7 +4439,8 @@ class Subtensor(Op):
slice_c
=
None
slice_c
=
None
return
slice
(
slice_a
,
slice_b
,
slice_c
)
return
slice
(
slice_a
,
slice_b
,
slice_c
)
# There is a bug in numpy that results in isinstance(x, int) returning False for numpy integers.
# There is a bug in numpy that results in isinstance(x, int) returning
# False for numpy integers.
# See <http://projects.scipy.org/numpy/ticket/2235>.
# See <http://projects.scipy.org/numpy/ticket/2235>.
elif
isinstance
(
entry
,
(
numpy
.
integer
,
int
)):
elif
isinstance
(
entry
,
(
numpy
.
integer
,
int
)):
return
entry
return
entry
...
@@ -7198,19 +7199,21 @@ def as_index_variable(idx):
...
@@ -7198,19 +7199,21 @@ def as_index_variable(idx):
raise
TypeError
(
'index must be integers'
)
raise
TypeError
(
'index must be integers'
)
return
idx
return
idx
def
as_int_none_variable
(
x
):
def
as_int_none_variable
(
x
):
if
x
is
None
:
if
x
is
None
:
return
NoneConst
return
NoneConst
x
=
as_tensor_variable
(
x
,
ndim
=
0
)
x
=
as_tensor_variable
(
x
,
ndim
=
0
)
if
x
.
type
.
dtype
[:
3
]
not
in
(
'int'
,
'uin'
):
if
x
.
type
.
dtype
[:
3
]
not
in
(
'int'
,
'uin'
):
raise
TypeError
(
'index must be integers'
)
raise
TypeError
(
'index must be integers'
)
return
x
return
x
class
MakeSlice
(
Op
):
class
MakeSlice
(
Op
):
def
make_node
(
self
,
slc
):
def
make_node
(
self
,
slc
):
return
Apply
(
self
,
return
Apply
(
self
,
map
(
as_int_none_variable
,[
slc
.
start
,
slc
.
stop
,
slc
.
step
]),
map
(
as_int_none_variable
,
[
slc
.
start
,
slc
.
stop
,
slc
.
step
]),
[
slicetype
()])
[
slicetype
()])
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
...
@@ -7218,7 +7221,7 @@ class MakeSlice(Op):
...
@@ -7218,7 +7221,7 @@ class MakeSlice(Op):
out
[
0
]
=
slice
(
*
inp
)
out
[
0
]
=
slice
(
*
inp
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
return
type
(
self
)
==
type
(
other
)
...
@@ -7226,11 +7229,11 @@ class MakeSlice(Op):
...
@@ -7226,11 +7229,11 @@ class MakeSlice(Op):
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
grad
(
self
,
inputs
,
grads
):
def
grad
(
self
,
inputs
,
grads
):
return
[
Di
connectedType
()()
for
i
in
inputs
]
return
[
Di
sconnectedType
()()
for
i
in
inputs
]
make_slice
=
MakeSlice
()
make_slice
=
MakeSlice
()
class
SliceType
(
gof
.
Type
):
class
SliceType
(
gof
.
Type
):
...
@@ -7244,7 +7247,6 @@ class SliceType(gof.Type):
...
@@ -7244,7 +7247,6 @@ class SliceType(gof.Type):
return
"slice"
return
"slice"
class
NoneTypeT
(
gof
.
Type
):
class
NoneTypeT
(
gof
.
Type
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
...
@@ -7257,13 +7259,16 @@ class NoneTypeT(gof.Type):
...
@@ -7257,13 +7259,16 @@ class NoneTypeT(gof.Type):
return
"None"
return
"None"
slicetype
=
SliceType
()
slicetype
=
SliceType
()
NoneConst
=
Constant
(
NoneTypeT
(),
None
,
name
=
'None'
)
NoneConst
=
Constant
(
NoneTypeT
(),
None
,
name
=
'None'
)
def
adv_index_broadcastable_pattern
(
a
,
idx
):
def
adv_index_broadcastable_pattern
(
a
,
idx
):
"""
"""
This function is only used to determine the broardcast pattern for AdvancedSubtensor output variable.
This function is only used to determine the broadcast pattern for
AdvancedSubtensor output variable.
For this, we make a fake ndarray and a fake idx and call use ask numpy the output. From this, we find the output broadcast pattern.
For this, we make a fake ndarray and a fake idx and call use ask numpy
the output. From this, we find the output broadcast pattern.
"""
"""
def
replace_slice
(
v
):
def
replace_slice
(
v
):
...
@@ -7274,21 +7279,22 @@ def adv_index_broadcastable_pattern(a, idx):
...
@@ -7274,21 +7279,22 @@ def adv_index_broadcastable_pattern(a, idx):
" to be fetched."
,
v
)
" to be fetched."
,
v
)
else
:
else
:
v
=
v
.
outputs
[
0
]
v
=
v
.
outputs
[
0
]
if
NoneConst
.
equals
(
v
):
if
NoneConst
.
equals
(
v
):
return
None
return
None
if
isinstance
(
v
.
type
,
SliceType
):
if
isinstance
(
v
.
type
,
SliceType
):
return
slice
(
None
,
None
)
return
slice
(
None
,
None
)
return
numpy
.
zeros
(
(
2
,)
*
v
.
ndim
,
int
)
return
numpy
.
zeros
(
(
2
,)
*
v
.
ndim
,
int
)
newidx
=
tuple
(
map
(
replace_slice
,
idx
))
newidx
=
tuple
(
map
(
replace_slice
,
idx
))
#2 - True = 1; 2 - False = 2
#2 - True = 1; 2 - False = 2
fakeshape
=
[
2
-
bc
for
bc
in
a
.
broadcastable
]
fakeshape
=
[
2
-
bc
for
bc
in
a
.
broadcastable
]
retshape
=
numpy
.
empty
(
fakeshape
)[
newidx
]
.
shape
retshape
=
numpy
.
empty
(
fakeshape
)[
newidx
]
.
shape
return
tuple
([
dim
==
1
for
dim
in
retshape
])
return
tuple
([
dim
==
1
for
dim
in
retshape
])
class
AdvancedSubtensor
(
Op
):
class
AdvancedSubtensor
(
Op
):
"""Return a subtensor copy, using advanced indexing.
"""Return a subtensor copy, using advanced indexing.
"""
"""
...
@@ -7309,13 +7315,11 @@ class AdvancedSubtensor(Op):
...
@@ -7309,13 +7315,11 @@ class AdvancedSubtensor(Op):
x
=
as_tensor_variable
(
x
)
x
=
as_tensor_variable
(
x
)
index
=
tuple
(
map
(
as_index_variable
,
index
))
index
=
tuple
(
map
(
as_index_variable
,
index
))
bcast
=
adv_index_broadcastable_pattern
(
x
,
index
)
return
gof
.
Apply
(
self
,
return
gof
.
Apply
(
self
,
(
x
,)
+
index
,
(
x
,)
+
index
,
[
tensor
(
dtype
=
x
.
type
.
dtype
,
[
tensor
(
dtype
=
x
.
type
.
dtype
,
broadcastable
=
adv_index_broadcastable_pattern
(
x
,
index
)
)])
broadcastable
=
bcast
)])
def
R_op
(
self
,
inputs
,
eval_points
):
def
R_op
(
self
,
inputs
,
eval_points
):
if
eval_points
[
0
]
is
None
:
if
eval_points
[
0
]
is
None
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论