Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c60f369f
提交
c60f369f
authored
3月 25, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
fba196d7
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
68 行增加
和
68 行删除
+68
-68
test_pool.py
theano/tensor/signal/tests/test_pool.py
+68
-68
没有找到文件。
theano/tensor/signal/tests/test_pool.py
浏览文件 @
c60f369f
...
@@ -10,7 +10,7 @@ from six.moves import cPickle
...
@@ -10,7 +10,7 @@ from six.moves import cPickle
import
six.moves.builtins
as
builtins
import
six.moves.builtins
as
builtins
import
sys
import
sys
import
numpy
import
numpy
as
np
import
theano
import
theano
import
theano.tensor
as
tensor
import
theano.tensor
as
tensor
...
@@ -46,14 +46,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -46,14 +46,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
=
list
(
input
.
shape
[:
-
2
])
out_shp
=
list
(
input
.
shape
[:
-
2
])
out_shp
.
append
(
input
.
shape
[
-
2
]
//
ws
[
0
]
+
xi
)
out_shp
.
append
(
input
.
shape
[
-
2
]
//
ws
[
0
]
+
xi
)
out_shp
.
append
(
input
.
shape
[
-
1
]
//
ws
[
1
]
+
yi
)
out_shp
.
append
(
input
.
shape
[
-
1
]
//
ws
[
1
]
+
yi
)
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
for
k
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
k
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii
=
i
*
ws
[
0
]
ii
=
i
*
ws
[
0
]
for
j
in
range
(
output_val
.
shape
[
-
1
]):
for
j
in
range
(
output_val
.
shape
[
-
1
]):
...
@@ -78,15 +78,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -78,15 +78,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
=
list
(
input
.
shape
[:
-
nd
])
out_shp
=
list
(
input
.
shape
[:
-
nd
])
for
i
in
range
(
nd
):
for
i
in
range
(
nd
):
out_shp
.
append
(
input
.
shape
[
-
nd
+
i
]
//
ws
[
i
]
+
si
[
i
])
out_shp
.
append
(
input
.
shape
[
-
nd
+
i
]
//
ws
[
i
]
+
si
[
i
])
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
for
l
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
l
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
r
in
n
umpy
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
for
r
in
n
p
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
patch
=
input
[
l
][
tuple
(
slice
(
r
[
i
]
*
ws
[
i
],
(
r
[
i
]
+
1
)
*
ws
[
i
])
patch
=
input
[
l
][
tuple
(
slice
(
r
[
i
]
*
ws
[
i
],
(
r
[
i
]
+
1
)
*
ws
[
i
])
for
i
in
range
(
nd
))]
for
i
in
range
(
nd
))]
output_val
[
l
][
r
]
=
func
(
patch
)
output_val
[
l
][
r
]
=
func
(
patch
)
...
@@ -104,7 +104,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -104,7 +104,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
assert
ws
[
1
]
>
pad_w
assert
ws
[
1
]
>
pad_w
def
pad_img
(
x
):
def
pad_img
(
x
):
y
=
n
umpy
.
zeros
(
y
=
n
p
.
zeros
(
(
x
.
shape
[
0
],
x
.
shape
[
1
],
(
x
.
shape
[
0
],
x
.
shape
[
1
],
x
.
shape
[
2
]
+
pad_h
*
2
,
x
.
shape
[
3
]
+
pad_w
*
2
),
x
.
shape
[
2
]
+
pad_h
*
2
,
x
.
shape
[
3
]
+
pad_w
*
2
),
dtype
=
x
.
dtype
)
dtype
=
x
.
dtype
)
...
@@ -120,16 +120,16 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -120,16 +120,16 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
.
append
(
out_c
)
out_shp
.
append
(
out_c
)
ws0
,
ws1
=
ws
ws0
,
ws1
=
ws
stride0
,
stride1
=
stride
stride0
,
stride1
=
stride
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
y
=
pad_img
(
x
)
y
=
pad_img
(
x
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
inc_pad
=
mode
==
'average_inc_pad'
inc_pad
=
mode
==
'average_inc_pad'
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
2
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii_stride
=
i
*
stride
[
0
]
ii_stride
=
i
*
stride
[
0
]
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
...
@@ -160,7 +160,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -160,7 +160,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
pad_img
(
x
):
def
pad_img
(
x
):
# initialize padded input
# initialize padded input
y
=
n
umpy
.
zeros
(
y
=
n
p
.
zeros
(
x
.
shape
[
0
:
-
nd
]
+
x
.
shape
[
0
:
-
nd
]
+
tuple
(
x
.
shape
[
-
nd
+
i
]
+
pad
[
i
]
*
2
for
i
in
range
(
nd
)),
tuple
(
x
.
shape
[
-
nd
+
i
]
+
pad
[
i
]
*
2
for
i
in
range
(
nd
)),
dtype
=
x
.
dtype
)
dtype
=
x
.
dtype
)
...
@@ -177,17 +177,17 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -177,17 +177,17 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
padded_size
=
input
.
shape
[
-
nd
+
i
]
+
2
*
pad
[
i
]
padded_size
=
input
.
shape
[
-
nd
+
i
]
+
2
*
pad
[
i
]
pad_img_shp
.
append
(
padded_size
)
pad_img_shp
.
append
(
padded_size
)
out_shp
.
append
((
padded_size
-
ws
[
i
])
//
stride
[
i
]
+
1
)
out_shp
.
append
((
padded_size
-
ws
[
i
])
//
stride
[
i
]
+
1
)
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
padded_input
=
pad_img
(
input
)
padded_input
=
pad_img
(
input
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
inc_pad
=
mode
==
'average_inc_pad'
inc_pad
=
mode
==
'average_inc_pad'
for
l
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
l
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
r
in
n
umpy
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
for
r
in
n
p
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
region
=
[]
region
=
[]
for
i
in
range
(
nd
):
for
i
in
range
(
nd
):
r_stride
=
r
[
i
]
*
stride
[
i
]
r_stride
=
r
[
i
]
*
stride
[
i
]
...
@@ -245,14 +245,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -245,14 +245,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
.
append
(
out_r
)
out_shp
.
append
(
out_r
)
out_shp
.
append
(
out_c
)
out_shp
.
append
(
out_c
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
for
k
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
k
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii_stride
=
i
*
stride
[
0
]
ii_stride
=
i
*
stride
[
0
]
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
...
@@ -289,15 +289,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -289,15 +289,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out
+=
1
out
+=
1
out_shp
.
append
(
out
)
out_shp
.
append
(
out
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
for
l
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
l
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
r
in
n
umpy
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
for
r
in
n
p
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
region
=
[]
region
=
[]
for
i
in
range
(
nd
):
for
i
in
range
(
nd
):
r_stride
=
r
[
i
]
*
stride
[
i
]
r_stride
=
r
[
i
]
*
stride
[
i
]
...
@@ -308,7 +308,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -308,7 +308,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
return
output_val
return
output_val
def
test_DownsampleFactorMax
(
self
):
def
test_DownsampleFactorMax
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, input size
# maxpool, input size
examples
=
(
examples
=
(
((
2
,),
(
16
,)),
((
2
,),
(
16
,)),
...
@@ -361,13 +361,13 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -361,13 +361,13 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
output_shape
=
Pool
.
out_shape
(
imval
.
shape
,
maxpoolshp
,
output_shape
=
Pool
.
out_shape
(
imval
.
shape
,
maxpoolshp
,
ndim
=
len
(
maxpoolshp
),
ndim
=
len
(
maxpoolshp
),
ignore_border
=
ignore_border
)
ignore_border
=
ignore_border
)
utt
.
assert_allclose
(
n
umpy
.
asarray
(
output_shape
),
numpy_output_val
.
shape
)
utt
.
assert_allclose
(
n
p
.
asarray
(
output_shape
),
numpy_output_val
.
shape
)
f
=
function
([],
maxpool_op
)
f
=
function
([],
maxpool_op
)
output_val
=
f
()
output_val
=
f
()
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxStride
(
self
):
def
test_DownsampleFactorMaxStride
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, ignore_border, input, output sizes
# maxpool, stride, ignore_border, input, output sizes
examples
=
(
examples
=
(
((
1
,
1
),
(
1
,
1
),
True
,
(
4
,
10
,
16
,
16
),
(
4
,
10
,
16
,
16
)),
((
1
,
1
),
(
1
,
1
),
True
,
(
4
,
10
,
16
,
16
),
(
4
,
10
,
16
,
16
)),
...
@@ -426,7 +426,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -426,7 +426,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxStrideExtra
(
self
):
def
test_DownsampleFactorMaxStrideExtra
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
5
,
3
),
(
5
,
3
),
(
5
,
3
),
(
5
,
5
),
(
3
,
2
),
(
7
,
7
),
(
9
,
9
))
maxpoolshps
=
((
5
,
3
),
(
5
,
3
),
(
5
,
3
),
(
5
,
5
),
(
3
,
2
),
(
7
,
7
),
(
9
,
9
))
stridesizes
=
((
3
,
2
),
(
7
,
5
),
(
10
,
6
),
(
1
,
1
),
stridesizes
=
((
3
,
2
),
(
7
,
5
),
(
10
,
6
),
(
1
,
1
),
(
2
,
3
),
(
10
,
10
),
(
1
,
1
))
(
2
,
3
),
(
10
,
10
),
(
1
,
1
))
...
@@ -438,7 +438,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -438,7 +438,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
(
4
,
10
,
4
,
2
),
(
4
,
10
,
1
,
0
),
(
4
,
10
,
1
,
1
),
(
4
,
10
,
4
,
2
),
(
4
,
10
,
1
,
0
),
(
4
,
10
,
1
,
1
),
(
4
,
10
,
0
,
0
),
(
4
,
10
,
1
,
1
))
(
4
,
10
,
0
,
0
),
(
4
,
10
,
1
,
1
))
images
=
tensor
.
dtensor4
()
images
=
tensor
.
dtensor4
()
for
indx
in
n
umpy
.
arange
(
len
(
maxpoolshps
)):
for
indx
in
n
p
.
arange
(
len
(
maxpoolshps
)):
imvsize
=
imvsizs
[
indx
]
imvsize
=
imvsizs
[
indx
]
imval
=
rng
.
rand
(
4
,
10
,
imvsize
[
0
],
imvsize
[
1
])
imval
=
rng
.
rand
(
4
,
10
,
imvsize
[
0
],
imvsize
[
1
])
stride
=
stridesizes
[
indx
]
stride
=
stridesizes
[
indx
]
...
@@ -468,7 +468,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -468,7 +468,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
test_DownsampleFactorMaxPaddingStride
(
self
):
def
test_DownsampleFactorMaxPaddingStride
(
self
):
ignore_border
=
True
# padding does not support ignore_border=False
ignore_border
=
True
# padding does not support ignore_border=False
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
5
,)),
((
3
,),
(
2
,),
(
2
,),
(
5
,)),
...
@@ -503,7 +503,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -503,7 +503,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxPaddingStride_grad
(
self
):
def
test_DownsampleFactorMaxPaddingStride_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
10
,),
(
5
,),
(
3
,),
(
2
,)),
((
10
,),
(
5
,),
(
3
,),
(
2
,)),
...
@@ -530,7 +530,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -530,7 +530,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_DownsampleFactorMax_grad
(
self
):
def
test_DownsampleFactorMax_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, input sizes
# maxpool, input sizes
examples
=
(
examples
=
(
((
2
,),
(
3
,)),
((
2
,),
(
3
,)),
...
@@ -599,7 +599,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -599,7 +599,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
testcase_func_name
=
utt
.
custom_name_func
)
testcase_func_name
=
utt
.
custom_name_func
)
def
test_DownsampleFactorMax_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
def
test_DownsampleFactorMax_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
# checks the gradient for the case that stride is used
# checks the gradient for the case that stride is used
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
(
maxpoolshp
,
stridesize
,
inputsize
)
=
example
(
maxpoolshp
,
stridesize
,
inputsize
)
=
example
imval
=
rng
.
rand
(
*
inputsize
)
imval
=
rng
.
rand
(
*
inputsize
)
...
@@ -611,7 +611,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -611,7 +611,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_DownsampleFactorMaxGrad_grad
(
self
):
def
test_DownsampleFactorMaxGrad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, input sizes
# maxpool, input sizes
examples
=
(
examples
=
(
((
2
,),
(
2
,)),
((
2
,),
(
2
,)),
...
@@ -649,7 +649,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -649,7 +649,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
def
test_AveragePoolGrad_grad
(
self
):
def
test_AveragePoolGrad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# avgpool, input sizes
# avgpool, input sizes
examples
=
(
examples
=
(
((
2
,),
(
2
,)),
((
2
,),
(
2
,)),
...
@@ -691,7 +691,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -691,7 +691,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
test_DownsampleFactorMaxGrad_grad_stride
(
self
,
example
,
ignore_border
):
def
test_DownsampleFactorMaxGrad_grad_stride
(
self
,
example
,
ignore_border
):
# checks the gradient of the gradient for
# checks the gradient of the gradient for
# the case that stride is used
# the case that stride is used
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
(
maxpoolshp
,
stride
,
inputsize
)
=
example
(
maxpoolshp
,
stride
,
inputsize
)
=
example
imval
=
rng
.
rand
(
*
inputsize
)
imval
=
rng
.
rand
(
*
inputsize
)
grad_shape
=
Pool
.
out_shape
(
grad_shape
=
Pool
.
out_shape
(
...
@@ -699,7 +699,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -699,7 +699,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
ignore_border
=
ignore_border
,
stride
=
stride
)
ignore_border
=
ignore_border
,
stride
=
stride
)
# skip the grad verification when the output is empty
# skip the grad verification when the output is empty
if
n
umpy
.
prod
(
grad_shape
)
!=
0
:
if
n
p
.
prod
(
grad_shape
)
!=
0
:
grad_val
=
rng
.
rand
(
*
grad_shape
)
grad_val
=
rng
.
rand
(
*
grad_shape
)
def
mp
(
input
,
grad
):
def
mp
(
input
,
grad
):
...
@@ -722,7 +722,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -722,7 +722,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
test_AveragePoolGrad_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
def
test_AveragePoolGrad_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
# checks the gradient of the gradient for
# checks the gradient of the gradient for
# the case that stride is used
# the case that stride is used
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
(
avgpoolshp
,
stride
,
inputsize
)
=
example
(
avgpoolshp
,
stride
,
inputsize
)
=
example
imval
=
rng
.
rand
(
*
inputsize
)
imval
=
rng
.
rand
(
*
inputsize
)
grad_shape
=
Pool
.
out_shape
(
grad_shape
=
Pool
.
out_shape
(
...
@@ -731,7 +731,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -731,7 +731,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
ignore_border
=
ignore_border
,
stride
=
stride
)
ignore_border
=
ignore_border
,
stride
=
stride
)
# skip the grad verification when the output is empty
# skip the grad verification when the output is empty
if
n
umpy
.
prod
(
grad_shape
)
!=
0
:
if
n
p
.
prod
(
grad_shape
)
!=
0
:
grad_val
=
rng
.
rand
(
*
grad_shape
)
grad_val
=
rng
.
rand
(
*
grad_shape
)
def
mp
(
input
,
grad
):
def
mp
(
input
,
grad
):
...
@@ -744,7 +744,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -744,7 +744,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
def
test_DownsampleFactorMaxPaddingStride_grad_grad
(
self
):
def
test_DownsampleFactorMaxPaddingStride_grad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
...
@@ -781,7 +781,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -781,7 +781,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
def
test_AveragePoolPaddingStride_grad_grad
(
self
):
def
test_AveragePoolPaddingStride_grad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# avgpool, stride, pad, input sizes
# avgpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
...
@@ -831,10 +831,10 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -831,10 +831,10 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
# The value has been manually computed from the theoretical gradient,
# The value has been manually computed from the theoretical gradient,
# and confirmed by the implementation.
# and confirmed by the implementation.
assert
n
umpy
.
allclose
(
fn_hess
([
1
,
2
]),
[[
0.
,
0.
],
[
0.
,
982.7667
]])
assert
n
p
.
allclose
(
fn_hess
([
1
,
2
]),
[[
0.
,
0.
],
[
0.
,
982.7667
]])
def
test_DownsampleFactorMaxGradGrad_grad
(
self
):
def
test_DownsampleFactorMaxGradGrad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
...
@@ -864,7 +864,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -864,7 +864,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval1
,
imval2
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval1
,
imval2
],
rng
=
rng
)
def
test_max_pool_2d_2D
(
self
):
def
test_max_pool_2d_2D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),
(
3
,
2
))
maxpoolshps
=
((
1
,
1
),
(
3
,
2
))
imval
=
rng
.
rand
(
4
,
5
)
imval
=
rng
.
rand
(
4
,
5
)
images
=
tensor
.
dmatrix
()
images
=
tensor
.
dmatrix
()
...
@@ -890,7 +890,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -890,7 +890,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool_3d_3D
(
self
):
def
test_max_pool_3d_3D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
imval
=
rng
.
rand
(
4
,
5
,
6
)
imval
=
rng
.
rand
(
4
,
5
,
6
)
images
=
tensor
.
dtensor3
()
images
=
tensor
.
dtensor3
()
...
@@ -916,7 +916,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -916,7 +916,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool_3d_3D_deprecated_interface
(
self
):
def
test_max_pool_3d_3D_deprecated_interface
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
imval
=
rng
.
rand
(
4
,
5
,
6
)
imval
=
rng
.
rand
(
4
,
5
,
6
)
images
=
tensor
.
dtensor3
()
images
=
tensor
.
dtensor3
()
...
@@ -945,12 +945,12 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -945,12 +945,12 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
mode
=
mode
)
mode
=
mode
)
def
test_max_pool_2d_2D_same_size
(
self
):
def
test_max_pool_2d_2D_same_size
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
test_input_array
=
n
umpy
.
array
([[[
test_input_array
=
n
p
.
array
([[[
[
1.
,
2.
,
3.
,
4.
],
[
1.
,
2.
,
3.
,
4.
],
[
5.
,
6.
,
7.
,
8.
]
[
5.
,
6.
,
7.
,
8.
]
]]])
.
astype
(
theano
.
config
.
floatX
)
]]])
.
astype
(
theano
.
config
.
floatX
)
test_answer_array
=
n
umpy
.
array
([[[
test_answer_array
=
n
p
.
array
([[[
[
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
0.
],
[
0.
,
6.
,
0.
,
8.
]
[
0.
,
6.
,
0.
,
8.
]
]]])
.
astype
(
theano
.
config
.
floatX
)
]]])
.
astype
(
theano
.
config
.
floatX
)
...
@@ -965,7 +965,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -965,7 +965,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
test_input_array
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
test_input_array
],
rng
=
rng
)
def
test_max_pool_2d_3D
(
self
):
def
test_max_pool_2d_3D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
[(
1
,
2
)]
maxpoolshps
=
[(
1
,
2
)]
imval
=
rng
.
rand
(
2
,
3
,
4
)
imval
=
rng
.
rand
(
2
,
3
,
4
)
images
=
tensor
.
dtensor3
()
images
=
tensor
.
dtensor3
()
...
@@ -992,7 +992,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -992,7 +992,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
# utt.verify_grad(mp, [imval], rng=rng)
# utt.verify_grad(mp, [imval], rng=rng)
def
test_max_pool_2d_6D
(
self
):
def
test_max_pool_2d_6D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
[(
3
,
2
)]
maxpoolshps
=
[(
3
,
2
)]
imval
=
rng
.
rand
(
2
,
1
,
1
,
1
,
3
,
4
)
imval
=
rng
.
rand
(
2
,
1
,
1
,
1
,
3
,
4
)
images
=
tensor
.
TensorType
(
'float64'
,
[
False
]
*
6
)()
images
=
tensor
.
TensorType
(
'float64'
,
[
False
]
*
6
)()
...
@@ -1022,7 +1022,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1022,7 +1022,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
image
=
tensor
.
dtensor4
()
image
=
tensor
.
dtensor4
()
maxout
=
tensor
.
dtensor4
()
maxout
=
tensor
.
dtensor4
()
gz
=
tensor
.
dtensor4
()
gz
=
tensor
.
dtensor4
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),
(
2
,
2
),
(
3
,
3
),
(
2
,
3
),
(
3
,
2
))
maxpoolshps
=
((
1
,
1
),
(
2
,
2
),
(
3
,
3
),
(
2
,
3
),
(
3
,
2
))
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
...
@@ -1078,7 +1078,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1078,7 +1078,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
window_size
=
tensor
.
ivector
()
window_size
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
# checking variable params vs fixed params
# checking variable params vs fixed params
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
...
@@ -1110,7 +1110,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1110,7 +1110,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
window_size
=
tensor
.
ivector
()
window_size
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
# checking variable params vs fixed params
# checking variable params vs fixed params
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
...
@@ -1172,8 +1172,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1172,8 +1172,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
dz_dx
=
theano
.
gradient
.
grad
(
z
.
sum
(),
x
)
dz_dx
=
theano
.
gradient
.
grad
(
z
.
sum
(),
x
)
new_fct
=
theano
.
function
([
x
],
[
y
,
z
,
dy_dx
,
dz_dx
])
new_fct
=
theano
.
function
([
x
],
[
y
,
z
,
dy_dx
,
dz_dx
])
# 3. Assert that the answer is the same
# 3. Assert that the answer is the same
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
.
astype
(
n
umpy
.
float32
)
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
.
astype
(
n
p
.
float32
)
old_out
=
old_fct
(
image_val
)
old_out
=
old_fct
(
image_val
)
new_out
=
new_fct
(
image_val
)
new_out
=
new_fct
(
image_val
)
for
o
,
n
in
zip
(
old_out
,
new_out
):
for
o
,
n
in
zip
(
old_out
,
new_out
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论