Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b876fbf7
提交
b876fbf7
authored
8月 28, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
d141c790
df50d612
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
314 行增加
和
34 行删除
+314
-34
profilemode.py
theano/compile/profilemode.py
+5
-3
test_function.py
theano/compile/tests/test_function.py
+1
-1
cmodule.py
theano/gof/cmodule.py
+7
-4
link.py
theano/gof/link.py
+0
-2
test_cc.py
theano/gof/tests/test_cc.py
+30
-1
downsample.py
theano/sandbox/downsample.py
+258
-0
basic.py
theano/tensor/basic.py
+13
-23
没有找到文件。
theano/compile/profilemode.py
浏览文件 @
b876fbf7
...
...
@@ -17,16 +17,18 @@ class ProfileMode(Mode):
def
blah
(
i
,
node
,
th
):
if
hasattr
(
th
,
'cthunk'
):
t0
=
time
.
time
()
run_cthunk
(
th
.
cthunk
)
failure
=
run_cthunk
(
th
.
cthunk
)
dt
=
time
.
time
()
-
t0
if
failure
:
raise
RuntimeError
(
'A C Op raised an exception. PerformLinker cannot tell you what it was though. Use a standard mode such as FAST_RUN to correct the problem.'
)
else
:
t0
=
time
.
time
()
th
()
dt
=
time
.
time
()
-
t0
local_time
[
0
]
+=
dt
apply_time
[(
i
,
node
.
op
)]
=
apply_time
.
get
((
i
,
node
.
op
),
0.0
)
+
dt
apply_call
[(
i
,
node
.
op
)]
=
apply_call
.
get
((
i
,
node
.
op
),
0
)
+
1
apply_time
[(
i
,
node
.
op
,
tuple
(
node
.
inputs
))]
=
apply_time
.
get
((
i
,
node
.
op
,
tuple
(
node
.
inputs
)
),
0.0
)
+
dt
apply_call
[(
i
,
node
.
op
,
tuple
(
node
.
inputs
))]
=
apply_call
.
get
((
i
,
node
.
op
,
tuple
(
node
.
inputs
)
),
0
)
+
1
op_time
[
node
.
op
]
=
op_time
.
get
(
node
.
op
,
0.0
)
+
dt
op_cimpl
[
node
.
op
]
=
hasattr
(
th
,
'cthunk'
)
op_call
[
node
.
op
]
=
op_call
.
get
(
node
.
op
,
0
)
+
1
...
...
theano/compile/tests/test_function.py
浏览文件 @
b876fbf7
...
...
@@ -279,7 +279,7 @@ class T_function(unittest.TestCase):
a
=
T
.
dmatrix
()
f
=
function
([
a
],
Out
(
a
,
borrow
=
False
))
o
=
N
.
ones
((
3
,
3
))
assert
o
is
not
f
(
o
)
assert
o
is
f
(
o
)
#borrow does not imply copy.
class
T_picklefunction
(
unittest
.
TestCase
):
...
...
theano/gof/cmodule.py
浏览文件 @
b876fbf7
...
...
@@ -386,10 +386,13 @@ class ModuleCache(object):
key_broken
=
True
if
not
key_broken
:
key_from_file
=
cPickle
.
load
(
file
(
key_pkl
))
if
key
!=
key_from_file
:
raise
Exception
(
"key not equal to unpickled version (Hint: verify the __eq__ and __hash__ functions for your Ops"
,
(
key
,
key_from_file
))
self
.
loaded_key_pkl
.
add
(
key_pkl
)
try
:
key_from_file
=
cPickle
.
load
(
file
(
key_pkl
))
if
key
!=
key_from_file
:
raise
Exception
(
"key not equal to unpickled version (Hint: verify the __eq__ and __hash__ functions for your Ops"
,
(
key
,
key_from_file
))
self
.
loaded_key_pkl
.
add
(
key_pkl
)
# adding the key file to this set means it is a versioned key
except
cPickle
.
UnpicklingError
:
warning
(
'Cache failure due to un-loadable key'
,
key
)
self
.
entry_from_key
[
key
]
=
name
self
.
module_from_name
[
name
]
=
module
...
...
theano/gof/link.py
浏览文件 @
b876fbf7
...
...
@@ -5,8 +5,6 @@ from type import Type
import
sys
,
traceback
from
copy
import
copy
from
cutils
import
run_cthunk
__excepthook
=
sys
.
excepthook
def
thunk_hook
(
type
,
value
,
trace
):
...
...
theano/gof/tests/test_cc.py
浏览文件 @
b876fbf7
...
...
@@ -127,7 +127,6 @@ class Div(Binary):
return
x
/
y
div
=
Div
()
def
inputs
():
x
=
double
(
'x'
)
y
=
double
(
'y'
)
...
...
@@ -250,3 +249,33 @@ def test_duallinker_mismatch():
raise
Exception
(
"An exception should have been raised here!"
)
except
MyExc
,
e
:
pass
################################
# Test that failure code works #
################################
class
AddFail
(
Binary
):
def
c_code
(
self
,
node
,
name
,
(
x
,
y
),
(
z
,
),
sub
):
fail
=
sub
[
'fail'
]
return
"""
%(z)
s =
%(x)
s +
%(y)
s;
PyErr_SetString(PyExc_RuntimeError, "failing here");
%(fail)
s;"""
%
locals
()
def
impl
(
self
,
x
,
y
):
return
x
+
y
add_fail
=
AddFail
()
def
test_fail_error
():
x
,
y
,
z
=
inputs
()
x
=
Constant
(
tdouble
,
7.2
,
name
=
'x'
)
e
=
add_fail
(
mul
(
x
,
y
),
mul
(
y
,
z
))
lnk
=
OpWiseCLinker
()
.
accept
(
Env
([
y
,
z
],
[
e
]))
fn
=
lnk
.
make_function
()
try
:
res
=
fn
(
1.5
,
3.0
)
except
RuntimeError
:
print
'Yay, TEST PASSED'
return
#test passed
assert
0
#test failed
theano/sandbox/downsample.py
0 → 100644
浏览文件 @
b876fbf7
""" Ops for downsampling images.
Planned:
DownsampleFactorMax, DownsampleAvg, DownsampleSoftmax.
"""
#This file should move along with conv.py
from
theano
import
sparse
,
gof
,
Op
,
tensor
,
Variable
,
Apply
from
theano.printing
import
Print
class
DownsampleFactorMaxGrad
(
Op
):
def
__init__
(
self
,
ds
,
ignore_border
):
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
def
make_node
(
self
,
x
,
maxout
,
gz
):
# make_node should only be called by the grad function of DownsampleFactorMax,
# so these asserts should not fail.
assert
isinstance
(
x
,
Variable
)
and
x
.
ndim
==
4
assert
isinstance
(
maxout
,
Variable
)
and
maxout
.
ndim
==
4
assert
isinstance
(
gz
,
Variable
)
and
gz
.
ndim
==
4
return
Apply
(
self
,
[
x
,
maxout
,
gz
],
[
x
.
type
()])
def
perform
(
self
,
node
,
(
x
,
maxout
,
gz
),
(
gx_stg
,)):
gx
=
N
.
zeros_like
(
x
)
ds0
,
ds1
=
self
.
ds
shape2
=
(
x
.
shape
[
2
]
/
ds0
*
ds0
)
if
self
.
ignore_border
else
x
.
shape
[
2
]
shape3
=
(
x
.
shape
[
3
]
/
ds1
*
ds1
)
if
self
.
ignore_border
else
x
.
shape
[
3
]
for
n
in
xrange
(
x
.
shape
[
0
]):
for
k
in
xrange
(
x
.
shape
[
1
]):
for
i
in
xrange
(
shape2
):
zi
=
i
/
ds0
for
j
in
xrange
(
shape3
):
zj
=
j
/
ds1
gx
[
n
,
k
,
i
,
j
]
=
gz
[
n
,
k
,
zi
,
zj
]
if
(
maxout
[
n
,
k
,
zi
,
zj
]
==
x
[
n
,
k
,
i
,
j
])
else
0
gx_stg
[
0
]
=
gx
def
c_code
(
self
,
node
,
name
,
(
x
,
z
,
gz
),
(
gx
,),
sub
):
fail
=
sub
[
'fail'
]
self_ignore_border
=
int
(
self
.
ignore_border
)
ds0
,
ds1
=
self
.
ds
return
"""
int x_typenum = PyArray_ObjectType((PyObject*)
%(x)
s, 0);
int z_typenum = PyArray_ObjectType((PyObject*)
%(z)
s, 0);
int gz_typenum = PyArray_ObjectType((PyObject*)
%(gz)
s, 0);
int x_shp0_usable;
int x_shp1_usable;
int z_shp0, z_shp1;
if ((x_typenum != z_typenum) || (x_typenum != gz_typenum))
{
PyErr_SetString(PyExc_ValueError, "input types must all match");
%(fail)
s;
}
if(
%(x)
s->nd!=4)
{
PyErr_SetString(PyExc_ValueError, "x must be a 4d ndarray");
%(fail)
s;
}
if(
%(z)
s->nd!=4)
{
PyErr_SetString(PyExc_ValueError, "z must be a 4d ndarray");
%(fail)
s;
}
if(
%(gz)
s->nd!=4)
{
PyErr_SetString(PyExc_ValueError, "gz must be a 4d ndarray");
%(fail)
s;
}
z_shp0 =
%(z)
s->dimensions[2];
z_shp1 =
%(z)
s->dimensions[3];
if (
%(self_ignore_border)
s)
{
x_shp0_usable = z_shp0 *
%(ds0)
s;
x_shp1_usable = z_shp1 *
%(ds1)
s;
}
else
{
x_shp0_usable =
%(x)
s->dimensions[2];
x_shp1_usable =
%(x)
s->dimensions[3];
}
if ((!
%(gx)
s)
|| *PyArray_DIMS(
%(gx)
s)!=4
||(
%(gx)
s->dimensions[0] !=
%(x)
s->dimensions[0])
||(
%(gx)
s->dimensions[1] !=
%(x)
s->dimensions[1])
||(
%(gx)
s->dimensions[2] !=
%(x)
s->dimensions[2])
||(
%(gx)
s->dimensions[3] !=
%(x)
s->dimensions[3])
)
{
Py_XDECREF(
%(gx)
s);
%(gx)
s = (PyArrayObject*) PyArray_ZEROS(4,
%(x)
s->dimensions, x_typenum,0);
}
for(int b=0;b<
%(x)
s->dimensions[0];b++){
for(int k=0;k<
%(x)
s->dimensions[1];k++){
int mini_i = 0;
int zi = 0;
for(int i=0;i< x_shp0_usable; i++){
int mini_j = 0;
int zj = 0;
for(int j=0; j< x_shp1_usable; j++){
dtype_
%(x)
s * __restrict__ xp = ((dtype_
%(x)
s*)(PyArray_GETPTR4(
%(x)
s,b,k,i,j)));
dtype_
%(gx)
s * __restrict__ gxp = ((dtype_
%(gx)
s*)(PyArray_GETPTR4(
%(gx)
s,b,k,i,j)));
dtype_
%(z)
s * __restrict__ zp = ((dtype_
%(z)
s*)(PyArray_GETPTR4(
%(z)
s,b,k,zi,zj)));
dtype_
%(gz)
s * __restrict__ gzp = ((dtype_
%(gz)
s*)(PyArray_GETPTR4(
%(gz)
s,b,k,zi,zj)));
gxp[0] = (zp[0] == xp[0]) ? gzp[0] : 0;
mini_j = (mini_j + 1 ==
%(ds1)
s) ? 0 : mini_j+1;
zj += (mini_j == 0);
}//for j
mini_i = (mini_i + 1 ==
%(ds0)
s) ? 0 : mini_i+1;
zi += (mini_i == 0);
for (int j = x_shp1_usable; j <
%(x)-
>dimensions[3]; ++j) {
dtype_
%(gx)
s * gxp = ((dtype_
%(gx)
s*)(PyArray_GETPTR4(
%(gx)
s,b,k,i,j)));
gxp[0] = 0;
}
}//for i
for(int i = x_shp0_usable; i <
%(x)
s->dimensions[2]; i++){
for (int j = 0; j <
%(x)-
>dimensions[3]; ++j) {
dtype_
%(gx)
s * gxp = ((dtype_
%(gx)
s*)(PyArray_GETPTR4(
%(gx)
s,b,k,i,j)));
gxp[0] = 0;
}
}
}//for k
}//for b
"""
%
locals
()
class
DownsampleFactorMax
(
Op
):
"""
For N-dimensional tensors, consider that the last two dimensions span images.
This Op downsamples these images by taking the max over non-overlapping rectangular regions.
"""
def
out_shape
(
imgshape
,
ignore_border
=
False
):
#old code not tested (not evenread)
rval
=
[
imgshape
[
0
],
imgshape
[
1
],
imgshape
[
2
]
/
self
.
ds
[
0
],
imgshape
[
3
]
/
self
.
ds
[
1
]]
if
imgshape
[
2
]
%
self
.
ds
[
0
]:
rval
[
2
]
+=
1
if
imgshape
[
3
]
%
self
.
ds
[
1
]:
rval
[
3
]
+=
1
return
tuple
(
rval
)
def
__init__
(
self
,
ds
,
ignore_border
=
False
):
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
def
make_node
(
self
,
x
):
dmatrix4
=
tensor
.
TensorType
(
x
.
type
.
dtype
,
(
False
,
False
,
False
,
False
))
if
x
.
type
!=
dmatrix4
:
raise
NotImplementedError
()
return
gof
.
Apply
(
self
,
[
x
],
[
dmatrix4
()])
def
perform
(
self
,
node
,
(
x
,),
(
z
,)):
"""
"""
if
len
(
x
.
shape
)
!=
4
:
raise
NotImplementedError
(
'DownsampleFactorMax requires 4D input for now'
)
if
z
[
0
]
is
None
:
z
[
0
]
=
N
.
zeros
(
self
.
out_shape
(
x
.
shape
,
self
.
ignore_border
))
-
float
(
'inf'
)
zz
=
z
[
0
]
ds0
,
ds1
=
self
.
ds
x_usable2
=
(
x
.
shape
[
2
]
/
ds0
*
ds0
)
if
self
.
ignore_border
else
x
.
shape
[
2
]
x_usable3
=
(
x
.
shape
[
3
]
/
ds1
*
ds1
)
if
self
.
ignore_border
else
x
.
shape
[
3
]
for
n
in
xrange
(
x
.
shape
[
0
]):
for
k
in
xrange
(
x
.
shape
[
1
]):
for
i
in
xrange
(
x_usable2
):
zi
=
i
/
ds0
for
j
in
xrange
(
x_usable3
):
zj
=
j
/
ds1
zz
[
n
,
k
,
zi
,
zj
]
=
__builtin__
.
max
(
zz
[
n
,
k
,
zi
,
zj
],
x
[
n
,
k
,
i
,
j
])
def
grad
(
self
,(
x
,),
(
gz
,)):
maxout
=
self
(
x
)
return
[
DownsampleFactorMaxGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
)(
x
,
maxout
,
gz
)]
def
c_code
(
self
,
node
,
name
,
(
x
,),
(
z
,
),
sub
):
fail
=
sub
[
'fail'
]
self_ignore_border
=
int
(
self
.
ignore_border
)
ds0
,
ds1
=
self
.
ds
return
"""
int typenum = PyArray_ObjectType((PyObject*)
%(x)
s, 0);
int x_shp0_usable;
int x_shp1_usable;
int z_shp0, z_shp1;
if(
%(x)
s->nd!=4)
{
PyErr_SetString(PyExc_ValueError, "x must be a 4d ndarray");
%(fail)
s;
}
z_shp0 =
%(x)
s->dimensions[2] /
%(ds0)
s;
z_shp1 =
%(x)
s->dimensions[3] /
%(ds1)
s;
if (
%(self_ignore_border)
s)
{
x_shp0_usable = z_shp0 *
%(ds0)
s;
x_shp1_usable = z_shp1 *
%(ds1)
s;
}
else
{
z_shp0 += (
%(x)
s->dimensions[2]
%% %(ds0)
s) ? 1 : 0;
z_shp1 += (
%(x)
s->dimensions[3]
%% %(ds1)
s) ? 1 : 0;
x_shp0_usable =
%(x)
s->dimensions[2];
x_shp1_usable =
%(x)
s->dimensions[3];
}
if ((!
%(z)
s)
|| *PyArray_DIMS(
%(z)
s)!=4
||(
%(z)
s->dimensions[0] !=
%(x)
s->dimensions[0])
||(
%(z)
s->dimensions[1] !=
%(x)
s->dimensions[1])
||(
%(z)
s->dimensions[2] != z_shp0)
||(
%(z)
s->dimensions[3] != z_shp1)
)
{
if (
%(z)
s) Py_XDECREF(
%(z)
s);
npy_intp dims[4] = {0,0,0,0};
dims[0]=
%(x)
s->dimensions[0];
dims[1]=
%(x)
s->dimensions[1];
dims[2]=z_shp0;
dims[3]=z_shp1;
%(z)
s = (PyArrayObject*) PyArray_ZEROS(4, dims, typenum,0); //TODO: zeros not necessary
}
for(int b=0;b<
%(x)
s->dimensions[0];b++){
for(int k=0;k<
%(x)
s->dimensions[1];k++){
int mini_i = 0;
int zi = 0;
for(int i=0;i< x_shp0_usable; i++){
int mini_j = 0;
int zj = 0;
for(int j=0; j<x_shp1_usable; j++){
dtype_
%(x)
s a = ((dtype_
%(x)
s*)(PyArray_GETPTR4(
%(x)
s,b,k,i,j)))[0];
dtype_
%(z)
s * __restrict__ z = ((dtype_
%(z)
s*)(PyArray_GETPTR4(
%(z)
s,b,k,zi,zj)));
z[0] = (((mini_j|mini_i) == 0) || z[0] < a) ? a : z[0];
mini_j = ((mini_j + 1) ==
%(ds1)
s) ? 0 : mini_j+1;
zj += (mini_j == 0);
}
mini_i = ((mini_i + 1) ==
%(ds0)
s) ? 0 : mini_i+1;
zi += (mini_i == 0);
}
}
}
"""
%
locals
()
theano/tensor/basic.py
浏览文件 @
b876fbf7
...
...
@@ -1592,19 +1592,20 @@ class Subtensor(Op):
# for entry in self.idx_list)
return
hash
(
idx_list
)
@staticmethod
def
str_from_slice
(
entry
):
msg
=
[]
for
x
in
[
entry
.
start
,
entry
.
stop
,
entry
.
step
]:
if
x
is
None
:
msg
.
append
(
""
)
else
:
msg
.
append
(
str
(
x
))
return
":"
.
join
(
msg
)
def
__str__
(
self
):
indices
=
[]
for
entry
in
self
.
idx_list
:
if
isinstance
(
entry
,
slice
):
msg
=
[]
for
x
in
[
entry
.
start
,
entry
.
stop
,
entry
.
step
]:
if
x
is
None
:
msg
+=
""
else
:
msg
+=
[
str
(
x
)]
indices
.
append
(
":"
.
join
(
msg
))
#backport
#indices.append(":".join("" if x is None else str(x) for x in [entry.start, entry.stop, entry.step]))
indices
.
append
(
self
.
str_from_slice
(
entry
))
else
:
indices
.
append
(
str
(
entry
))
return
"
%
s{
%
s}"
%
(
self
.
__class__
.
__name__
,
", "
.
join
(
indices
))
...
...
@@ -1696,24 +1697,13 @@ class SetSubtensor(Op):
indices
=
[]
for
entry
in
self
.
idx_list
:
if
isinstance
(
entry
,
slice
):
msg
=
[]
for
x
in
[
entry
.
start
,
entry
.
stop
,
entry
.
step
]:
if
x
is
None
:
msg
+=
""
else
:
msg
+=
[
str
(
x
)]
indices
.
append
(
":"
.
join
(
msg
))
#backport
#indices.append(":".join("" if x is None else str(x) for x in [entry.start, entry.stop, entry.step]))
indices
.
append
(
Subtensor
.
str_from_slice
(
entry
))
else
:
indices
.
append
(
str
(
entry
))
if
self
.
inplace
:
msg
=
'Inplace'
msg
=
'Inplace'
else
:
msg
=
''
#backport
#return "%s%s{%s}" % ('Inplace' if self.inplace else '',
msg
=
''
return
"
%
s
%
s{
%
s}"
%
(
msg
,
self
.
__class__
.
__name__
,
", "
.
join
(
indices
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论