Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f2d9c06e
提交
f2d9c06e
authored
10月 27, 2009
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make test for DownsampleFactorMax op faster and cover more case.
上级
388dba8b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
53 行增加
和
63 行删除
+53
-63
test_downsample.py
theano/sandbox/test_downsample.py
+53
-63
没有找到文件。
theano/sandbox/test_downsample.py
浏览文件 @
f2d9c06e
...
...
@@ -5,69 +5,67 @@ from theano.tests import unittest_tools as utt
from
theano.sandbox.downsample
import
DownsampleFactorMax
,
max_pool
from
theano
import
function
,
Mode
class
TestDown
Sample
(
unittest
.
TestCase
):
class
TestDown
sampleFactorMax
(
unittest
.
TestCase
):
def
test_maxpool
(
self
):
# generate flatted images
maxpoolshps
=
((
1
,
1
),(
2
,
2
),(
3
,
3
),(
2
,
3
))
imval
=
N
.
random
.
rand
(
4
,
10
,
64
,
64
)
do_theano
=
True
images
=
T
.
dmatrix
()
dmatrix4
=
T
.
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
))
images4
=
dmatrix4
()
tctot
,
tpytot
,
ntot
,
gtot
=
[],
[],[],[]
tctot
,
tpytot
,
ntot
=
[],[],[]
for
maxpoolshp
in
maxpoolshps
:
print
'maxpoolshp'
,
maxpoolshp
for
border
in
[
True
,
False
]:
print
'maxpoolshp'
,
maxpoolshp
,
'border'
,
border
# numeric verification
my_output_val
=
N
.
zeros
((
imval
.
shape
[
0
],
imval
.
shape
[
1
],
imval
.
shape
[
2
]
/
maxpoolshp
[
0
],
imval
.
shape
[
3
]
/
maxpoolshp
[
1
]))
# numeric verification
xi
=
0
yi
=
0
if
not
border
:
if
imval
.
shape
[
-
2
]
%
maxpoolshp
[
0
]:
xi
+=
1
if
imval
.
shape
[
-
1
]
%
maxpoolshp
[
1
]:
yi
+=
1
my_output_val
=
N
.
zeros
((
imval
.
shape
[
0
],
imval
.
shape
[
1
],
imval
.
shape
[
2
]
/
maxpoolshp
[
0
]
+
xi
,
imval
.
shape
[
3
]
/
maxpoolshp
[
1
]
+
yi
))
time1
=
time
.
time
()
for
n
in
range
(
imval
.
shape
[
0
]):
for
k
in
range
(
imval
.
shape
[
1
]):
for
i
in
range
(
my_output_val
.
shape
[
2
]):
ii
=
i
*
maxpoolshp
[
0
]
for
j
in
range
(
my_output_val
.
shape
[
3
]):
jj
=
j
*
maxpoolshp
[
1
]
patch
=
imval
[
n
,
k
,
ii
:
ii
+
maxpoolshp
[
0
],
jj
:
jj
+
maxpoolshp
[
1
]]
my_output_val
[
n
,
k
,
i
,
j
]
=
N
.
max
(
patch
)
my_output_val
=
my_output_val
.
reshape
(
imval
.
shape
[
0
],
-
1
)
ntot
+=
[
time
.
time
()
-
time1
]
time1
=
time
.
time
()
for
n
in
range
(
imval
.
shape
[
0
]):
for
k
in
range
(
imval
.
shape
[
1
]):
for
i
in
range
(
my_output_val
.
shape
[
2
]):
ii
=
i
*
maxpoolshp
[
0
]
for
j
in
range
(
my_output_val
.
shape
[
3
]):
jj
=
j
*
maxpoolshp
[
1
]
patch
=
imval
[
n
,
k
,
ii
:
ii
+
maxpoolshp
[
0
],
jj
:
jj
+
maxpoolshp
[
1
]]
my_output_val
[
n
,
k
,
i
,
j
]
=
N
.
max
(
patch
)
my_output_val
=
my_output_val
.
reshape
(
imval
.
shape
[
0
],
-
1
)
ntot
+=
[
time
.
time
()
-
time1
]
# symbolic stuff
if
do_theano
:
#### wrapper to DownsampleFactorMax op ####
output
,
outshp
=
max_pool
(
images
,
imval
.
shape
[
1
:],
maxpoolshp
)
# symbolic stuff
#### wrapper to DownsampleFactorMax op ####
output
,
outshp
=
max_pool
(
images
,
imval
.
shape
[
1
:],
maxpoolshp
,
border
)
assert
N
.
prod
(
my_output_val
.
shape
[
1
:])
==
N
.
prod
(
outshp
)
print
outshp
print
my_output_val
.
shape
assert
N
.
prod
(
my_output_val
.
shape
[
1
:])
==
N
.
prod
(
outshp
)
f
=
function
([
images
,],[
output
,])
imval2
=
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
)
output_val
=
f
(
imval2
)
assert
N
.
all
(
output_val
==
my_output_val
)
else
:
tctot
=-
1
output_val
=
my_output_val
.
copy
()
#DownsampleFactorMax op
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
True
)(
images4
)
f
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"py"
))
f2
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"c"
))
time1
=
time
.
time
()
output_val2
=
f
(
imval
)
tpytot
+=
[
time
.
time
()
-
time1
]
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val2
.
flatten
())
<
1e-5
)
.
all
()
time1
=
time
.
time
()
output_val2
=
f2
(
imval
)
tctot
+=
[
time
.
time
()
-
time1
]
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val2
.
flatten
())
<
1e-5
)
.
all
()
def
mp
(
input
):
output
,
outshp
=
max_pool
(
input
,
imval
.
shape
[
1
:],
maxpoolshp
)
return
output
#DownsampleFactorMax op
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
border
)(
images4
)
f
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"py"
))
f2
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"c"
))
f3
=
function
([
images4
],
maxpool_op
)
#for when we want to use the debug mode
time1
=
time
.
time
()
output_val
=
f
(
imval
)
tctot
+=
[
time
.
time
()
-
time1
]
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val
.
flatten
())
<
1e-5
)
.
all
()
time1
=
time
.
time
()
output_val
=
f2
(
imval
)
tpytot
+=
[
time
.
time
()
-
time1
]
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val
.
flatten
())
<
1e-5
)
.
all
()
output_val
=
f3
(
imval
)
print
'Numpy processing time:
%.3
fs'
%
sum
(
ntot
),
ntot
print
'c Theano(DownsampleFactorMax) processing time:
%.3
fs'
%
sum
(
tctot
),
tctot
...
...
@@ -76,29 +74,21 @@ class TestDownSample(unittest.TestCase):
print
'speed up c theano(DownsampleFactorMax) vs manual:
%.3
f'
%
d
.
mean
(),
d
d
=
N
.
asarray
(
ntot
)
/
tpytot
print
'speed up py theano(DownsampleFactorMax) vs manual:
%.3
f'
%
d
.
mean
(),
d
print
'verify_grad time
%.3
f'
%
sum
(
gtot
),
def
test_
maxpool
_grad
(
self
):
def
test_
DownsampleFactorMax
_grad
(
self
):
# generate flatted images
maxpoolshps
=
((
1
,
1
),(
2
,
2
),(
3
,
3
),(
2
,
3
))
imval
=
N
.
random
.
rand
(
3
,
7
,
10
,
10
)
*
10.0
#more variance means numeric gradient will be more accurate
maxpoolshps
=
((
1
,
1
),(
3
,
2
),(
2
,
3
))
imval
=
N
.
random
.
rand
(
2
,
3
,
3
,
4
)
*
10.0
#more variance means numeric gradient will be more accurate
do_theano
=
True
images
=
T
.
dmatrix
()
dmatrix4
=
T
.
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
))
images4
=
dmatrix4
()
for
maxpoolshp
in
maxpoolshps
:
print
'maxpoolshp'
,
maxpoolshp
def
mp
(
input
):
output
,
outshp
=
max_pool
(
input
,
imval
.
shape
[
1
:],
maxpoolshp
)
return
output
print
>>
sys
.
stderr
,
'max_pool verify_grad requires unusually large tolerance... is it correct?'
utt
.
verify_grad
(
mp
,
[
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
)],
tol
=
1e-2
)
for
border
in
[
True
,
False
]:
print
'maxpoolshp'
,
maxpoolshp
,
'border'
,
border
def
mp
(
input
):
return
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
border
)(
input
)
utt
.
verify_grad
(
mp
,
[
imval
])
if
__name__
==
'__main__'
:
t
=
TestSP
(
"test_convolution"
)
t
=
TestSP
(
"test_maxpool"
)
.
run
()
# t.test_convolution()
# t.test_multilayer_conv()
t
=
TestDownsampleFactorMax
(
"test_maxpool"
)
.
run
()
#t.test_maxpool()
from
theano.tests
import
main
# main("test_sp")
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论