Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c3dd554a
提交
c3dd554a
authored
7月 15, 2012
作者:
Eric Larsen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
correcting file layout
上级
a6a9f6fb
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
52 行删除
+46
-52
test_downsample.py
theano/tensor/signal/tests/test_downsample.py
+46
-52
没有找到文件。
theano/tensor/signal/tests/test_downsample.py
浏览文件 @
c3dd554a
import
unittest
,
sys
,
time
import
unittest
import
sys
import
time
import
numpy
import
numpy
import
theano.tensor
as
tensor
import
theano.tensor
as
tensor
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
...
@@ -13,118 +15,113 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -13,118 +15,113 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
numpy_max_pool_2d
(
input
,
ds
,
ignore_border
=
False
):
def
numpy_max_pool_2d
(
input
,
ds
,
ignore_border
=
False
):
'''Helper function, implementing max_pool_2d in pure numpy'''
'''Helper function, implementing max_pool_2d in pure numpy'''
if
len
(
input
.
shape
)
<
2
:
if
len
(
input
.
shape
)
<
2
:
raise
NotImplementedError
(
'input should have at least 2 dim, shape is
%
s'
\
raise
NotImplementedError
(
'input should have at least 2 dim,'
' shape is
%
s'
\
%
str
(
input
.
shape
))
%
str
(
input
.
shape
))
xi
=
0
xi
=
0
yi
=
0
yi
=
0
if
not
ignore_border
:
if
not
ignore_border
:
if
input
.
shape
[
-
2
]
%
ds
[
0
]:
if
input
.
shape
[
-
2
]
%
ds
[
0
]:
xi
+=
1
xi
+=
1
if
input
.
shape
[
-
1
]
%
ds
[
1
]:
if
input
.
shape
[
-
1
]
%
ds
[
1
]:
yi
+=
1
yi
+=
1
out_shp
=
list
(
input
.
shape
[:
-
2
])
out_shp
=
list
(
input
.
shape
[:
-
2
])
out_shp
.
append
(
input
.
shape
[
-
2
]
/
ds
[
0
]
+
xi
)
out_shp
.
append
(
input
.
shape
[
-
2
]
/
ds
[
0
]
+
xi
)
out_shp
.
append
(
input
.
shape
[
-
1
]
/
ds
[
1
]
+
yi
)
out_shp
.
append
(
input
.
shape
[
-
1
]
/
ds
[
1
]
+
yi
)
output_val
=
numpy
.
zeros
(
out_shp
)
output_val
=
numpy
.
zeros
(
out_shp
)
for
k
in
numpy
.
ndindex
(
input
.
shape
[:
-
2
]):
for
k
in
numpy
.
ndindex
(
input
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii
=
i
*
ds
[
0
]
ii
=
i
*
ds
[
0
]
for
j
in
range
(
output_val
.
shape
[
-
1
]):
for
j
in
range
(
output_val
.
shape
[
-
1
]):
jj
=
j
*
ds
[
1
]
jj
=
j
*
ds
[
1
]
patch
=
input
[
k
][
ii
:
ii
+
ds
[
0
],
jj
:
jj
+
ds
[
1
]]
patch
=
input
[
k
][
ii
:
ii
+
ds
[
0
],
jj
:
jj
+
ds
[
1
]]
output_val
[
k
][
i
,
j
]
=
numpy
.
max
(
patch
)
output_val
[
k
][
i
,
j
]
=
numpy
.
max
(
patch
)
return
output_val
return
output_val
def
test_DownsampleFactorMax
(
self
):
def
test_DownsampleFactorMax
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
# generate random images
# generate random images
maxpoolshps
=
((
1
,
1
),(
2
,
2
),(
3
,
3
),(
2
,
3
))
maxpoolshps
=
((
1
,
1
),
(
2
,
2
),
(
3
,
3
),
(
2
,
3
))
imval
=
rng
.
rand
(
4
,
10
,
64
,
64
)
imval
=
rng
.
rand
(
4
,
10
,
64
,
64
)
images
=
tensor
.
dtensor4
()
images
=
tensor
.
dtensor4
()
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
#print 'maxpoolshp =', maxpoolshp
#print 'maxpoolshp =', maxpoolshp
#print 'ignore_border =', ignore_border
#print 'ignore_border =', ignore_border
#
#
Pure Numpy computation
# Pure Numpy computation
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
f
=
function
([
images
,
],[
output
,
])
f
=
function
([
images
,
],
[
output
,
])
output_val
=
f
(
imval
)
output_val
=
f
(
imval
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
#DownsampleFactorMax op
#DownsampleFactorMax op
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
ignore_border
)(
images
)
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
ignore_border
)(
images
)
f
=
function
([
images
],
maxpool_op
)
f
=
function
([
images
],
maxpool_op
)
output_val
=
f
(
imval
)
output_val
=
f
(
imval
)
assert
(
numpy
.
abs
(
output_val
-
numpy_output_val
)
<
1e-5
)
.
all
()
assert
(
numpy
.
abs
(
output_val
-
numpy_output_val
)
<
1e-5
)
.
all
()
def
test_DownsampleFactorMax_grad
(
self
):
def
test_DownsampleFactorMax_grad
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),(
3
,
2
),(
2
,
3
))
maxpoolshps
=
((
1
,
1
),
(
3
,
2
),
(
2
,
3
))
imval
=
rng
.
rand
(
2
,
3
,
3
,
4
)
*
10.0
#more variance means numeric gradient will be more accurate
imval
=
rng
.
rand
(
2
,
3
,
3
,
4
)
*
10.0
#more variance means numeric gradient will be more accurate
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
#print 'maxpoolshp =', maxpoolshp
#print 'maxpoolshp =', maxpoolshp
#print 'ignore_border =', ignore_border
#print 'ignore_border =', ignore_border
def
mp
(
input
):
def
mp
(
input
):
return
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
ignore_border
)(
input
)
return
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
ignore_border
)(
input
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool_2d_2D
(
self
):
def
test_max_pool_2d_2D
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
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
()
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
#print 'maxpoolshp =', maxpoolshp
#print 'maxpoolshp =', maxpoolshp
#print 'ignore_border =', ignore_border
#print 'ignore_border =', ignore_border
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
output_val
=
function
([
images
],
output
)(
imval
)
output_val
=
function
([
images
],
output
)(
imval
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
def
mp
(
input
):
def
mp
(
input
):
return
max_pool_2d
(
input
,
maxpoolshp
,
ignore_border
)
return
max_pool_2d
(
input
,
maxpoolshp
,
ignore_border
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool_2d_3D
(
self
):
def
test_max_pool_2d_3D
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
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
()
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
#print 'maxpoolshp =', maxpoolshp
#print 'maxpoolshp =', maxpoolshp
#print 'ignore_border =', ignore_border
#print 'ignore_border =', ignore_border
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
output_val
=
function
([
images
],
output
)(
imval
)
output_val
=
function
([
images
],
output
)(
imval
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
c
=
tensor
.
sum
(
output
)
c
=
tensor
.
sum
(
output
)
c_val
=
function
([
images
],
c
)(
imval
)
c_val
=
function
([
images
],
c
)(
imval
)
g
=
tensor
.
grad
(
c
,
images
)
g
=
tensor
.
grad
(
c
,
images
)
g_val
=
function
([
images
],
g_val
=
function
([
images
],
[
g
.
shape
,
[
g
.
shape
,
tensor
.
min
(
g
,
axis
=
(
0
,
1
,
2
)),
tensor
.
min
(
g
,
axis
=
(
0
,
1
,
2
)),
tensor
.
max
(
g
,
axis
=
(
0
,
1
,
2
))]
tensor
.
max
(
g
,
axis
=
(
0
,
1
,
2
))]
)(
imval
)
)(
imval
)
#removed as already tested in test_max_pool_2d_2D
#removed as already tested in test_max_pool_2d_2D
...
@@ -133,20 +130,18 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -133,20 +130,18 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
# return max_pool_2d(input, maxpoolshp, ignore_border)
# return max_pool_2d(input, maxpoolshp, ignore_border)
# 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
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
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
)()
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
#print 'maxpoolshp =', maxpoolshp
#print 'maxpoolshp =', maxpoolshp
#print 'ignore_border =', ignore_border
#print 'ignore_border =', ignore_border
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
numpy_output_val
=
self
.
numpy_max_pool_2d
(
imval
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
output
=
max_pool_2d
(
images
,
maxpoolshp
,
ignore_border
)
output_val
=
function
([
images
],
output
)(
imval
)
output_val
=
function
([
images
],
output
)(
imval
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
...
@@ -158,7 +153,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -158,7 +153,6 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
# utt.verify_grad(mp, [imval], rng=rng)
# utt.verify_grad(mp, [imval], rng=rng)
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
image
=
tensor
.
dtensor4
()
image
=
tensor
.
dtensor4
()
maxout
=
tensor
.
dtensor4
()
maxout
=
tensor
.
dtensor4
()
gz
=
tensor
.
dtensor4
()
gz
=
tensor
.
dtensor4
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论