Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
8d55ea28
提交
8d55ea28
authored
12月 11, 2013
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
f01c2cd6
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
67 行增加
和
48 行删除
+67
-48
downsample.py
theano/tensor/signal/downsample.py
+67
-48
没有找到文件。
theano/tensor/signal/downsample.py
浏览文件 @
8d55ea28
...
@@ -26,11 +26,13 @@ def max_pool_2d(input, ds, ignore_border=False):
...
@@ -26,11 +26,13 @@ def max_pool_2d(input, ds, ignore_border=False):
patches of size (ds[0],ds[1])
patches of size (ds[0],ds[1])
:type input: N-D theano tensor of input images.
:type input: N-D theano tensor of input images.
:param input: input images. Max pooling will be done over the 2 last dimensions.
:param input: input images. Max pooling will be done over the 2 last
dimensions.
:type ds: tuple of length 2
:type ds: tuple of length 2
:param ds: factor by which to downscale. (2,2) will halve the image in each dimension.
:param ds: factor by which to downscale. (2,2) will halve the image in
:param ignore_border: boolean value. When True, (5,5) input with ds=(2,2) will generate a
each dimension.
(2,2) output. (3,3) otherwise.
:param ignore_border: boolean value. When True, (5,5) input with ds=(2,2)
will generate a (2,2) output. (3,3) otherwise.
"""
"""
if
input
.
ndim
<
2
:
if
input
.
ndim
<
2
:
raise
NotImplementedError
(
'max_pool_2d requires a dimension >= 2'
)
raise
NotImplementedError
(
'max_pool_2d requires a dimension >= 2'
)
...
@@ -44,7 +46,7 @@ def max_pool_2d(input, ds, ignore_border=False):
...
@@ -44,7 +46,7 @@ def max_pool_2d(input, ds, ignore_border=False):
# store as 4D tensor with shape: (batch_size,1,height,width)
# store as 4D tensor with shape: (batch_size,1,height,width)
new_shape
=
tensor
.
cast
(
tensor
.
join
(
0
,
batch_size
,
new_shape
=
tensor
.
cast
(
tensor
.
join
(
0
,
batch_size
,
tensor
.
as_tensor
([
1
,
]),
tensor
.
as_tensor
([
1
]),
img_shape
),
'int64'
)
img_shape
),
'int64'
)
input_4D
=
tensor
.
reshape
(
input
,
new_shape
,
ndim
=
4
)
input_4D
=
tensor
.
reshape
(
input
,
new_shape
,
ndim
=
4
)
...
@@ -67,27 +69,29 @@ class DownsampleFactorMax(Op):
...
@@ -67,27 +69,29 @@ class DownsampleFactorMax(Op):
@staticmethod
@staticmethod
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
):
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
):
"""Return the shape of the output from this op, for input of given shape and flags.
"""Return the shape of the output from this op, for input of given
shape and flags.
:param imgshape: the shape of a tensor of images. The last two elements
are interpreted
:param imgshape: the shape of a tensor of images. The last two elements
as the number of rows, and the number of cols.
are interpreted
as the number of rows, and the number of cols.
:type imgshape: tuple, list, or similar of integer or
:type imgshape: tuple, list, or similar of integer or
scalar Theano variable.
scalar Theano variable.
:param ds: downsample factor over rows and columns
:param ds: downsample factor over rows and columns
:type ds: list or tuple of two ints
:type ds: list or tuple of two ints
:param ignore_border: if ds doesn't divide imgshape, do we include an
extra row/col of
:param ignore_border: if ds doesn't divide imgshape, do we include an
partial downsampling (False) or ignore it (True).
extra row/col of
partial downsampling (False) or ignore it (True).
:type ignore_border: bool
:type ignore_border: bool
:rtype: list
:rtype: list
:returns: the shape of the output from this op, for input of given
shape. This will
:returns: the shape of the output from this op, for input of given
have the same length as imgshape, but with last two elements reduced as per the
shape. This will have the same length as imgshape, but with last
downsampling & ignore_border flags.
two elements reduced as per the
downsampling & ignore_border flags.
"""
"""
if
len
(
imgshape
)
<
2
:
if
len
(
imgshape
)
<
2
:
raise
TypeError
(
'imgshape must have at least two elements (rows, cols)'
)
raise
TypeError
(
'imgshape must have at least two elements '
'(rows, cols)'
)
r
,
c
=
imgshape
[
-
2
:]
r
,
c
=
imgshape
[
-
2
:]
rval
=
list
(
imgshape
[:
-
2
])
+
[
r
//
ds
[
0
],
c
//
ds
[
1
]]
rval
=
list
(
imgshape
[:
-
2
])
+
[
r
//
ds
[
0
],
c
//
ds
[
1
]]
...
@@ -107,8 +111,9 @@ class DownsampleFactorMax(Op):
...
@@ -107,8 +111,9 @@ class DownsampleFactorMax(Op):
:param ds: downsample factor over rows and columns
:param ds: downsample factor over rows and columns
:type ds: list or tuple of two ints
:type ds: list or tuple of two ints
:param ignore_border: if ds doesn't divide imgshape, do we include an extra row/col of
:param ignore_border: if ds doesn't divide imgshape, do we include
partial downsampling (False) or ignore it (True).
an extra row/col of partial downsampling (False) or
ignore it (True).
:type ignore_border: bool
:type ignore_border: bool
TODO: why is poolsize an op parameter here?
TODO: why is poolsize an op parameter here?
...
@@ -302,9 +307,10 @@ class DownsampleFactorMaxGrad(Op):
...
@@ -302,9 +307,10 @@ class DownsampleFactorMaxGrad(Op):
zi
=
i
//
ds0
zi
=
i
//
ds0
for
j
in
xrange
(
shape3
):
for
j
in
xrange
(
shape3
):
zj
=
j
//
ds1
zj
=
j
//
ds1
if
(
maxout
[
n
,
k
,
zi
,
zj
]
==
x
[
n
,
k
,
i
,
j
]):
if
(
maxout
[
n
,
k
,
zi
,
zj
]
==
x
[
n
,
k
,
i
,
j
]):
gx
[
n
,
k
,
i
,
j
]
=
gz
[
n
,
k
,
zi
,
zj
]
gx
[
n
,
k
,
i
,
j
]
=
gz
[
n
,
k
,
zi
,
zj
]
else
:
gx
[
n
,
k
,
i
,
j
]
=
0
else
:
gx
[
n
,
k
,
i
,
j
]
=
0
gx_stg
[
0
]
=
gx
gx_stg
[
0
]
=
gx
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
...
@@ -313,7 +319,10 @@ class DownsampleFactorMaxGrad(Op):
...
@@ -313,7 +319,10 @@ class DownsampleFactorMaxGrad(Op):
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
x
,
maxout
,
gz
=
inp
x
,
maxout
,
gz
=
inp
ggx
,
=
grads
ggx
,
=
grads
return
[
theano
.
tensor
.
zeros_like
(
x
),
theano
.
tensor
.
zeros_like
(
maxout
),
DownsampleFactorMaxGradGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
)(
x
,
maxout
,
ggx
)]
return
[
theano
.
tensor
.
zeros_like
(
x
),
theano
.
tensor
.
zeros_like
(
maxout
),
DownsampleFactorMaxGradGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
)(
x
,
maxout
,
ggx
)]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
z
,
gz
=
inp
x
,
z
,
gz
=
inp
...
@@ -405,38 +414,42 @@ class DownsampleFactorMaxGrad(Op):
...
@@ -405,38 +414,42 @@ class DownsampleFactorMaxGrad(Op):
}
}
}//for k
}//for k
}//for b
}//for b
"""
%
locals
()
"""
%
locals
()
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
0
,
1
)
return
(
0
,
1
)
class
DownsampleFactorMaxGradGrad
(
Op
):
class
DownsampleFactorMaxGradGrad
(
Op
):
@staticmethod
@staticmethod
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
):
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
):
"""Return the shape of the output from this op, for input of given shape and flags.
"""Return the shape of the output from this op, for input of given
shape and flags.
:param imgshape: the shape of a tensor of images. The last two elements are interpreted
:param imgshape: the shape of a tensor of images. The last two elements
as the number of rows, and the number of cols.
are interpreted
as the number of rows, and the number of cols.
:type imgshape: tuple, list, or similar of integer or
:type imgshape: tuple, list, or similar of integer or
scalar Theano variable.
scalar Theano variable.
:param ds: downsample factor over rows and columns
:param ds: downsample factor over rows and columns
:type ds: list or tuple of two ints
:type ds: list or tuple of two ints
:param ignore_border: if ds doesn't divide imgshape, do we include an extra row/col of
:param ignore_border: if ds doesn't divide imgshape, do we include
partial downsampling (False) or ignore it (True).
an extra row/col of partial downsampling (False) or ignore
it (True).
:type ignore_border: bool
:type ignore_border: bool
:rtype: list
:rtype: list
:returns: the shape of the output from this op, for input of given shape. This will
:returns: the shape of the output from this op, for input of given
have the same length as imgshape, but with last two elements reduced as per the
shape. This will have the same length as imgshape, but with last
downsampling & ignore_border flags.
two elements reduced as per the
downsampling & ignore_border flags.
"""
"""
if
len
(
imgshape
)
<
2
:
if
len
(
imgshape
)
<
2
:
raise
TypeError
(
'imgshape must have at least two elements (rows, cols)'
)
raise
TypeError
(
'imgshape must have at least two elements '
'(rows, cols)'
)
r
,
c
=
imgshape
[
-
2
:]
r
,
c
=
imgshape
[
-
2
:]
rval
=
list
(
imgshape
[:
-
2
])
+
[
r
//
ds
[
0
],
c
//
ds
[
1
]
]
rval
=
list
(
imgshape
[:
-
2
])
+
[
r
//
ds
[
0
],
c
//
ds
[
1
]
]
if
not
ignore_border
:
if
not
ignore_border
:
if
isinstance
(
r
,
theano
.
Variable
):
if
isinstance
(
r
,
theano
.
Variable
):
...
@@ -454,20 +467,23 @@ class DownsampleFactorMaxGradGrad(Op):
...
@@ -454,20 +467,23 @@ class DownsampleFactorMaxGradGrad(Op):
self
.
ignore_border
=
ignore_border
self
.
ignore_border
=
ignore_border
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
return
(
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
def
__str__
(
self
):
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
def
make_node
(
self
,
x
,
maxout
,
gz
):
def
make_node
(
self
,
x
,
maxout
,
gz
):
# make_node should only be called by the grad function of DownsampleFactorMax,
# make_node should only be called by the grad function of
#
so these asserts should not fail.
# DownsampleFactorMaxGrad,
so these asserts should not fail.
assert
isinstance
(
x
,
Variable
)
and
x
.
ndim
==
4
assert
isinstance
(
x
,
Variable
)
and
x
.
ndim
==
4
assert
isinstance
(
maxout
,
Variable
)
and
maxout
.
ndim
==
4
assert
isinstance
(
maxout
,
Variable
)
and
maxout
.
ndim
==
4
assert
isinstance
(
gz
,
Variable
)
and
gz
.
ndim
==
4
assert
isinstance
(
gz
,
Variable
)
and
gz
.
ndim
==
4
return
Apply
(
self
,
[
x
,
maxout
,
gz
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
,
maxout
,
gz
],
[
x
.
type
()])
...
@@ -476,21 +492,25 @@ class DownsampleFactorMaxGradGrad(Op):
...
@@ -476,21 +492,25 @@ class DownsampleFactorMaxGradGrad(Op):
x
,
maxout
,
ggx
=
inp
x
,
maxout
,
ggx
=
inp
z
,
=
out
z
,
=
out
if
len
(
x
.
shape
)
!=
4
:
if
len
(
x
.
shape
)
!=
4
:
raise
NotImplementedError
(
'DownsampleFactorMax requires 4D input for now'
)
raise
NotImplementedError
(
'DownsampleFactorMaxGradGrad requires 4D input for now'
)
z_shape
=
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
)
z_shape
=
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
)
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
z
[
0
]
=
numpy
.
zeros
(
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
))
z
[
0
]
=
numpy
.
zeros
(
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
))
z
[
0
]
=
theano
.
_asarray
(
z
[
0
],
dtype
=
x
.
dtype
)
z
[
0
]
=
theano
.
_asarray
(
z
[
0
],
dtype
=
x
.
dtype
)
ggz
=
z
[
0
]
ggz
=
z
[
0
]
ds0
,
ds1
=
self
.
ds
ds0
,
ds1
=
self
.
ds
if
self
.
ignore_border
:
if
self
.
ignore_border
:
x_usable2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
x_usable2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
else
:
x_usable2
=
x
.
shape
[
2
]
else
:
x_usable2
=
x
.
shape
[
2
]
if
self
.
ignore_border
:
if
self
.
ignore_border
:
x_usable3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
x_usable3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
else
:
x_usable3
=
x
.
shape
[
3
]
else
:
x_usable3
=
x
.
shape
[
3
]
for
n
in
xrange
(
x
.
shape
[
0
]):
for
n
in
xrange
(
x
.
shape
[
0
]):
for
k
in
xrange
(
x
.
shape
[
1
]):
for
k
in
xrange
(
x
.
shape
[
1
]):
...
@@ -498,9 +518,8 @@ class DownsampleFactorMaxGradGrad(Op):
...
@@ -498,9 +518,8 @@ class DownsampleFactorMaxGradGrad(Op):
zi
=
i
//
ds0
zi
=
i
//
ds0
for
j
in
xrange
(
x_usable3
):
for
j
in
xrange
(
x_usable3
):
zj
=
j
//
ds1
zj
=
j
//
ds1
if
(
maxout
[
n
,
k
,
zi
,
zj
]
==
x
[
n
,
k
,
i
,
j
]):
if
(
maxout
[
n
,
k
,
zi
,
zj
]
==
x
[
n
,
k
,
i
,
j
]):
ggz
[
n
,
k
,
zi
,
zj
]
=
ggx
[
n
,
k
,
i
,
j
]
ggz
[
n
,
k
,
zi
,
zj
]
=
ggx
[
n
,
k
,
i
,
j
]
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
return
[
in_shapes
[
0
]]
return
[
in_shapes
[
0
]]
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论