Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
238de8a4
提交
238de8a4
authored
6月 08, 2016
作者:
root
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pool optimization
上级
7176a3d1
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
26 行删除
+46
-26
pool.py
theano/tensor/signal/pool.py
+46
-26
没有找到文件。
theano/tensor/signal/pool.py
浏览文件 @
238de8a4
...
...
@@ -14,7 +14,7 @@ from six.moves import xrange
import
six.moves.builtins
as
builtins
import
theano
from
theano
import
gof
,
Op
,
tensor
,
Variable
,
Apply
from
theano
import
gof
,
Op
enMPOp
,
tensor
,
Variable
,
Apply
def
max_pool_2d_same_size
(
input
,
patch_size
):
...
...
@@ -114,7 +114,7 @@ def pool_2d(input, ds, ignore_border=None, st=None, padding=(0, 0),
return
tensor
.
reshape
(
output
,
outshp
,
ndim
=
input
.
ndim
)
class
Pool
(
Op
):
class
Pool
(
Op
enMPOp
):
"""
For N-dimensional tensors, consider that the last two dimensions span
images. This Op downsamples these images by taking the max, sum or average
...
...
@@ -236,7 +236,8 @@ class Pool(Op):
return
rval
def
__init__
(
self
,
ds
,
ignore_border
=
False
,
st
=
None
,
padding
=
(
0
,
0
),
mode
=
'max'
):
mode
=
'max'
,
openmp
=
None
):
super
(
Pool
,
self
)
.
__init__
(
openmp
=
openmp
)
self
.
ds
=
tuple
(
ds
)
if
not
all
([
isinstance
(
d
,
integer_types
)
for
d
in
ds
]):
raise
ValueError
(
...
...
@@ -350,7 +351,9 @@ class Pool(Op):
x
,
gz
)]
def
c_headers
(
self
):
return
[
'<algorithm>'
]
headers
=
[
'<algorithm>'
]
headers
+=
super
(
Pool
,
self
)
.
c_headers
()
return
headers
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
if
self
.
mode
not
in
(
'max'
,
'sum'
,
'average_exc_pad'
,
'average_inc_pad'
):
...
...
@@ -362,6 +365,10 @@ class Pool(Op):
ds0
,
ds1
=
self
.
ds
st0
,
st1
=
self
.
st
pd0
,
pd1
=
self
.
padding
if
self
.
openmp
:
omp_parallel
=
'#pragma omp parallel for private(r_st, r_end, c_st, c_end, collector) schedule(static)'
else
:
omp_parallel
=
''
ccode
=
"""
int typenum = PyArray_ObjectType((PyObject*)
%(x)
s, 0);
int z_r, z_c; // shape of the output
...
...
@@ -443,13 +450,15 @@ class Pool(Op):
%(z)
s = (PyArrayObject*) PyArray_ZEROS(4, dims, typenum,0);
}
// used for indexing a pool region inside the input
int r_st, r_end, c_st, c_end;
dtype_
%(x)
s collector; // temp var for the value in a region
if (z_r && z_c)
{
for(int b=0; b<PyArray_DIMS(
%(x)
s)[0]; b++){
for(int k=0; k<PyArray_DIMS(
%(x)
s)[1]; k++){
for(int i=0; i< z_r; i++){
int r_st, r_end, c_st, c_end;
%(omp_parallel)
s
for(int t = 0; t < PyArray_DIMS(
%(x)
s)[0] * PyArray_DIMS(
%(x)
s)[1]; t++){
int b = t / PyArray_DIMS(
%(x)
s)[1];
int k = t
%%
PyArray_DIMS(
%(x)
s)[1];
for(int i=0; i < z_r; i++){
r_st = i *
%(st0)
s;
r_end = r_st +
%(ds0)
s;
// skip the padding
...
...
@@ -526,15 +535,14 @@ class Pool(Op):
}
}
}
}
"""
return
ccode
%
locals
()
def
c_code_cache_version
(
self
):
return
(
0
,
6
,
8
,
4
)
return
(
0
,
6
,
8
,
4
,
self
.
openmp
)
class
PoolGrad
(
Op
):
class
PoolGrad
(
Op
enMPOp
):
__props__
=
(
'ds'
,
'ignore_border'
,
'st'
,
'padding'
,
'mode'
)
@staticmethod
...
...
@@ -617,7 +625,7 @@ class PoolGrad(Op):
rval
=
list
(
imgshape
[:
-
2
])
+
[
nr
,
nc
]
return
rval
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
,
padding
=
(
0
,
0
),
mode
=
'max'
):
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
,
padding
=
(
0
,
0
),
mode
=
'max'
,
openmp
=
None
):
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
if
st
is
None
:
...
...
@@ -629,6 +637,7 @@ class PoolGrad(Op):
"Pool mode parameter only support 'max', 'sum',"
" 'average_inc_pad' and 'average_exc_pad'. Got
%
s"
%
mode
)
self
.
mode
=
mode
super
(
PoolGrad
,
self
)
.
__init__
(
openmp
=
openmp
)
def
infer_shape
(
self
,
node
,
in_shapes
):
return
[
in_shapes
[
0
]]
...
...
@@ -708,6 +717,10 @@ class MaxPoolGrad(PoolGrad):
ds0
,
ds1
=
self
.
ds
st0
,
st1
=
self
.
st
pd0
,
pd1
=
self
.
padding
if
self
.
openmp
:
omp_parallel
=
'#pragma omp parallel for private(r_st, r_end, c_st, c_end, maximum) schedule(static)'
else
:
omp_parallel
=
''
return
"""
// sanity checks
int x_typenum = PyArray_ObjectType((PyObject*)
%(x)
s, 0);
...
...
@@ -757,13 +770,15 @@ class MaxPoolGrad(PoolGrad):
else {
PyArray_FILLWBYTE(
%(gx)
s, 0);
}
int r_st, r_end, c_st, c_end; // used to index into the input img x
dtype_
%(z)
s maximum; // temp var for maximum value in a region
if (z_r && z_c)
{
for(int b=0; b<PyArray_DIMS(
%(x)
s)[0]; b++){
for(int k=0; k<PyArray_DIMS(
%(x)
s)[1]; k++){
for(int i=0; i< z_r; i++){
int r_st, r_end, c_st, c_end;
%(omp_parallel)
s
for(int t = 0; t < PyArray_DIMS(
%(x)
s)[0] * PyArray_DIMS(
%(x)
s)[1]; t++){
int b = t / PyArray_DIMS(
%(x)
s)[1];
int k = t
%%
PyArray_DIMS(
%(x)
s)[1];
for(int i=0; i < z_r; i++){
r_st = i *
%(st0)
s;
r_end = r_st +
%(ds0)
s;
// skip the padding
...
...
@@ -803,11 +818,10 @@ class MaxPoolGrad(PoolGrad):
}
}
}
}
"""
%
locals
()
def
c_code_cache_version
(
self
):
return
(
0
,
7
)
return
(
0
,
7
,
self
.
openmp
)
class
AveragePoolGrad
(
PoolGrad
):
...
...
@@ -895,10 +909,10 @@ class AveragePoolGrad(PoolGrad):
st
=
self
.
st
,
padding
=
self
.
padding
,
mode
=
self
.
mode
)(
ggx
)]
class
DownsampleFactorMaxGradGrad
(
Op
):
class
DownsampleFactorMaxGradGrad
(
Op
enMPOp
):
__props__
=
(
'ds'
,
'ignore_border'
,
'st'
,
'padding'
,
'mode'
)
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
,
padding
=
(
0
,
0
),
mode
=
'max'
):
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
,
padding
=
(
0
,
0
),
mode
=
'max'
,
openmp
=
None
):
self
.
ds
=
tuple
(
ds
)
if
not
all
([
isinstance
(
d
,
integer_types
)
for
d
in
ds
]):
raise
ValueError
(
...
...
@@ -917,6 +931,7 @@ class DownsampleFactorMaxGradGrad(Op):
raise
NotImplementedError
(
'padding_h and padding_w must be smaller than strides'
)
self
.
mode
=
mode
super
(
DownsampleFactorMaxGradGrad
,
self
)
.
__init__
(
openmp
=
openmp
)
assert
self
.
mode
==
'max'
def
make_node
(
self
,
x
,
maxout
,
gz
):
...
...
@@ -990,6 +1005,10 @@ class DownsampleFactorMaxGradGrad(Op):
ds0
,
ds1
=
self
.
ds
st0
,
st1
=
self
.
st
pd0
,
pd1
=
self
.
padding
if
self
.
openmp
:
omp_parallel
=
'#pragma omp parallel for private(r_st, r_end, c_st, c_end, maximum) schedule(static)'
else
:
omp_parallel
=
''
return
"""
int z_typenum = PyArray_ObjectType((PyObject*)
%(maxout)
s, 0);
int z_r, z_c;
...
...
@@ -1017,10 +1036,12 @@ class DownsampleFactorMaxGradGrad(Op):
PyArray_FILLWBYTE(
%(z)
s, 0);
}
dtype_
%(maxout)
s maximum; // temp var for maximum value in a region
int r_st, r_end, c_st, c_end; // used to index into the input img x
for(int b=0; b<PyArray_DIMS(
%(x)
s)[0]; b++){
for(int k=0; k<PyArray_DIMS(
%(x)
s)[1]; k++){
for(int i=0; i< z_r; i++){
int r_st, r_end, c_st, c_end;
%(omp_parallel)
s
for(int t = 0; t < PyArray_DIMS(
%(x)
s)[0] * PyArray_DIMS(
%(x)
s)[1]; t++){
int b = t / PyArray_DIMS(
%(x)
s)[1];
int k = t
%%
PyArray_DIMS(
%(x)
s)[1];
for(int i=0; i < z_r; i++){
r_st = i *
%(st0)
s;
r_end = r_st +
%(ds0)
s;
// skip the padding
...
...
@@ -1058,8 +1079,7 @@ class DownsampleFactorMaxGradGrad(Op):
}
}
}
}
"""
%
locals
()
def
c_code_cache_version
(
self
):
return
(
0
,
1
)
return
(
0
,
1
,
self
.
openmp
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论