Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
84c51913
提交
84c51913
authored
12月 04, 2014
作者:
Sina Honari
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
resolving the conflict for git rebase
上级
1d2eec53
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
104 行增加
和
54 行删除
+104
-54
downsample.py
theano/tensor/signal/downsample.py
+104
-54
没有找到文件。
theano/tensor/signal/downsample.py
浏览文件 @
84c51913
...
...
@@ -236,7 +236,8 @@ class DownsampleFactorMax(Op):
gz
,
=
grads
maxout
=
self
(
x
)
return
[
DownsampleFactorMaxGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
)(
ignore_border
=
self
.
ignore_border
,
st
=
self
.
st
)(
x
,
maxout
,
gz
)]
def
c_code_tmp
(
self
,
node
,
name
,
inp
,
out
,
sub
):
...
...
@@ -317,21 +318,26 @@ class DownsampleFactorMax(Op):
class
DownsampleFactorMaxGrad
(
Op
):
def
__init__
(
self
,
ds
,
ignore_border
):
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
):
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
if
st
is
None
:
st
=
ds
self
.
st
=
tuple
(
st
)
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
st
==
other
.
st
and
self
.
ignore_border
==
other
.
ignore_border
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
\
hash
(
self
.
st
)
^
hash
(
self
.
ignore_border
)
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
return
'
%
s{
%
s,
%
s
,
%
s
}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
st
,
self
.
ignore_border
)
def
make_node
(
self
,
x
,
maxout
,
gz
):
# make_node should only be called by the grad function of
...
...
@@ -347,22 +353,27 @@ class DownsampleFactorMaxGrad(Op):
gx_stg
,
=
out
gx
=
numpy
.
zeros_like
(
x
)
#number of pooling output rows
pr
=
maxout
.
shape
[
-
2
]
#number of pooling output cols
pc
=
maxout
.
shape
[
-
1
]
ds0
,
ds1
=
self
.
ds
shape2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
if
not
self
.
ignore_border
:
shape2
=
x
.
shape
[
2
]
shape3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
if
not
self
.
ignore_border
:
shape3
=
x
.
shape
[
3
]
st0
,
st1
=
self
.
st
img_rows
=
x
.
shape
[
-
2
]
img_cols
=
x
.
shape
[
-
1
]
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
if
(
maxout
[
n
,
k
,
zi
,
zj
]
==
x
[
n
,
k
,
i
,
j
]):
gx
[
n
,
k
,
i
,
j
]
=
gz
[
n
,
k
,
zi
,
zj
]
# No else clause needed as it is allocated with zeros
for
r
in
xrange
(
pr
):
row_st
=
r
*
st0
row_end
=
__builtin__
.
min
(
row_st
+
ds0
,
img_rows
)
for
c
in
xrange
(
pc
):
col_st
=
c
*
st1
col_end
=
__builtin__
.
min
(
col_st
+
ds1
,
img_cols
)
for
row_ind
in
xrange
(
row_st
,
row_end
):
for
col_ind
in
xrange
(
col_st
,
col_end
):
if
(
maxout
[
n
,
k
,
r
,
c
]
==
x
[
n
,
k
,
row_ind
,
col_ind
]):
gx
[
n
,
k
,
row_ind
,
col_ind
]
=
gz
[
n
,
k
,
r
,
c
]
gx_stg
[
0
]
=
gx
def
infer_shape
(
self
,
node
,
in_shapes
):
...
...
@@ -374,9 +385,9 @@ class DownsampleFactorMaxGrad(Op):
return
[
theano
.
tensor
.
zeros_like
(
x
),
theano
.
tensor
.
zeros_like
(
maxout
),
DownsampleFactorMaxGradGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
)(
x
,
maxout
,
ggx
)]
self
.
ds
,
ignore_border
=
self
.
ignore_border
,
st
=
self
.
st
)(
x
,
maxout
,
ggx
)]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
_tmp
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
z
,
gz
=
inp
gx
,
=
out
fail
=
sub
[
'fail'
]
...
...
@@ -475,7 +486,7 @@ class DownsampleFactorMaxGrad(Op):
class
DownsampleFactorMaxGradGrad
(
Op
):
@staticmethod
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
):
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
,
st
=
None
):
"""Return the shape of the output from this op, for input of given
shape and flags.
...
...
@@ -485,11 +496,15 @@ class DownsampleFactorMaxGradGrad(Op):
scalar Theano variable.
:param ds: downsample factor over rows and columns
this parameter indicates the size of the pooling region
: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 partial downsampling (False) or ignore
it (True).
:param st: the stride size. This is the distance between the pooling
regions. If it's set to None, in which case it equlas ds.
:type st: list or tuple of two ints
:param ignore_border: if ds doesn't divide imgshape, do we include an
extra row/col of partial downsampling (False) or ignore it (True).
:type ignore_border: bool
:rtype: list
...
...
@@ -500,35 +515,66 @@ class DownsampleFactorMaxGradGrad(Op):
if
len
(
imgshape
)
<
2
:
raise
TypeError
(
'imgshape must have at least two elements '
'(rows, cols)'
)
if
st
is
None
:
st
=
ds
r
,
c
=
imgshape
[
-
2
:]
rval
=
list
(
imgshape
[:
-
2
])
+
[
r
//
ds
[
0
],
c
//
ds
[
1
]]
if
not
ignore_border
:
if
ignore_border
:
out_r
=
(
r
-
ds
[
0
])
//
st
[
0
]
+
1
out_c
=
(
c
-
ds
[
1
])
//
st
[
1
]
+
1
if
isinstance
(
r
,
theano
.
Variable
):
rval
[
-
2
]
=
tensor
.
switch
(
r
%
ds
[
0
],
rval
[
-
2
]
+
1
,
rval
[
-
2
]
)
el
if
r
%
ds
[
0
]
:
rval
[
-
2
]
+=
1
nr
=
tensor
.
maximum
(
out_r
,
0
)
el
se
:
nr
=
numpy
.
maximum
(
out_r
,
0
)
if
isinstance
(
c
,
theano
.
Variable
):
rval
[
-
1
]
=
tensor
.
switch
(
c
%
ds
[
1
],
rval
[
-
1
]
+
1
,
rval
[
-
1
])
elif
c
%
ds
[
1
]:
rval
[
-
1
]
+=
1
nc
=
tensor
.
maximum
(
out_c
,
0
)
else
:
nc
=
numpy
.
maximum
(
out_c
,
0
)
else
:
if
isinstance
(
r
,
theano
.
Variable
):
nr
=
tensor
.
switch
(
tensor
.
ge
(
st
[
0
],
ds
[
0
]),
(
r
-
1
)
//
st
[
0
]
+
1
,
tensor
.
maximum
(
0
,
(
r
-
1
-
ds
[
0
])
//
st
[
0
]
+
1
)
+
1
)
elif
st
[
0
]
>=
ds
[
0
]:
nr
=
(
r
-
1
)
//
st
[
0
]
+
1
else
:
nr
=
max
(
0
,
(
r
-
1
-
ds
[
0
])
//
st
[
0
]
+
1
)
+
1
if
isinstance
(
c
,
theano
.
Variable
):
nc
=
tensor
.
switch
(
tensor
.
ge
(
st
[
1
],
ds
[
1
]),
(
c
-
1
)
//
st
[
1
]
+
1
,
tensor
.
maximum
(
0
,
(
c
-
1
-
ds
[
1
])
//
st
[
1
]
+
1
)
+
1
)
elif
st
[
1
]
>=
ds
[
1
]:
nc
=
(
c
-
1
)
//
st
[
1
]
+
1
else
:
nc
=
max
(
0
,
(
c
-
1
-
ds
[
1
])
//
st
[
1
]
+
1
)
+
1
rval
=
list
(
imgshape
[:
-
2
])
+
[
nr
,
nc
]
return
rval
def
__init__
(
self
,
ds
,
ignore_border
):
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
):
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
if
st
is
None
:
st
=
ds
self
.
st
=
tuple
(
st
)
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
st
==
other
.
st
and
self
.
ignore_border
==
other
.
ignore_border
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
\
hash
(
self
.
st
)
^
hash
(
self
.
ignore_border
)
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s
}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
return
'
%
s{
%
s,
%
s
,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
st
,
self
.
ignore_border
)
def
make_node
(
self
,
x
,
maxout
,
gz
):
# make_node should only be called by the grad function of
...
...
@@ -540,38 +586,42 @@ class DownsampleFactorMaxGradGrad(Op):
return
Apply
(
self
,
[
x
,
maxout
,
gz
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inp
,
out
):
x
,
maxout
,
ggx
=
inp
z
,
=
out
if
len
(
x
.
shape
)
!=
4
:
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
,
self
.
st
)
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
,
self
.
st
))
z
[
0
]
=
theano
.
_asarray
(
z
[
0
],
dtype
=
x
.
dtype
)
ggz
=
z
[
0
]
## zz needs to be initialized with -inf for the following to work
ggz
-=
numpy
.
inf
#number of pooling output rows
pr
=
ggz
.
shape
[
-
2
]
#number of pooling output cols
pc
=
ggz
.
shape
[
-
1
]
ds0
,
ds1
=
self
.
ds
if
self
.
ignore_border
:
x_usable2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
else
:
x_usable2
=
x
.
shape
[
2
]
if
self
.
ignore_border
:
x_usable3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
else
:
x_usable3
=
x
.
shape
[
3
]
st0
,
st1
=
self
.
st
img_rows
=
x
.
shape
[
-
2
]
img_cols
=
x
.
shape
[
-
1
]
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
if
(
maxout
[
n
,
k
,
zi
,
zj
]
==
x
[
n
,
k
,
i
,
j
]):
ggz
[
n
,
k
,
zi
,
zj
]
=
ggx
[
n
,
k
,
i
,
j
]
for
r
in
xrange
(
pr
):
row_st
=
r
*
st0
row_end
=
__builtin__
.
min
(
row_st
+
ds0
,
img_rows
)
for
c
in
xrange
(
pc
):
col_st
=
c
*
st1
col_end
=
__builtin__
.
min
(
col_st
+
ds1
,
img_cols
)
for
row_ind
in
xrange
(
row_st
,
row_end
):
for
col_ind
in
xrange
(
col_st
,
col_end
):
if
(
maxout
[
n
,
k
,
r
,
c
]
==
x
[
n
,
k
,
row_ind
,
col_ind
]):
ggz
[
n
,
k
,
r
,
c
]
=
ggx
[
n
,
k
,
row_ind
,
col_ind
]
def
infer_shape
(
self
,
node
,
in_shapes
):
return
[
in_shapes
[
0
]]
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论