Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c58953f6
提交
c58953f6
authored
3月 25, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
da609fe9
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
29 行增加
和
29 行删除
+29
-29
pool.py
theano/tensor/signal/pool.py
+29
-29
没有找到文件。
theano/tensor/signal/pool.py
浏览文件 @
c58953f6
...
@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
import
warnings
import
warnings
import
itertools
import
itertools
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
six.moves.builtins
as
builtins
import
six.moves.builtins
as
builtins
import
theano
import
theano
...
@@ -412,7 +412,7 @@ class Pool(OpenMPOp):
...
@@ -412,7 +412,7 @@ class Pool(OpenMPOp):
if
isinstance
(
out
,
theano
.
Variable
):
if
isinstance
(
out
,
theano
.
Variable
):
return
tensor
.
maximum
(
out
,
0
)
return
tensor
.
maximum
(
out
,
0
)
else
:
else
:
return
n
umpy
.
maximum
(
out
,
0
)
return
n
p
.
maximum
(
out
,
0
)
else
:
else
:
if
isinstance
(
v
,
theano
.
Variable
):
if
isinstance
(
v
,
theano
.
Variable
):
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
...
@@ -516,7 +516,7 @@ class Pool(OpenMPOp):
...
@@ -516,7 +516,7 @@ class Pool(OpenMPOp):
if
not
self
.
ignore_border
:
if
not
self
.
ignore_border
:
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
z
[
0
]
=
n
umpy
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
z
[
0
]
=
n
p
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
zz
=
z
[
0
]
zz
=
z
[
0
]
# size of pooling output
# size of pooling output
pool_out_shp
=
zz
.
shape
[
-
nd
:]
pool_out_shp
=
zz
.
shape
[
-
nd
:]
...
@@ -525,16 +525,16 @@ class Pool(OpenMPOp):
...
@@ -525,16 +525,16 @@ class Pool(OpenMPOp):
# pad the image
# pad the image
if
max
(
pad
)
!=
0
:
if
max
(
pad
)
!=
0
:
y
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
else
:
else
:
y
=
x
y
=
x
func
=
n
umpy
.
max
func
=
n
p
.
max
if
self
.
mode
==
'sum'
:
if
self
.
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
self
.
mode
!=
'max'
:
elif
self
.
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
# precompute the region boundaries for each dimension
# precompute the region boundaries for each dimension
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
...
@@ -548,11 +548,11 @@ class Pool(OpenMPOp):
...
@@ -548,11 +548,11 @@ class Pool(OpenMPOp):
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
zzk
=
zz
[
k
]
zzk
=
zz
[
k
]
yk
=
y
[
k
]
yk
=
y
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
zzk
[
r
]
=
func
(
zzk
[
r
]
=
func
(
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]])
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]])
...
@@ -1020,7 +1020,7 @@ class PoolGrad(OpenMPOp):
...
@@ -1020,7 +1020,7 @@ class PoolGrad(OpenMPOp):
if
isinstance
(
out
,
theano
.
Variable
):
if
isinstance
(
out
,
theano
.
Variable
):
return
tensor
.
maximum
(
out
,
0
)
return
tensor
.
maximum
(
out
,
0
)
else
:
else
:
return
n
umpy
.
maximum
(
out
,
0
)
return
n
p
.
maximum
(
out
,
0
)
else
:
else
:
if
isinstance
(
v
,
theano
.
Variable
):
if
isinstance
(
v
,
theano
.
Variable
):
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
...
@@ -1128,12 +1128,12 @@ class MaxPoolGrad(PoolGrad):
...
@@ -1128,12 +1128,12 @@ class MaxPoolGrad(PoolGrad):
# pad the image
# pad the image
if
max
(
pad
)
!=
0
:
if
max
(
pad
)
!=
0
:
y
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
else
:
else
:
y
=
x
y
=
x
gx
=
n
umpy
.
zeros_like
(
y
)
gx
=
n
p
.
zeros_like
(
y
)
# precompute the region boundaries for each dimension
# precompute the region boundaries for each dimension
region_ranges
=
[[]
for
i
in
xrange
(
nd
)]
region_ranges
=
[[]
for
i
in
xrange
(
nd
)]
...
@@ -1144,13 +1144,13 @@ class MaxPoolGrad(PoolGrad):
...
@@ -1144,13 +1144,13 @@ class MaxPoolGrad(PoolGrad):
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
gxk
=
gx
[
k
]
gxk
=
gx
[
k
]
gzk
=
gz
[
k
]
gzk
=
gz
[
k
]
yk
=
y
[
k
]
yk
=
y
[
k
]
maxoutk
=
maxout
[
k
]
maxoutk
=
maxout
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
maxout_value
=
maxoutk
[
r
]
maxout_value
=
maxoutk
[
r
]
# iterate inside region
# iterate inside region
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
...
@@ -1444,7 +1444,7 @@ class AveragePoolGrad(PoolGrad):
...
@@ -1444,7 +1444,7 @@ class AveragePoolGrad(PoolGrad):
raise
NotImplementedError
()
raise
NotImplementedError
()
z_shape
=
self
.
out_shape
(
x
.
shape
,
ws
,
self
.
ignore_border
,
stride
,
pad
,
nd
)
z_shape
=
self
.
out_shape
(
x
.
shape
,
ws
,
self
.
ignore_border
,
stride
,
pad
,
nd
)
if
(
gx_stg
[
0
]
is
None
)
or
(
gx_stg
[
0
]
.
shape
!=
z_shape
):
if
(
gx_stg
[
0
]
is
None
)
or
(
gx_stg
[
0
]
.
shape
!=
z_shape
):
gx_stg
[
0
]
=
n
umpy
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
gx_stg
[
0
]
=
n
p
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
zz
=
gx_stg
[
0
]
zz
=
gx_stg
[
0
]
# size of pooling output
# size of pooling output
pool_out_shp
=
zz
.
shape
[
-
nd
:]
pool_out_shp
=
zz
.
shape
[
-
nd
:]
...
@@ -1453,7 +1453,7 @@ class AveragePoolGrad(PoolGrad):
...
@@ -1453,7 +1453,7 @@ class AveragePoolGrad(PoolGrad):
sum_mode
=
self
.
mode
==
'sum'
sum_mode
=
self
.
mode
==
'sum'
# initialize the padded output
# initialize the padded output
gx
=
n
umpy
.
zeros
((
x
.
shape
[:
-
nd
]
+
img_shp
),
dtype
=
x
.
dtype
)
gx
=
n
p
.
zeros
((
x
.
shape
[:
-
nd
]
+
img_shp
),
dtype
=
x
.
dtype
)
# precompute the region boundaries and sizes for each dimension
# precompute the region boundaries and sizes for each dimension
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
...
@@ -1470,11 +1470,11 @@ class AveragePoolGrad(PoolGrad):
...
@@ -1470,11 +1470,11 @@ class AveragePoolGrad(PoolGrad):
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
region_slice
=
[
None
]
*
nd
region_slice
=
[
None
]
*
nd
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
gzk
=
gz
[
k
]
gzk
=
gz
[
k
]
gxk
=
gx
[
k
]
gxk
=
gx
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
region_size
=
1
region_size
=
1
for
i
in
xrange
(
nd
):
for
i
in
xrange
(
nd
):
region_slice
[
i
]
=
region_slices
[
i
][
r
[
i
]]
region_slice
[
i
]
=
region_slices
[
i
][
r
[
i
]]
...
@@ -1783,7 +1783,7 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
...
@@ -1783,7 +1783,7 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
'DownsampleFactorMaxGradGrad requires input '
'DownsampleFactorMaxGradGrad requires input '
'with {} or more dimensions'
.
format
(
nd
))
'with {} or more dimensions'
.
format
(
nd
))
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
maxout
.
shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
maxout
.
shape
):
z
[
0
]
=
n
umpy
.
zeros
(
maxout
.
shape
,
dtype
=
x
.
dtype
)
z
[
0
]
=
n
p
.
zeros
(
maxout
.
shape
,
dtype
=
x
.
dtype
)
ggz
=
z
[
0
]
# grad wrt maxout_grad has the same shape as maxout
ggz
=
z
[
0
]
# grad wrt maxout_grad has the same shape as maxout
# size of pooling output
# size of pooling output
pool_out_shp
=
ggz
.
shape
[
-
nd
:]
pool_out_shp
=
ggz
.
shape
[
-
nd
:]
...
@@ -1791,10 +1791,10 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
...
@@ -1791,10 +1791,10 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
# pad the image and its gradients
# pad the image and its gradients
if
max
(
pad
)
>
0
:
if
max
(
pad
)
>
0
:
y_padded
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y_padded
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
ggx_padded
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
ggx_padded
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
ggx_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
ggx_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ggx
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ggx
...
@@ -1811,13 +1811,13 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
...
@@ -1811,13 +1811,13 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
ggxk
=
ggx_padded
[
k
]
ggxk
=
ggx_padded
[
k
]
ggzk
=
ggz
[
k
]
ggzk
=
ggz
[
k
]
yk
=
y_padded
[
k
]
yk
=
y_padded
[
k
]
maxoutk
=
maxout
[
k
]
maxoutk
=
maxout
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
# iterate inside region
# iterate inside region
maxout_value
=
maxoutk
[
r
]
maxout_value
=
maxoutk
[
r
]
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
...
@@ -2113,7 +2113,7 @@ class MaxPoolRop(OpenMPOp):
...
@@ -2113,7 +2113,7 @@ class MaxPoolRop(OpenMPOp):
if
not
self
.
ignore_border
:
if
not
self
.
ignore_border
:
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
z
[
0
]
=
n
umpy
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
z
[
0
]
=
n
p
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
zz
=
z
[
0
]
zz
=
z
[
0
]
# size of pooling output
# size of pooling output
pool_out_shp
=
zz
.
shape
[
-
nd
:]
pool_out_shp
=
zz
.
shape
[
-
nd
:]
...
@@ -2122,10 +2122,10 @@ class MaxPoolRop(OpenMPOp):
...
@@ -2122,10 +2122,10 @@ class MaxPoolRop(OpenMPOp):
# pad the image and the eval point
# pad the image and the eval point
if
max
(
pad
)
!=
0
:
if
max
(
pad
)
!=
0
:
y
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
ey
=
n
umpy
.
zeros
(
ex
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
ex
.
dtype
)
ey
=
n
p
.
zeros
(
ex
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
ex
.
dtype
)
ey
[(
slice
(
None
),)
*
(
len
(
ex
.
shape
)
-
nd
)
+
ey
[(
slice
(
None
),)
*
(
len
(
ex
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ex
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ex
else
:
else
:
...
@@ -2144,18 +2144,18 @@ class MaxPoolRop(OpenMPOp):
...
@@ -2144,18 +2144,18 @@ class MaxPoolRop(OpenMPOp):
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
zzk
=
zz
[
k
]
zzk
=
zz
[
k
]
yk
=
y
[
k
]
yk
=
y
[
k
]
eyk
=
ey
[
k
]
eyk
=
ey
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
# current slice in padded input
# current slice in padded input
ykslice
=
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
ykslice
=
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
# current slice in eval points
# current slice in eval points
eykslice
=
eyk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
eykslice
=
eyk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
# indices of maximum
# indices of maximum
idx
=
n
umpy
.
unravel_index
(
numpy
.
argmax
(
ykslice
),
ykslice
.
shape
)
idx
=
n
p
.
unravel_index
(
np
.
argmax
(
ykslice
),
ykslice
.
shape
)
zzk
[
r
]
=
eykslice
[
idx
]
zzk
[
r
]
=
eykslice
[
idx
]
def
c_headers
(
self
):
def
c_headers
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论