Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d5944c96
提交
d5944c96
authored
7月 18, 2016
作者:
nke001
提交者:
abergeron
7月 18, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ccw4057 (#4592)
* flake 8 changes * flake 8 changes * add missing imports * code cleanup * code clean up * flake 8 changes * code cleanup * code cleanup * code cleanup * code cleanup * add missing import
上级
74b4c807
隐藏空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
621 行增加
和
596 行删除
+621
-596
__init__.py
theano/tensor/nnet/tests/__init__.py
+1
-0
speed_test_conv.py
theano/tensor/nnet/tests/speed_test_conv.py
+47
-53
test_conv.py
theano/tensor/nnet/tests/test_conv.py
+171
-119
test_conv3d.py
theano/tensor/nnet/tests/test_conv3d.py
+89
-82
test_conv3d2d.py
theano/tensor/nnet/tests/test_conv3d2d.py
+15
-15
test_neighbours.py
theano/tensor/nnet/tests/test_neighbours.py
+110
-137
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+150
-143
test_sigm.py
theano/tensor/nnet/tests/test_sigm.py
+38
-39
test_flake8.py
theano/tests/test_flake8.py
+0
-8
没有找到文件。
theano/tensor/nnet/tests/__init__.py
浏览文件 @
d5944c96
from
__future__
import
absolute_import
,
print_function
,
division
theano/tensor/nnet/tests/speed_test_conv.py
浏览文件 @
d5944c96
from
__future__
import
absolute_import
,
print_function
,
division
import
sys
,
time
,
unittest
import
time
import
numpy
import
numpy
as
N
from
six.moves
import
xrange
from
theano.tests
import
unittest_tools
as
utt
from
theano
import
function
,
Mode
import
theano.tensor
as
T
...
...
@@ -38,7 +36,6 @@ def flip(kern, kshp):
flip
[
k
,
m
,
i
,
j
]
=
next
(
it
)
else
:
raise
NotImplementedError
()
return
flip
global_rng
=
N
.
random
.
RandomState
(
3423489
)
...
...
@@ -46,7 +43,8 @@ global_rng = N.random.RandomState(3423489)
dmatrix4
=
T
.
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
))
def
exec_multilayer_conv_nnet_old
(
conv_mode
,
ss
,
bsize
,
imshp
,
kshps
,
nkerns
,
def
exec_multilayer_conv_nnet_old
(
conv_mode
,
ss
,
bsize
,
imshp
,
kshps
,
nkerns
,
unroll_batch
=
0
,
unroll_kern
=
0
,
img
=
T
.
dmatrix
(),
validate
=
True
,
conv_op_py
=
False
,
do_print
=
True
,
repeat
=
1
,
unroll_patch
=
False
,
unroll_patch_size
=
False
,
verbose
=
0
):
...
...
@@ -68,7 +66,6 @@ def exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp, kshps, nkerns,
xrange
(
len
(
nkerns
))):
if
do_print
:
print
(
'************* layer
%
i ***************'
%
n_layer
)
print
(
conv_mode
,
ss
,
n_layer
,
kshp
,
nkern
)
# actual values
...
...
@@ -79,10 +76,11 @@ def exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp, kshps, nkerns,
# check first stage
padimg
=
imgval
if
conv_mode
==
'full'
:
padimg_shp
=
N
.
array
(
imshp
[
1
:])
+
2
*
(
N
.
array
(
kshp
)
-
N
.
array
([
1
,
1
]))
padimg_shp
=
N
.
array
(
imshp
[
1
:])
+
2
*
(
N
.
array
(
kshp
)
-
N
.
array
([
1
,
1
]))
padimg
=
N
.
zeros
(
N
.
r_
[
bsize
,
imshp
[
0
],
padimg_shp
])
padimg
[:,
:,
kshp
[
0
]
-
1
:
-
kshp
[
0
]
+
1
,
kshp
[
1
]
-
1
:
-
kshp
[
1
]
+
1
]
=
imgval
padimg
[
:,
:,
kshp
[
0
]
-
1
:
-
kshp
[
0
]
+
1
,
kshp
[
1
]
-
1
:
-
kshp
[
1
]
+
1
]
=
imgval
outshp
=
N
.
hstack
((
nkern
,
ConvOp
.
getOutputShape
(
imshp
[
1
:],
kshp
,
ss
,
conv_mode
)))
...
...
@@ -91,13 +89,13 @@ def exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp, kshps, nkerns,
if
validate
:
# causes an atexit problem
from
scipy.signal.sigtools
import
_convolve2d
from
scipy.signal.signaltools
import
_valfrommode
,
_bvalfromboundary
from
scipy.signal.signaltools
import
_valfrommode
,
_bvalfromboundary
val
=
_valfrommode
(
conv_mode
)
bval
=
_bvalfromboundary
(
'fill'
)
for
b
in
xrange
(
bsize
):
# loop over batches
for
n
in
xrange
(
nkern
):
# loop over filters
for
i
in
xrange
(
imshp
[
0
]):
# loop over input feature maps
outval
[
b
,
n
,
...
]
+=
_convolve2d
(
\
outval
[
b
,
n
,
...
]
+=
_convolve2d
(
imgval
[
b
,
i
,
...
],
w_flip
[
n
,
i
,
...
],
1
,
val
,
bval
,
0
)[
0
::
ss
[
0
],
0
::
ss
[
1
]]
ntot
+=
time
.
time
()
-
time1
...
...
@@ -108,8 +106,8 @@ def exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp, kshps, nkerns,
else
:
conv_op
=
ConvOp
(
imshp
,
kshp
,
nkern
,
bsize
,
ss
[
0
],
ss
[
1
],
conv_mode
,
unroll_batch
=
unroll_batch
,
unroll_kern
=
unroll_kern
,
unroll_patch
=
unroll_patch
,
verbose
=
verbose
)(
inputs4
,
kerns4
)
l1shp
=
N
.
hstack
((
nkern
,
ConvOp
.
getOutputShape
(
imshp
[
1
:],
kshp
,
ss
,
conv_mode
)))
#
l1shp = N.hstack((nkern,
#
ConvOp.getOutputShape(imshp[1:], kshp, ss, conv_mode)))
propup2
=
function
([
inputs4
,
kerns4
],
conv_op
)
propup3
=
function
([
inputs4
,
kerns4
],
conv_op
,
mode
=
Mode
(
linker
=
"py"
))
...
...
@@ -125,7 +123,7 @@ def exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp, kshps, nkerns,
hidval3_
=
propup3
(
imgval
,
w_flip
)
hidval3
=
hidval3_
# [:,:,0::ss[0],0::ss[1]]
tpytot
+=
time
.
time
()
-
time1
assert
(
N
.
abs
(
hidval2
-
hidval3
)
<
1e-5
)
.
all
()
assert
(
N
.
abs
(
hidval2
-
hidval3
)
<
1e-5
)
.
all
()
else
:
tpytot
+=
0
...
...
@@ -142,10 +140,11 @@ def exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp, kshps, nkerns,
return
tctot
,
tpytot
,
ntot
def
exec_multilayer_conv_nnet
(
conv_mode
,
ss
,
bsize
,
imshp
,
kshps
,
nkerns
,
unroll_batch
=
0
,
unroll_kern
=
0
,
img
=
T
.
dmatrix
(),
do_print
=
True
,
repeat
=
1
,
unroll_patch
=
False
,
unroll_patch_size
=
False
,
verbose
=
0
):
def
exec_multilayer_conv_nnet
(
conv_mode
,
ss
,
bsize
,
imshp
,
kshps
,
nkerns
,
unroll_batch
=
0
,
unroll_kern
=
0
,
img
=
T
.
dmatrix
(),
do_print
=
True
,
repeat
=
1
,
unroll_patch
=
False
,
unroll_patch_size
=
False
,
verbose
=
0
):
# build actual input images
imgval
=
global_rng
.
rand
(
bsize
,
imshp
[
0
],
imshp
[
1
],
imshp
[
2
])
...
...
@@ -163,7 +162,6 @@ def exec_multilayer_conv_nnet(conv_mode, ss, bsize, imshp, kshps, nkerns,
for
kshp
,
kern
,
nkern
,
n_layer
in
zip
(
kshps
,
kerns
,
nkerns
,
xrange
(
len
(
nkerns
))):
if
do_print
:
print
(
'************* layer
%
i ***************'
%
n_layer
)
print
(
conv_mode
,
ss
,
n_layer
,
kshp
,
nkern
)
# actual values
...
...
@@ -173,7 +171,7 @@ def exec_multilayer_conv_nnet(conv_mode, ss, bsize, imshp, kshps, nkerns,
outshp
=
N
.
hstack
((
nkern
,
ConvOp
.
getOutputShape
(
imshp
[
1
:],
kshp
,
ss
,
conv_mode
)))
time1
=
time
.
time
()
outval
=
N
.
zeros
(
N
.
r_
[
bsize
,
outshp
])
#
outval = N.zeros(N.r_[bsize, outshp])
# ConvOp
if
unroll_patch
and
not
unroll_patch_size
:
...
...
@@ -182,18 +180,17 @@ def exec_multilayer_conv_nnet(conv_mode, ss, bsize, imshp, kshps, nkerns,
else
:
conv_op
=
ConvOp
(
imshp
,
kshp
,
nkern
,
bsize
,
ss
[
0
],
ss
[
1
],
conv_mode
,
unroll_batch
=
unroll_batch
,
unroll_kern
=
unroll_kern
,
unroll_patch
=
unroll_patch
,
verbose
=
verbose
)(
inputs4
,
kerns4
)
l1shp
=
N
.
hstack
((
nkern
,
ConvOp
.
getOutputShape
(
imshp
[
1
:],
kshp
,
ss
,
conv_mode
)))
#
l1shp = N.hstack((nkern,
#
ConvOp.getOutputShape(imshp[1:], kshp, ss, conv_mode)))
propup2
=
function
([
inputs4
,
kerns4
],
conv_op
)
time1
=
time
.
time
()
for
i
in
xrange
(
repeat
):
hidval2_
=
propup2
(
imgval
,
w_flip
)
hidval2
=
hidval2_
# [:,:,0::ss[0],0::ss[1]]
propup2
(
imgval
,
w_flip
)
tctot
+=
time
.
time
()
-
time1
imshp
=
tuple
(
outshp
)
imgval
=
outval
.
reshape
(
bsize
,
outshp
[
0
],
outshp
[
1
],
outshp
[
2
])
#
imgval = outval.reshape(bsize, outshp[0], outshp[1], outshp[2])
return
tctot
,
tpytot
,
ntot
...
...
@@ -201,31 +198,28 @@ def exec_multilayer_conv_nnet(conv_mode, ss, bsize, imshp, kshps, nkerns,
def
speed_multilayer_conv
():
# calculate the speed up of different combination of unroll
# put the paramter to the same you will try.
validate
=
False
# we don't validate the result to have it much faster!
# validate = False # we don't validate the result to have it much faster!
repeat
=
3
verbose
=
1
unroll_batch
=
[
1
,
2
,
3
,
4
,
5
,
6
,
10
]
# 15, 30, 60 always much slower
unroll_kern
=
[
1
,
2
,
3
,
4
,
5
,
6
,
10
]
# 15, 30, 60 always much slower
#unroll_batch = [1,4,5]
#unroll_kern = [1,4,5]
#unroll_batch = [1,4]
#unroll_kern = [1,4]
unroll_patch
=
[
True
,
False
]
# unroll_batch = [1,4,5]
# unroll_kern = [1,4,5]
# unroll_batch = [1,4]
# unroll_kern = [1,4]
# unroll_patch = [True, False]
bsize
=
60
# batch size
imshp_start
=
(
1
,
48
,
48
)
# un square shape to test more corner case.
kshps
=
([
11
,
12
],)
# un square shape to test more corner case.
nkerns
=
[
60
]
# per output pixel
ssizes
=
[(
1
,
1
),
]
# (1,1)]#(2,2) bugged
convmodes
=
[
'valid'
,
'full'
]
do_convolve2
=
False
#
do_convolve2 = False
a
=
T
.
dmatrix
()
kerns
=
[
a
for
i
in
nkerns
]
assert
len
(
kshps
)
==
len
(
nkerns
)
==
len
(
kerns
)
timing
=
N
.
zeros
((
len
(
unroll_batch
),
len
(
unroll_kern
),
3
,
len
(
convmodes
)
*
len
(
ssizes
)))
timing
=
N
.
zeros
((
len
(
unroll_batch
),
len
(
unroll_kern
),
3
,
len
(
convmodes
)
*
len
(
ssizes
)))
t_b_k
=
[]
# calculate the timing with unrolling
...
...
@@ -235,12 +229,12 @@ def speed_multilayer_conv():
t_
=
[]
for
unroll_b
,
n_b
in
zip
(
unroll_batch
,
xrange
(
len
(
unroll_batch
))):
for
unroll_k
,
n_k
in
zip
(
unroll_kern
,
xrange
(
len
(
unroll_kern
))):
t_b_k
.
append
(
str
(
unroll_b
)
+
"/"
+
str
(
unroll_k
))
t_b_k
.
append
(
str
(
unroll_b
)
+
"/"
+
str
(
unroll_k
))
if
not
t_
:
tctot
,
tpytot
,
ntot
=
[],
[],
[]
for
conv_mode
,
n_mode
in
zip
(
convmodes
,
xrange
(
len
(
convmodes
))):
for
ss
,
n_ss
in
zip
(
ssizes
,
xrange
(
len
(
ssizes
))):
#
tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=unroll_b, unroll_kern=unroll_k, validate=validate, verbose=verbose,do_print=False)
#
tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=unroll_b, unroll_kern=unroll_k, validate=validate, verbose=verbose,do_print=False)
tctot_
,
tpytot_
,
ntot_
=
exec_multilayer_conv_nnet
(
conv_mode
,
ss
,
bsize
,
imshp_start
,
kshps
,
nkerns
,
unroll_batch
=
unroll_b
,
unroll_kern
=
unroll_k
,
verbose
=
verbose
,
do_print
=
False
,
repeat
=
repeat
)
tctot
+=
[
tctot_
]
tpytot
+=
[
tpytot_
]
...
...
@@ -264,13 +258,14 @@ def speed_multilayer_conv():
if
not
tctot_
:
for
conv_mode
,
n_mode
in
zip
(
convmodes
,
xrange
(
len
(
convmodes
))):
for
ss
,
n_ss
in
zip
(
ssizes
,
xrange
(
len
(
ssizes
))):
#
tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=0, unroll_kern=0, validate=validate, verbose=verbose,do_print=False)
#
tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=0, unroll_kern=0, validate=validate, verbose=verbose,do_print=False)
tctot_
,
tpytot_
,
ntot_
=
exec_multilayer_conv_nnet
(
conv_mode
,
ss
,
bsize
,
imshp_start
,
kshps
,
nkerns
,
unroll_batch
=
0
,
unroll_kern
=
0
,
verbose
=
verbose
,
do_print
=
False
,
repeat
=
repeat
)
tctot
+=
[
tctot_
]
tpytot
+=
[
tpytot_
]
ntot
+=
[
ntot_
]
else
:
tctot
=
N
.
asarray
(
tctot_
)
print
(
"old code timing
%.3
fs"
%
sum
(
tctot
),
tctot
)
else
:
tctot
=
N
.
asarray
(
tctot_
)
print
(
"old code timing
%.3
fs"
%
sum
(
tctot
),
tctot
)
best
=
N
.
asarray
(
best
)
worst
=
N
.
asarray
(
worst
)
print
(
"timing for unrolled version"
)
...
...
@@ -278,12 +273,12 @@ def speed_multilayer_conv():
for
n_b
in
xrange
(
len
(
unroll_batch
)):
for
n_k
in
xrange
(
len
(
unroll_kern
)):
print
((
unroll_batch
[
n_b
],
unroll_kern
[
n_k
])
+
tuple
(
t
[
n_b
,
n_k
]),
','
)
t_detail
=
t
#
t_detail = t
t
=
t
.
sum
(
axis
=
2
)
print
(
"max
%.3
fs"
%
t
.
max
(),
"max param(batch unloop size/kernel unloop size)"
,
t_b_k
[
t
.
argmax
()])
print
(
"min
%.3
fs"
%
t
.
min
(),
"min param(batch unloop size/kernel unloop size)"
,
t_b_k
[
t
.
argmin
()])
print
(
"speedup vs (1/1)
%.3
fx, vs old
%.3
fx"
%
(
t
.
max
()
/
t
.
min
(),
sum
(
tctot
)
/
t
.
min
()))
print
(
worst
/
best
,
tctot
/
best
)
print
(
"max
%.3
fs"
%
t
.
max
(),
"max param(batch unloop size/kernel unloop size)"
,
t_b_k
[
t
.
argmax
()])
print
(
"min
%.3
fs"
%
t
.
min
(),
"min param(batch unloop size/kernel unloop size)"
,
t_b_k
[
t
.
argmin
()])
print
(
"speedup vs (1/1)
%.3
fx, vs old
%.3
fx"
%
(
t
.
max
()
/
t
.
min
(),
sum
(
tctot
)
/
t
.
min
()))
print
(
worst
/
best
,
tctot
/
best
)
# calculate the timing of unroll_patch
print
(
'time unroll_patch'
)
...
...
@@ -291,22 +286,21 @@ def speed_multilayer_conv():
tctot_patch_size
=
[]
for
conv_mode
,
n_mode
in
zip
(
convmodes
,
xrange
(
len
(
convmodes
))):
for
ss
,
n_ss
in
zip
(
ssizes
,
xrange
(
len
(
ssizes
))):
#tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=0, unroll_kern=0, validate=validate,unroll_patch=True,verbose=verbose,do_print=False)
#
tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=0, unroll_kern=0, validate=validate,unroll_patch=True,verbose=verbose,do_print=False)
tctot_
,
tpytot_
,
ntot_
=
exec_multilayer_conv_nnet
(
conv_mode
,
ss
,
bsize
,
imshp_start
,
kshps
,
nkerns
,
unroll_batch
=
0
,
unroll_kern
=
0
,
unroll_patch
=
True
,
verbose
=
verbose
,
do_print
=
False
,
repeat
=
repeat
)
tctot_patch
+=
[
tctot_
]
#tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=0, unroll_kern=0, validate=validate,unroll_patch=True,verbose=verbose,do_print=False,unroll_patch_size=True)
#
tctot_, tpytot_, ntot_ = exec_multilayer_conv_nnet_old(conv_mode, ss, bsize, imshp_start, kshps, nkerns, unroll_batch=0, unroll_kern=0, validate=validate,unroll_patch=True,verbose=verbose,do_print=False,unroll_patch_size=True)
tctot_
,
tpytot_
,
ntot_
=
exec_multilayer_conv_nnet
(
conv_mode
,
ss
,
bsize
,
imshp_start
,
kshps
,
nkerns
,
unroll_batch
=
0
,
unroll_kern
=
0
,
unroll_patch
=
True
,
verbose
=
verbose
,
do_print
=
False
,
unroll_patch_size
=
True
,
repeat
=
repeat
)
tctot_patch_size
+=
[
tctot_
]
t_patch
=
sum
(
tctot_patch
)
print
(
"unroll_patch without shape time"
,
tctot_patch
)
print
(
"speedup vs (1/1)
%.3
fx, vs old
%.3
fx"
%
(
t
.
max
()
/
t_patch
,
sum
(
tctot
)
/
t_patch
))
print
(
best
/
tctot_patch
,
worst
/
tctot_patch
)
print
(
"speedup vs (1/1)
%.3
fx, vs old
%.3
fx"
%
(
t
.
max
()
/
t_patch
,
sum
(
tctot
)
/
t_patch
))
print
(
best
/
tctot_patch
,
worst
/
tctot_patch
)
t_patch_size
=
sum
(
tctot_patch_size
)
print
(
"unroll_patch with shape time"
,
tctot_patch_size
)
print
(
"speedup vs (1/1)
%.3
fx, vs old
%.3
fx"
%
(
t
.
max
()
/
t_patch_size
,
sum
(
tctot
)
/
t_patch_size
))
print
(
best
/
tctot_patch_size
,
worst
/
tctot_patch_size
)
print
(
"speedup vs (1/1)
%.3
fx, vs old
%.3
fx"
%
(
t
.
max
()
/
t_patch_size
,
sum
(
tctot
)
/
t_patch_size
))
print
(
best
/
tctot_patch_size
,
worst
/
tctot_patch_size
)
return
if
__name__
==
'__main__'
:
...
...
theano/tensor/nnet/tests/test_conv.py
浏览文件 @
d5944c96
...
...
@@ -49,27 +49,28 @@ class TestConv2D(utt.InferShapeTester):
"""
if
N_image_shape
is
None
:
N_image_shape
=
[
T
.
get_scalar_constant_value
(
T
.
as_tensor_variable
(
x
))
for
x
in
image_shape
]
N_image_shape
=
[
T
.
get_scalar_constant_value
(
T
.
as_tensor_variable
(
x
))
for
x
in
image_shape
]
if
N_filter_shape
is
None
:
N_filter_shape
=
[
T
.
get_scalar_constant_value
(
T
.
as_tensor_variable
(
x
))
for
x
in
filter_shape
]
N_filter_shape
=
[
T
.
get_scalar_constant_value
(
T
.
as_tensor_variable
(
x
))
for
x
in
filter_shape
]
if
input
is
None
:
input
=
self
.
input
if
not
filters
:
filters
=
self
.
filters
#
############ THEANO IMPLEMENTATION ############
#
THEANO IMPLEMENTATION
# we create a symbolic function so that verify_grad can work
def
sym_conv2d
(
input
,
filters
):
# define theano graph and function
input
.
name
=
'input'
filters
.
name
=
'filters'
rval
=
conv
.
conv2d
(
input
,
filters
,
image_shape
,
filter_shape
,
border_mode
,
subsample
,
unroll_batch
=
unroll_batch
,
unroll_kern
=
unroll_kern
,
unroll_patch
=
unroll_patch
)
rval
=
conv
.
conv2d
(
input
,
filters
,
image_shape
,
filter_shape
,
border_mode
,
subsample
,
unroll_batch
=
unroll_batch
,
unroll_kern
=
unroll_kern
,
unroll_patch
=
unroll_patch
)
rval
.
name
=
'conv_output'
return
rval
...
...
@@ -89,15 +90,15 @@ class TestConv2D(utt.InferShapeTester):
else
:
if
should_raise
:
raise
Exception
(
"ConvOp should have generated an error"
)
"ConvOp should have generated an error"
)
#
############ REFERENCE IMPLEMENTATION ############
#
REFERENCE IMPLEMENTATION
s
=
1.
orig_image_data
=
image_data
if
border_mode
is
not
'full'
:
s
=
-
1.
out_shape2d
=
numpy
.
array
(
N_image_shape
[
-
2
:])
+
\
s
*
numpy
.
array
(
N_filter_shape
[
-
2
:])
-
s
s
*
numpy
.
array
(
N_filter_shape
[
-
2
:])
-
s
out_shape2d
=
numpy
.
ceil
(
out_shape2d
/
numpy
.
array
(
subsample
))
# avoid numpy deprecation
out_shape2d
=
out_shape2d
.
astype
(
'int32'
)
...
...
@@ -110,8 +111,9 @@ class TestConv2D(utt.InferShapeTester):
image_data2
=
numpy
.
zeros
((
N_image_shape
[
0
],
N_image_shape
[
1
],
N_image_shape
[
2
]
+
2
*
N_filter_shape
[
2
]
-
2
,
N_image_shape
[
3
]
+
2
*
N_filter_shape
[
3
]
-
2
))
image_data2
[:,
:,
N_filter_shape
[
2
]
-
1
:
N_filter_shape
[
2
]
-
1
+
N_image_shape
[
2
],
N_filter_shape
[
3
]
-
1
:
N_filter_shape
[
3
]
-
1
+
N_image_shape
[
3
]]
=
image_data
image_data2
[
:,
:,
N_filter_shape
[
2
]
-
1
:
N_filter_shape
[
2
]
-
1
+
N_image_shape
[
2
],
N_filter_shape
[
3
]
-
1
:
N_filter_shape
[
3
]
-
1
+
N_image_shape
[
3
]]
=
image_data
image_data
=
image_data2
N_image_shape
=
image_data
.
shape
for
bb
in
range
(
N_image_shape
[
0
]):
...
...
@@ -130,7 +132,7 @@ class TestConv2D(utt.InferShapeTester):
self
.
assertTrue
(
_allclose
(
theano_output
,
ref_output
))
#
############ TEST GRADIENT ############
#
TEST GRADIENT
if
verify_grad
:
utt
.
verify_grad
(
sym_conv2d
,
[
orig_image_data
,
filter_data
])
...
...
@@ -157,12 +159,11 @@ class TestConv2D(utt.InferShapeTester):
def
test_uint_image_shape_datatype
(
self
):
"""Tests for uint datatype in image_shape.
"""
self
.
validate
((
2
,
2
,
3
,
numpy
.
uint8
(
3
)),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
numpy
.
uint16
(
2
),
2
,
3
,
3
),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
2
,
numpy
.
uint32
(
2
),
3
,
3
),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
def
test_uint_filter_shape_datatype
(
self
):
"""Tests for uint datatype in filter_shape
...
...
@@ -170,7 +171,7 @@ class TestConv2D(utt.InferShapeTester):
self
.
validate
((
3
,
2
,
3
,
3
),
(
2
,
2
,
3
,
numpy
.
uint8
(
3
)),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
numpy
.
uint16
(
2
),
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
2
,
numpy
.
uint32
(
2
),
3
,
3
),
'valid'
,
verify_grad
=
False
)
def
test_img_kernel_same_shape
(
self
):
self
.
validate
((
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'full'
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'valid'
)
...
...
@@ -181,8 +182,9 @@ class TestConv2D(utt.InferShapeTester):
"""
self
.
validate
((
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'valid'
,
unroll_patch
=
True
)
self
.
validate
((
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'full'
,
unroll_patch
=
True
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'valid'
,
unroll_patch
=
True
,
verify_grad
=
False
)
self
.
validate
(
(
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'valid'
,
unroll_patch
=
True
,
verify_grad
=
False
)
def
test_unroll_patch_false
(
self
):
"""
...
...
@@ -190,22 +192,26 @@ class TestConv2D(utt.InferShapeTester):
"""
self
.
validate
((
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'valid'
,
unroll_patch
=
False
)
self
.
validate
((
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'full'
,
unroll_patch
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'valid'
,
unroll_patch
=
False
,
verify_grad
=
False
)
self
.
validate
(
(
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'valid'
,
unroll_patch
=
False
,
verify_grad
=
False
)
def
test_unroll_patch_true_fail
(
self
):
"""
Test basic convs with True.
"""
self
.
validate
((
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'valid'
,
unroll_patch
=
True
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
((
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'full'
,
unroll_patch
=
True
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'valid'
,
unroll_patch
=
True
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
(
(
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'valid'
,
unroll_patch
=
True
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
(
(
3
,
2
,
7
,
5
),
(
5
,
2
,
2
,
3
),
'full'
,
unroll_patch
=
True
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
(
(
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'valid'
,
unroll_patch
=
True
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
def
test_unroll_special
(
self
):
"""
...
...
@@ -218,20 +224,24 @@ class TestConv2D(utt.InferShapeTester):
Test mini-batch unrolling for various legal values.
"""
# mini-batch of size 6 is multiple of 2 and 3. Should work.
self
.
validate
((
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
verify_grad
=
False
)
self
.
validate
((
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
3
,
verify_grad
=
False
)
self
.
validate
(
(
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
verify_grad
=
False
)
self
.
validate
(
(
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
3
,
verify_grad
=
False
)
def
test_unroll_kern
(
self
):
"""
Test kernel unrolling for various legal values.
"""
# 6 filters is a multiple of 2 and 3. Should work.
self
.
validate
((
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_kern
=
2
,
verify_grad
=
False
)
self
.
validate
((
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_kern
=
3
,
verify_grad
=
False
)
self
.
validate
(
(
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_kern
=
2
,
verify_grad
=
False
)
self
.
validate
(
(
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_kern
=
3
,
verify_grad
=
False
)
def
test_unroll_batch_kern
(
self
):
"""Test mini-batch unrolling with kernel unrolling for various
...
...
@@ -239,15 +249,19 @@ class TestConv2D(utt.InferShapeTester):
"""
# mini-batch of size 6 is multiple of 2 and 3. Should work.
self
.
validate
((
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
3
,
verify_grad
=
False
)
self
.
validate
((
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
3
,
unroll_kern
=
3
,
verify_grad
=
False
)
self
.
validate
(
(
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
3
,
verify_grad
=
False
)
self
.
validate
(
(
6
,
2
,
3
,
3
),
(
3
,
2
,
2
,
2
),
'valid'
,
unroll_batch
=
3
,
unroll_kern
=
3
,
verify_grad
=
False
)
# 6 filters is a multiple of 2 and 3. Should work.
self
.
validate
((
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
2
,
verify_grad
=
False
)
self
.
validate
((
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
3
,
verify_grad
=
False
)
self
.
validate
(
(
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
2
,
verify_grad
=
False
)
self
.
validate
(
(
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
3
,
verify_grad
=
False
)
def
test_unroll_batch_kern_fail
(
self
):
"""Test mini-batch unrolling with kernel unrolling for various
...
...
@@ -264,14 +278,16 @@ class TestConv2D(utt.InferShapeTester):
unroll_batch
=
3
,
unroll_kern
=
3
,
N_image_shape
=
(
6
,
2
,
3
,
3
),
N_filter_shape
=
(
4
,
2
,
2
,
2
),
should_raise
=
True
)
self
.
validate
((
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
2
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
((
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
3
,
N_image_shape
=
(
2
,
3
,
3
,
3
),
N_filter_shape
=
(
5
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
(
(
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
2
,
N_image_shape
=
(
1
,
3
,
3
,
3
),
N_filter_shape
=
(
6
,
3
,
2
,
2
),
should_raise
=
True
)
self
.
validate
(
(
2
,
3
,
3
,
3
),
(
6
,
3
,
2
,
2
),
'valid'
,
unroll_batch
=
2
,
unroll_kern
=
3
,
N_image_shape
=
(
2
,
3
,
3
,
3
),
N_filter_shape
=
(
5
,
3
,
2
,
2
),
should_raise
=
True
)
@attr
(
'slow'
)
def
test_subsample
(
self
):
...
...
@@ -292,12 +308,14 @@ class TestConv2D(utt.InferShapeTester):
Tests convolution where the {image,filter}_shape is a Constant tensor.
"""
as_t
=
T
.
as_tensor_variable
self
.
validate
(
(
as_t
(
3
),
as_t
(
2
),
as_t
(
7
),
as_t
(
5
)),
(
5
,
2
,
2
,
3
),
'valid'
)
self
.
validate
(
(
as_t
(
3
),
as_t
(
2
),
as_t
(
7
),
as_t
(
5
)),
(
5
,
2
,
2
,
3
),
'valid'
)
self
.
validate
(
as_t
([
3
,
2
,
7
,
5
]),
(
5
,
2
,
2
,
3
),
'valid'
)
self
.
validate
(
as_t
((
3
,
2
,
7
,
5
)),
(
5
,
2
,
2
,
3
),
'valid'
)
self
.
validate
((
3
,
2
,
7
,
5
),
(
as_t
(
5
),
as_t
(
2
),
as_t
(
2
),
as_t
(
3
)),
'valid'
)
self
.
validate
(
(
3
,
2
,
7
,
5
),
(
as_t
(
5
),
as_t
(
2
),
as_t
(
2
),
as_t
(
3
)),
'valid'
)
self
.
validate
((
3
,
2
,
7
,
5
),
as_t
([
5
,
2
,
2
,
3
]),
'valid'
)
self
.
validate
((
3
,
2
,
7
,
5
),
as_t
((
5
,
2
,
2
,
3
)),
'valid'
)
self
.
validate
(
as_t
([
3
,
2
,
7
,
5
]),
as_t
([
5
,
2
,
2
,
3
]),
'full'
)
...
...
@@ -444,10 +462,11 @@ class TestConv2D(utt.InferShapeTester):
print
(
border_mode
)
for
openmp
in
[
False
,
True
]:
print
(
"OpenMP"
,
openmp
)
image_shapes
=
[(
1
,
5
,
6
,
6
),
(
10
,
5
,
6
,
6
),
#(10, 10, 16, 16),
#(10, 10, 32, 32)
image_shapes
=
[
(
1
,
5
,
6
,
6
),
(
10
,
5
,
6
,
6
)
# (10, 10, 16, 16),
# (10, 10, 32, 32)]
]
print
(
"image_shape"
,
image_shapes
)
for
image_shape
in
image_shapes
:
...
...
@@ -458,11 +477,12 @@ class TestConv2D(utt.InferShapeTester):
input
=
theano
.
shared
(
numpy
.
random
.
random
(
image_shape
))
filters
=
theano
.
shared
(
numpy
.
random
.
random
(
filter_shape
))
output
=
self
.
conv2d
(
input
,
filters
,
image_shape
,
filter_shape
,
border_mode
,
unroll_patch
=
True
,
openmp
=
openmp
)
output
=
self
.
conv2d
(
input
,
filters
,
image_shape
,
filter_shape
,
border_mode
,
unroll_patch
=
True
,
openmp
=
openmp
)
mode
=
theano
.
Mode
(
linker
=
theano
.
gof
.
vm
.
VM_Linker
(
allow_gc
=
False
,
use_cloop
=
True
))
...
...
@@ -474,8 +494,8 @@ class TestConv2D(utt.InferShapeTester):
print
()
def
test_infer_shape
(
self
):
# Note: infer_shape is incomplete and thus input and filter shapes
# must be provided explicitly
# Note: infer_shape is incomplete and thus input and filter shapes
# must be provided explicitly
def
rand
(
*
shape
):
r
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
...
...
@@ -487,71 +507,101 @@ class TestConv2D(utt.InferShapeTester):
bivec_val
=
[
7
,
5
,
3
,
2
]
adtens_val
=
rand
(
*
aivec_val
)
bdtens_val
=
rand
(
*
bivec_val
)
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
aivec_val
=
[
6
,
2
,
8
,
3
]
bivec_val
=
[
4
,
2
,
5
,
3
]
adtens_val
=
rand
(
*
aivec_val
)
bdtens_val
=
rand
(
*
bivec_val
)
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
aivec_val
=
[
3
,
6
,
7
,
5
]
bivec_val
=
[
5
,
6
,
3
,
2
]
adtens_val
=
rand
(
*
aivec_val
)
bdtens_val
=
rand
(
*
bivec_val
)
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
aivec_val
=
[
3
,
6
,
7
,
5
]
bivec_val
=
[
5
,
6
,
2
,
3
]
adtens_val
=
rand
(
*
aivec_val
)
bdtens_val
=
rand
(
*
bivec_val
)
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
aivec_val
=
[
5
,
2
,
4
,
3
]
bivec_val
=
[
6
,
2
,
4
,
3
]
adtens_val
=
rand
(
*
aivec_val
)
bdtens_val
=
rand
(
*
bivec_val
)
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
([
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'valid'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
self
.
_compile_and_check
(
[
adtens
,
bdtens
],
[
self
.
conv2d
(
adtens
,
bdtens
,
aivec_val
,
bivec_val
,
border_mode
=
'full'
)],
[
adtens_val
,
bdtens_val
],
conv
.
ConvOp
,
excluding
=
[
'conv_gemm'
])
class
TestDefaultConv2D
(
TestConv2D
):
...
...
@@ -560,17 +610,19 @@ class TestDefaultConv2D(TestConv2D):
# Test that broadcasting of gradients works correctly when using the
# nnet.conv2d() interface. This was reported in #3763, and uses the example
# code from that ticket.
def
test_broadcast_grad
():
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
#
rng = numpy.random.RandomState(utt.fetch_seed())
x1
=
T
.
tensor4
(
'x'
)
x1_data
=
rng
.
randn
(
1
,
1
,
300
,
300
)
#
x1_data = rng.randn(1, 1, 300, 300)
sigma
=
T
.
scalar
(
'sigma'
)
sigma_data
=
20
#
sigma_data = 20
window_radius
=
3
filter_1d
=
T
.
arange
(
-
window_radius
,
window_radius
+
1
)
filter_1d
=
T
.
arange
(
-
window_radius
,
window_radius
+
1
)
filter_1d
=
filter_1d
.
astype
(
theano
.
config
.
floatX
)
filter_1d
=
T
.
exp
(
-
0.5
*
filter_1d
**
2
/
sigma
**
2
)
filter_1d
=
T
.
exp
(
-
0.5
*
filter_1d
**
2
/
sigma
**
2
)
filter_1d
=
filter_1d
/
filter_1d
.
sum
()
filter_W
=
filter_1d
.
dimshuffle
([
'x'
,
'x'
,
0
,
'x'
])
...
...
theano/tensor/nnet/tests/test_conv3d.py
浏览文件 @
d5944c96
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
theano
import
theano.tensor
as
T
from
theano
import
function
,
shared
...
...
@@ -8,13 +7,13 @@ from theano.tensor.nnet.ConvTransp3D import convTransp3D, ConvTransp3D
from
theano.tensor.nnet.ConvGrad3D
import
convGrad3D
,
ConvGrad3D
from
theano.tensor.nnet.Conv3D
import
conv3D
,
Conv3D
from
theano.tests.unittest_tools
import
attr
from
nose.plugins.skip
import
SkipTest
import
numpy
as
N
from
six.moves
import
xrange
import
copy
import
theano.sparse
if
theano
.
sparse
.
enable_sparse
:
from
scipy
import
sparse
from
nose.plugins.skip
import
SkipTest
floatX
=
theano
.
config
.
floatX
...
...
@@ -74,8 +73,8 @@ class DummyConvGrad3D:
self
.
V
,
self
.
dCdH
=
VdHvals
self
.
dV
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
ddCdH
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
dCdH
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
ddCdH
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
dCdH
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
d
=
d
self
.
WShape
=
WShape
...
...
@@ -142,8 +141,8 @@ class TestConv3D(utt.InferShapeTester):
self
.
RShape
=
T
.
vector
(
dtype
=
'int64'
)
self
.
RShape
.
name
=
'RShape'
self
.
otherH
=
T
.
TensorType
(
floatX
,
(
False
,
False
,
False
,
False
,
False
))(
name
=
'otherH'
)
self
.
otherH
=
T
.
TensorType
(
floatX
,
(
False
,
False
,
False
,
False
,
False
))(
name
=
'otherH'
)
self
.
transp
=
convTransp3D
(
self
.
W
,
self
.
rb
,
self
.
d
,
self
.
otherH
,
self
.
RShape
)
self
.
transp
.
name
=
'transp'
...
...
@@ -165,18 +164,17 @@ class TestConv3D(utt.InferShapeTester):
W_grad
=
T
.
grad
(
self
.
reconsObj
,
self
.
W
)
self
.
gradientsFunc
=
function
(
[
self
.
RShape
],
[
W_grad
,
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
mode
=
mode
)
self
.
gradientsFunc
=
function
(
[
self
.
RShape
]
,
[
W_grad
,
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
mode
=
mode
)
self
.
check_c_against_python
=
function
(
[
self
.
RShape
],
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
mode
=
'DEBUG_MODE'
)
self
.
check_c_against_python
=
function
(
[
self
.
RShape
]
,
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
mode
=
'DEBUG_MODE'
)
self
.
dCdW_shape_func
=
function
([
self
.
RShape
],
T
.
grad
(
self
.
reconsObj
,
self
.
W
)
.
shape
,
mode
=
mode
)
self
.
dCdW_shape_func
=
function
([
self
.
RShape
],
T
.
grad
(
self
.
reconsObj
,
self
.
W
)
.
shape
,
mode
=
mode
)
def
random_tensor
(
self
,
*
dims
):
return
N
.
asarray
(
self
.
rng
.
uniform
(
-.
05
,
.
05
,
dims
),
dtype
=
floatX
)
...
...
@@ -205,20 +203,22 @@ class TestConv3D(utt.InferShapeTester):
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
\
self
.
rng
.
randint
(
1
,
15
)
outputHeight
=
int
((
videoHeight
-
filterHeight
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
0
])
+
1
outputWidth
=
int
((
videoWidth
-
filterWidth
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
1
])
+
1
outputDur
=
int
((
videoDur
-
filterDur
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
2
])
+
1
int
((
videoHeight
-
filterHeight
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
0
])
+
1
int
((
videoWidth
-
filterWidth
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
1
])
+
1
int
((
videoDur
-
filterDur
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
2
])
+
1
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
def
test_c_against_python
(
self
):
...
...
@@ -250,15 +250,17 @@ class TestConv3D(utt.InferShapeTester):
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
W
.
get_value
(
borrow
=
True
)
*
(
self
.
W
.
get_value
(
borrow
=
True
)
<
1e-5
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
W
.
get_value
(
borrow
=
True
)
*
(
self
.
W
.
get_value
(
borrow
=
True
)
<
1e-5
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
Hv
=
self
.
H_func
()
...
...
@@ -272,11 +274,11 @@ class TestConv3D(utt.InferShapeTester):
Hv_mat
=
N
.
zeros
((
batchSize
,
numFilters
))
for
qi
in
xrange
(
0
,
numFilters
):
W_mat
[:,
qi
]
=
\
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
Hv_mat
[:,
qi
]
=
Hv
[:,
0
,
0
,
0
,
qi
]
for
qi
in
xrange
(
0
,
batchSize
):
V_mat
[
qi
,
:]
=
\
self
.
V
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
self
.
V
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
H_mat
=
N
.
dot
(
V_mat
,
W_mat
)
+
self
.
b
.
get_value
(
borrow
=
True
)
...
...
@@ -288,16 +290,16 @@ class TestConv3D(utt.InferShapeTester):
print
(
H_mat
)
print
(
Hv_mat
)
print
(
'max error: '
+
str
(
N
.
abs
(
H_mat
-
Hv_mat
)
.
max
()))
W
.
get_value
(
borrow
=
True
)[
W
.
get_value
(
borrow
=
True
)
!=
0
]
+=
1.0
print
(
'min non-zero kernel mag: '
+
\
str
(
N
.
abs
(
W
.
get_value
(
borrow
=
True
))
.
min
()))
self
.
W
.
get_value
(
borrow
=
True
)[
self
.
W
.
get_value
(
borrow
=
True
)
!=
0
]
+=
1.0
print
(
'min non-zero kernel mag: '
+
str
(
N
.
abs
(
self
.
W
.
get_value
(
borrow
=
True
))
.
min
()))
assert
False
def
test_c_against_mat_transp_mul
(
self
):
# Use a filter of the same size as the image, so the convolution is just a
# dense matrix multiply.
# Check that dense matrix multiplication by the transpose of the matrix
# gives the same result as ConvTransp.
# Use a filter of the same size as the image, so the convolution is just a
# dense matrix multiply.
# Check that dense matrix multiplication by the transpose of the matrix
# gives the same result as ConvTransp.
batchSize
=
self
.
rng
.
randint
(
1
,
10
)
videoDur
=
self
.
rng
.
randint
(
3
,
15
)
videoWidth
=
self
.
rng
.
randint
(
3
,
15
)
...
...
@@ -315,13 +317,15 @@ class TestConv3D(utt.InferShapeTester):
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
H_shape
=
self
.
H_shape_func
()
...
...
@@ -330,7 +334,7 @@ class TestConv3D(utt.InferShapeTester):
assert
H_shape
[
2
]
==
1
assert
H_shape
[
3
]
==
1
Hv
=
self
.
random_tensor
(
*
H_shape
)
Hv
=
self
.
random_tensor
(
*
H_shape
)
Vv
=
self
.
transp_func
(
Hv
,
[
videoHeight
,
videoWidth
,
videoDur
])
...
...
@@ -344,12 +348,12 @@ class TestConv3D(utt.InferShapeTester):
Hv_mat
=
N
.
zeros
((
numFilters
,
batchSize
))
for
qi
in
xrange
(
0
,
numFilters
):
W_mat
[
qi
,
:]
=
\
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
Hv_mat
[
qi
,
:]
=
Hv
[:,
0
,
0
,
0
,
qi
]
for
qi
in
xrange
(
0
,
batchSize
):
Vv_mat
[:,
qi
]
=
Vv
[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
V_mat
=
(
N
.
dot
(
W_mat
.
transpose
(),
Hv_mat
)
.
transpose
()
+
\
V_mat
=
(
N
.
dot
(
W_mat
.
transpose
(),
Hv_mat
)
.
transpose
()
+
rbv
)
.
transpose
()
if
N
.
abs
(
V_mat
-
Vv_mat
)
.
max
()
>
1e-5
:
...
...
@@ -359,13 +363,14 @@ class TestConv3D(utt.InferShapeTester):
for
qq
in
xrange
(
V_mat
.
shape
[
0
]):
for
qqq
in
xrange
(
Vv_mat
.
shape
[
1
]):
if
abs
(
V_mat
[
qq
,
qqq
]
-
Vv_mat
[
qq
,
qqq
])
>
1e-5
:
print
((
'wrong at '
+
str
((
qq
,
qqq
))
+
': '
+
str
(
V_mat
[
qq
,
qqq
],
Vv_mat
[
qq
,
qqq
])))
print
(
(
'wrong at '
+
str
((
qq
,
qqq
))
+
': '
+
str
(
V_mat
[
qq
,
qqq
],
Vv_mat
[
qq
,
qqq
])))
assert
False
def
test_c_against_sparse_mat_transp_mul
(
self
):
# like test_c_against_mat_transp_mul but using a sparse matrix and a kernel
# that is smaller than the image
# like test_c_against_mat_transp_mul but using a sparse matrix and a kernel
# that is smaller than the image
if
not
theano
.
sparse
.
enable_sparse
:
raise
SkipTest
(
'Optional package sparse disabled'
)
...
...
@@ -390,23 +395,22 @@ class TestConv3D(utt.InferShapeTester):
col_steps
=
self
.
rng
.
randint
(
1
,
4
)
time_steps
=
self
.
rng
.
randint
(
1
,
4
)
#print (row_steps,col_steps,time_steps)
#
print (row_steps,col_steps,time_steps)
videoDur
=
(
time_steps
-
1
)
*
dt
+
filterDur
+
\
self
.
rng
.
randint
(
0
,
3
)
videoWidth
=
(
col_steps
-
1
)
*
dc
+
filterWidth
+
\
self
.
rng
.
randint
(
0
,
3
)
videoHeight
=
(
row_steps
-
1
)
*
dr
+
filterHeight
+
\
self
.
rng
.
randint
(
0
,
3
)
videoDur
=
(
time_steps
-
1
)
*
dt
+
filterDur
+
self
.
rng
.
randint
(
0
,
3
)
videoWidth
=
(
col_steps
-
1
)
*
dc
+
filterWidth
+
self
.
rng
.
randint
(
0
,
3
)
videoHeight
=
(
row_steps
-
1
)
*
dr
+
filterHeight
+
self
.
rng
.
randint
(
0
,
3
)
inputChannels
=
self
.
rng
.
randint
(
1
,
15
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
# just needed so H_shape works
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
H_shape
=
self
.
H_shape_func
()
...
...
@@ -455,10 +459,10 @@ class TestConv3D(utt.InferShapeTester):
placed_filter
=
N
.
zeros
(
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:])
placed_filter
[
ri
*
dr
:
ri
*
dr
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
1
],
ci
*
dc
:
ci
*
dc
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
2
],
ti
*
dt
:
ti
*
dt
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
3
],
:]
=
self
.
W
.
get_value
(
borrow
=
True
)[
hi
,
:,
:,
:,
:]
ri
*
dr
:
ri
*
dr
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
1
],
ci
*
dc
:
ci
*
dc
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
2
],
ti
*
dt
:
ti
*
dt
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
3
],
:]
=
self
.
W
.
get_value
(
borrow
=
True
)[
hi
,
:,
:,
:,
:]
W_mat
[
qi
,
:]
=
placed_filter
.
reshape
((
n
))
Hv_mat
[
qi
,
:]
=
Hv
[:,
ri
,
ci
,
ti
,
hi
]
...
...
@@ -488,29 +492,32 @@ class TestConv3D(utt.InferShapeTester):
self
.
_compile_and_check
([],
[
self
.
H
],
[],
Conv3D
)
# ConvTransp3D
self
.
_compile_and_check
([
self
.
RShape
],
[
self
.
R
],
[
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]],
ConvTransp3D
)
self
.
_compile_and_check
(
[
self
.
RShape
],
[
self
.
R
],
[
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]],
ConvTransp3D
)
# ConvGrad3D
self
.
_compile_and_check
(
[
self
.
RShape
],
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
)
,
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
[
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]],
ConvGrad3D
)
self
.
_compile_and_check
(
[
self
.
RShape
]
,
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
[
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]],
ConvGrad3D
)
def
test_gradient
(
self
):
self
.
randomize
()
rng
,
V
,
W
,
b
,
d
,
rb
=
self
.
rng
,
self
.
V
,
self
.
W
,
self
.
b
,
self
.
d
,
self
.
rb
dCdH
=
shared
(
self
.
random_tensor
(
*
self
.
H_shape_func
()))
testsPerDir
=
2
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConv3D
(
rng
,
(
V
,
W
,
b
),
d
),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvTransp3D
(
rng
,
(
W
,
rb
,
dCdH
),
d
,
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvGrad3D
(
rng
,
(
V
,
dCdH
),
d
,
W
.
get_value
(
borrow
=
True
)
.
shape
),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConv3D
(
rng
,
(
V
,
W
,
b
),
d
),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvTransp3D
(
rng
,
(
W
,
rb
,
dCdH
),
d
,
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvGrad3D
(
rng
,
(
V
,
dCdH
),
d
,
W
.
get_value
(
borrow
=
True
)
.
shape
),
[
0.0
],
n_tests
=
testsPerDir
)
if
__name__
==
'__main__'
:
...
...
theano/tensor/nnet/tests/test_conv3d2d.py
浏览文件 @
d5944c96
...
...
@@ -11,7 +11,7 @@ from six.moves import xrange
import
theano
from
theano.gof.opt
import
check_stack_trace
from
theano.tensor.nnet.conv3d2d
import
*
from
theano.tensor.nnet.conv3d2d
import
conv3d
,
get_diagonal_subtensor_view
,
DiagonalSubtensor
,
IncDiagonalSubtensor
import
theano.tests.unittest_tools
as
utt
...
...
@@ -57,11 +57,11 @@ def pyconv3d(signals, filters):
Ns
,
Ts
,
C
,
Hs
,
Ws
=
signals
.
shape
Nf
,
Tf
,
C
,
Hf
,
Wf
=
filters
.
shape
Tf2
=
Tf
//
2
Hf2
=
Hf
//
2
Wf2
=
Wf
//
2
Tf2
=
Tf
//
2
Hf2
=
Hf
//
2
Wf2
=
Wf
//
2
rval
=
numpy
.
zeros
((
Ns
,
Ts
-
Tf
+
1
,
Nf
,
Hs
-
Hf
+
1
,
Ws
-
Wf
+
1
))
rval
=
numpy
.
zeros
((
Ns
,
Ts
-
Tf
+
1
,
Nf
,
Hs
-
Hf
+
1
,
Ws
-
Wf
+
1
))
for
ns
in
xrange
(
Ns
):
for
nf
in
xrange
(
Nf
):
for
c
in
xrange
(
C
):
...
...
@@ -71,7 +71,7 @@ def pyconv3d(signals, filters):
o_i
=
ndimage
.
convolve
(
s_i
,
f_i
,
mode
=
'constant'
,
cval
=
1
)
o_i_sh0
=
o_i
.
shape
[
0
]
# print s_i.shape, f_i.shape, r_i.shape, o_i.shape
r_i
+=
o_i
[
Tf2
:
o_i_sh0
-
Tf2
,
Hf2
:
-
Hf2
,
Wf2
:
-
Wf2
]
r_i
+=
o_i
[
Tf2
:
o_i_sh0
-
Tf2
,
Hf2
:
-
Hf2
,
Wf2
:
-
Wf2
]
return
rval
...
...
@@ -85,10 +85,10 @@ def test_conv3d(mode=mode_without_gpu, shared=theano.tensor._shared):
raise
SkipTest
(
"conv3d2d tests need SciPy"
)
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
10
,
3
,
32
,
32
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
5
,
3
,
5
,
5
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
5
,
3
,
5
,
5
signals
=
numpy
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
numpy
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
signals
=
numpy
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
numpy
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
t0
=
time
.
time
()
pyres
=
pyconv3d
(
signals
,
filters
)
...
...
@@ -96,7 +96,7 @@ def test_conv3d(mode=mode_without_gpu, shared=theano.tensor._shared):
s_signals
=
shared
(
signals
)
s_filters
=
shared
(
filters
)
s_output
=
shared
(
signals
*
0
)
s_output
=
shared
(
signals
*
0
)
out
=
conv3d
(
s_signals
,
s_filters
,
signals_shape
=
signals
.
shape
,
...
...
@@ -130,12 +130,12 @@ def test_conv3d(mode=mode_without_gpu, shared=theano.tensor._shared):
filters
=
numpy
.
random
.
rand
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
utt
.
verify_grad
(
conv3d
,
[
signals
,
filters
],
eps
=
1e-1
,
mode
=
mode
)
#
##
Additional Test that covers the case of patched implementation for filter with Tf=1
# Additional Test that covers the case of patched implementation for filter with Tf=1
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
10
,
3
,
32
,
32
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
1
,
3
,
5
,
5
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
1
,
3
,
5
,
5
signals
=
numpy
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
numpy
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
signals
=
numpy
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
numpy
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
t0
=
time
.
time
()
pyres
=
pyconv3d
(
signals
,
filters
)
...
...
@@ -143,7 +143,7 @@ def test_conv3d(mode=mode_without_gpu, shared=theano.tensor._shared):
s_signals
=
shared
(
signals
)
s_filters
=
shared
(
filters
)
s_output
=
shared
(
signals
*
0
)
s_output
=
shared
(
signals
*
0
)
out
=
conv3d
(
s_signals
,
s_filters
,
signals_shape
=
signals
.
shape
,
...
...
theano/tensor/nnet/tests/test_neighbours.py
浏览文件 @
d5944c96
from
__future__
import
absolute_import
,
print_function
,
division
from
nose.plugins.skip
import
SkipTest
import
numpy
import
unittest
...
...
@@ -22,13 +21,11 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for
shape
,
pshape
in
[((
10
,
7
,
18
,
18
),
(
2
,
2
)),
((
10
,
7
,
6
,
18
),
(
3
,
2
)),
((
5
,
7
,
66
,
66
),
(
33
,
33
)),
((
5
,
7
,
68
,
66
),
(
34
,
33
))
]:
((
5
,
7
,
68
,
66
),
(
34
,
33
))]:
for
border
in
[
'valid'
,
'ignore_borders'
]:
for
dtype
in
self
.
dtypes
:
images
=
shared
(
numpy
.
arange
(
numpy
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
numpy
.
arange
(
numpy
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
(
pshape
)
f
=
function
([],
...
...
@@ -51,8 +48,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
shape
=
(
2
,
3
,
4
,
4
)
for
dtype
in
self
.
dtypes
:
images
=
shared
(
numpy
.
arange
(
numpy
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
numpy
.
arange
(
numpy
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
((
2
,
2
))
for
border
in
[
'valid'
,
'ignore_borders'
]:
...
...
@@ -64,31 +60,31 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
# print images.get_value(borrow=True)
neibs
=
f
()
# print neibs
assert
numpy
.
allclose
(
neibs
,
[[
0
,
1
,
4
,
5
],
[
2
,
3
,
6
,
7
],
[
8
,
9
,
12
,
13
],
[
10
,
11
,
14
,
15
],
[
16
,
17
,
20
,
21
],
[
18
,
19
,
22
,
23
],
[
24
,
25
,
28
,
29
],
[
26
,
27
,
30
,
31
],
[
32
,
33
,
36
,
37
],
[
34
,
35
,
38
,
39
],
[
40
,
41
,
44
,
45
],
[
42
,
43
,
46
,
47
],
[
48
,
49
,
52
,
53
],
[
50
,
51
,
54
,
55
],
[
56
,
57
,
60
,
61
],
[
58
,
59
,
62
,
63
],
[
64
,
65
,
68
,
69
],
[
66
,
67
,
70
,
71
],
[
72
,
73
,
76
,
77
],
[
74
,
75
,
78
,
79
],
[
80
,
81
,
84
,
85
],
[
82
,
83
,
86
,
87
],
[
88
,
89
,
92
,
93
],
[
90
,
91
,
94
,
95
]])
assert
numpy
.
allclose
(
neibs
,
[
[
0
,
1
,
4
,
5
],
[
2
,
3
,
6
,
7
],
[
8
,
9
,
12
,
13
],
[
10
,
11
,
14
,
15
],
[
16
,
17
,
20
,
21
],
[
18
,
19
,
22
,
23
],
[
24
,
25
,
28
,
29
],
[
26
,
27
,
30
,
31
],
[
32
,
33
,
36
,
37
],
[
34
,
35
,
38
,
39
],
[
40
,
41
,
44
,
45
],
[
42
,
43
,
46
,
47
],
[
48
,
49
,
52
,
53
],
[
50
,
51
,
54
,
55
],
[
56
,
57
,
60
,
61
],
[
58
,
59
,
62
,
63
],
[
64
,
65
,
68
,
69
],
[
66
,
67
,
70
,
71
],
[
72
,
73
,
76
,
77
],
[
74
,
75
,
78
,
79
],
[
80
,
81
,
84
,
85
],
[
82
,
83
,
86
,
87
],
[
88
,
89
,
92
,
93
],
[
90
,
91
,
94
,
95
]])
g
=
function
([],
neibs2images
(
neibs
,
neib_shape
,
images
.
shape
),
mode
=
self
.
mode
)
...
...
@@ -111,38 +107,38 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
assert
self
.
op
in
[
type
(
node
.
op
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
assert
numpy
.
allclose
(
neibs
,
[[
0
,
1
,
2
,
5
,
6
,
7
,
10
,
11
,
12
],
[
2
,
3
,
4
,
7
,
8
,
9
,
12
,
13
,
14
],
[
10
,
11
,
12
,
15
,
16
,
17
,
20
,
21
,
22
],
[
12
,
13
,
14
,
17
,
18
,
19
,
22
,
23
,
24
],
[
25
,
26
,
27
,
30
,
31
,
32
,
35
,
36
,
37
],
[
27
,
28
,
29
,
32
,
33
,
34
,
37
,
38
,
39
],
[
35
,
36
,
37
,
40
,
41
,
42
,
45
,
46
,
47
],
[
37
,
38
,
39
,
42
,
43
,
44
,
47
,
48
,
49
],
[
50
,
51
,
52
,
55
,
56
,
57
,
60
,
61
,
62
],
[
52
,
53
,
54
,
57
,
58
,
59
,
62
,
63
,
64
],
[
60
,
61
,
62
,
65
,
66
,
67
,
70
,
71
,
72
],
[
62
,
63
,
64
,
67
,
68
,
69
,
72
,
73
,
74
],
[
75
,
76
,
77
,
80
,
81
,
82
,
85
,
86
,
87
],
[
77
,
78
,
79
,
82
,
83
,
84
,
87
,
88
,
89
],
[
85
,
86
,
87
,
90
,
91
,
92
,
95
,
96
,
97
],
[
87
,
88
,
89
,
92
,
93
,
94
,
97
,
98
,
99
],
[
100
,
101
,
102
,
105
,
106
,
107
,
110
,
111
,
112
],
[
102
,
103
,
104
,
107
,
108
,
109
,
112
,
113
,
114
],
[
110
,
111
,
112
,
115
,
116
,
117
,
120
,
121
,
122
],
[
112
,
113
,
114
,
117
,
118
,
119
,
122
,
123
,
124
],
[
125
,
126
,
127
,
130
,
131
,
132
,
135
,
136
,
137
],
[
127
,
128
,
129
,
132
,
133
,
134
,
137
,
138
,
139
],
[
135
,
136
,
137
,
140
,
141
,
142
,
145
,
146
,
147
],
[
137
,
138
,
139
,
142
,
143
,
144
,
147
,
148
,
149
]])
assert
numpy
.
allclose
(
neibs
,
[
[
0
,
1
,
2
,
5
,
6
,
7
,
10
,
11
,
12
],
[
2
,
3
,
4
,
7
,
8
,
9
,
12
,
13
,
14
],
[
10
,
11
,
12
,
15
,
16
,
17
,
20
,
21
,
22
],
[
12
,
13
,
14
,
17
,
18
,
19
,
22
,
23
,
24
],
[
25
,
26
,
27
,
30
,
31
,
32
,
35
,
36
,
37
],
[
27
,
28
,
29
,
32
,
33
,
34
,
37
,
38
,
39
],
[
35
,
36
,
37
,
40
,
41
,
42
,
45
,
46
,
47
],
[
37
,
38
,
39
,
42
,
43
,
44
,
47
,
48
,
49
],
[
50
,
51
,
52
,
55
,
56
,
57
,
60
,
61
,
62
],
[
52
,
53
,
54
,
57
,
58
,
59
,
62
,
63
,
64
],
[
60
,
61
,
62
,
65
,
66
,
67
,
70
,
71
,
72
],
[
62
,
63
,
64
,
67
,
68
,
69
,
72
,
73
,
74
],
[
75
,
76
,
77
,
80
,
81
,
82
,
85
,
86
,
87
],
[
77
,
78
,
79
,
82
,
83
,
84
,
87
,
88
,
89
],
[
85
,
86
,
87
,
90
,
91
,
92
,
95
,
96
,
97
],
[
87
,
88
,
89
,
92
,
93
,
94
,
97
,
98
,
99
],
[
100
,
101
,
102
,
105
,
106
,
107
,
110
,
111
,
112
],
[
102
,
103
,
104
,
107
,
108
,
109
,
112
,
113
,
114
],
[
110
,
111
,
112
,
115
,
116
,
117
,
120
,
121
,
122
],
[
112
,
113
,
114
,
117
,
118
,
119
,
122
,
123
,
124
],
[
125
,
126
,
127
,
130
,
131
,
132
,
135
,
136
,
137
],
[
127
,
128
,
129
,
132
,
133
,
134
,
137
,
138
,
139
],
[
135
,
136
,
137
,
140
,
141
,
142
,
145
,
146
,
147
],
[
137
,
138
,
139
,
142
,
143
,
144
,
147
,
148
,
149
]])
# neibs2images do not seam to support step != neib_shape
# g = function([], neibs2images(neibs, neib_shape, images.shape),
# mode=self.mode)
# print g()
#assert numpy.allclose(images.get_value(borrow=True), g())
#
assert numpy.allclose(images.get_value(borrow=True), g())
def
test_neibs_bad_shape
(
self
):
shape
=
(
2
,
3
,
10
,
10
)
...
...
@@ -168,36 +164,36 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_neibs_wrap_centered_step_manual
(
self
):
expected1
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
21
,
22
,
23
,
1
,
2
,
3
,
6
,
7
,
8
],
[
23
,
24
,
20
,
3
,
4
,
0
,
8
,
9
,
5
],
[
9
,
5
,
6
,
14
,
10
,
11
,
19
,
15
,
16
],
[
6
,
7
,
8
,
11
,
12
,
13
,
16
,
17
,
18
],
[
8
,
9
,
5
,
13
,
14
,
10
,
18
,
19
,
15
],
[
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
],
[
16
,
17
,
18
,
21
,
22
,
23
,
1
,
2
,
3
],
[
18
,
19
,
15
,
23
,
24
,
20
,
3
,
4
,
0
]]
expected2
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
22
,
23
,
24
,
2
,
3
,
4
,
7
,
8
,
9
],
[
14
,
10
,
11
,
19
,
15
,
16
,
24
,
20
,
21
],
[
12
,
13
,
14
,
17
,
18
,
19
,
22
,
23
,
24
]]
expected3
=
[[
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
,
14
,
10
,
11
],
[
17
,
18
,
19
,
22
,
23
,
24
,
2
,
3
,
4
,
7
,
8
,
9
,
12
,
13
,
14
],
[
9
,
5
,
6
,
14
,
10
,
11
,
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
],
[
7
,
8
,
9
,
12
,
13
,
14
,
17
,
18
,
19
,
22
,
23
,
24
,
2
,
3
,
4
]]
expected4
=
[[
23
,
24
,
20
,
21
,
22
,
3
,
4
,
0
,
1
,
2
,
8
,
9
,
5
,
6
,
7
],
[
21
,
22
,
23
,
24
,
20
,
1
,
2
,
3
,
4
,
0
,
6
,
7
,
8
,
9
,
5
],
expected1
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
21
,
22
,
23
,
1
,
2
,
3
,
6
,
7
,
8
],
[
23
,
24
,
20
,
3
,
4
,
0
,
8
,
9
,
5
],
[
9
,
5
,
6
,
14
,
10
,
11
,
19
,
15
,
16
],
[
6
,
7
,
8
,
11
,
12
,
13
,
16
,
17
,
18
],
[
8
,
9
,
5
,
13
,
14
,
10
,
18
,
19
,
15
],
[
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
],
[
16
,
17
,
18
,
21
,
22
,
23
,
1
,
2
,
3
],
[
18
,
19
,
15
,
23
,
24
,
20
,
3
,
4
,
0
]]
expected2
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
22
,
23
,
24
,
2
,
3
,
4
,
7
,
8
,
9
],
[
14
,
10
,
11
,
19
,
15
,
16
,
24
,
20
,
21
],
[
12
,
13
,
14
,
17
,
18
,
19
,
22
,
23
,
24
]]
expected3
=
[[
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
,
14
,
10
,
11
],
[
17
,
18
,
19
,
22
,
23
,
24
,
2
,
3
,
4
,
7
,
8
,
9
,
12
,
13
,
14
],
[
9
,
5
,
6
,
14
,
10
,
11
,
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
],
[
7
,
8
,
9
,
12
,
13
,
14
,
17
,
18
,
19
,
22
,
23
,
24
,
2
,
3
,
4
]]
expected4
=
[[
23
,
24
,
20
,
21
,
22
,
3
,
4
,
0
,
1
,
2
,
8
,
9
,
5
,
6
,
7
],
[
21
,
22
,
23
,
24
,
20
,
1
,
2
,
3
,
4
,
0
,
6
,
7
,
8
,
9
,
5
],
[
13
,
14
,
10
,
11
,
12
,
18
,
19
,
15
,
16
,
17
,
23
,
24
,
20
,
21
,
22
],
[
11
,
12
,
13
,
14
,
10
,
16
,
17
,
18
,
19
,
15
,
21
,
22
,
23
,
24
,
20
]]
expected5
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
22
,
23
,
24
,
2
,
3
,
4
,
7
,
8
,
9
],
[
9
,
5
,
6
,
14
,
10
,
11
,
19
,
15
,
16
],
[
7
,
8
,
9
,
12
,
13
,
14
,
17
,
18
,
19
],
[
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
],
[
17
,
18
,
19
,
22
,
23
,
24
,
2
,
3
,
4
]]
expected6
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
21
,
22
,
23
,
1
,
2
,
3
,
6
,
7
,
8
],
[
23
,
24
,
20
,
3
,
4
,
0
,
8
,
9
,
5
],
expected5
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
22
,
23
,
24
,
2
,
3
,
4
,
7
,
8
,
9
],
[
9
,
5
,
6
,
14
,
10
,
11
,
19
,
15
,
16
],
[
7
,
8
,
9
,
12
,
13
,
14
,
17
,
18
,
19
],
[
19
,
15
,
16
,
24
,
20
,
21
,
4
,
0
,
1
],
[
17
,
18
,
19
,
22
,
23
,
24
,
2
,
3
,
4
]]
expected6
=
[[
24
,
20
,
21
,
4
,
0
,
1
,
9
,
5
,
6
],
[
21
,
22
,
23
,
1
,
2
,
3
,
6
,
7
,
8
],
[
23
,
24
,
20
,
3
,
4
,
0
,
8
,
9
,
5
],
[
14
,
10
,
11
,
19
,
15
,
16
,
24
,
20
,
21
],
[
11
,
12
,
13
,
16
,
17
,
18
,
21
,
22
,
23
],
[
13
,
14
,
10
,
18
,
19
,
15
,
23
,
24
,
20
]]
...
...
@@ -212,8 +208,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
[(
80
,
90
,
5
,
5
),
(
3
,
3
),
(
2
,
3
),
expected5
],
[(
1025
,
9
,
5
,
5
),
(
3
,
3
),
(
3
,
2
),
expected6
],
[(
1
,
1
,
5
,
1035
),
(
3
,
3
),
(
3
,
3
),
None
],
[(
1
,
1
,
1045
,
5
),
(
3
,
3
),
(
3
,
3
),
None
],
]
):
[(
1
,
1
,
1045
,
5
),
(
3
,
3
),
(
3
,
3
),
None
],
]
):
for
dtype
in
self
.
dtypes
:
...
...
@@ -231,16 +227,15 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
if
expected
.
size
>
1
:
for
i
in
range
(
shape
[
0
]
*
shape
[
1
]):
assert
numpy
.
allclose
(
neibs
[
i
*
expected
.
shape
[
0
]:
(
i
+
1
)
*
expected
.
shape
[
0
],
:],
expected
+
25
*
i
),
"wrap_centered"
neibs
[
i
*
expected
.
shape
[
0
]:(
i
+
1
)
*
expected
.
shape
[
0
],
:],
expected
+
25
*
i
),
"wrap_centered"
assert
self
.
op
in
[
type
(
node
.
op
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
#g = function([], neibs2images(neibs, neib_shape, images.shape), mode=self.mode)
#
g = function([], neibs2images(neibs, neib_shape, images.shape), mode=self.mode)
# TODO: why this is commented?
#assert numpy.allclose(images.get_value(borrow=True), g())
#
assert numpy.allclose(images.get_value(borrow=True), g())
def
test_neibs_bad_shape_wrap_centered
(
self
):
shape
=
(
2
,
3
,
10
,
10
)
...
...
@@ -309,7 +304,6 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
unittest_tools
.
verify_grad
(
fn
,
[
images_val
],
mode
=
self
.
mode
,
eps
=
0.1
)
def
test_grad_ignore_border
(
self
):
shape
=
(
2
,
3
,
5
,
5
)
images_val
=
numpy
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
...
...
@@ -375,62 +369,41 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
shape
=
(
100
,
40
,
6
,
3
)
images
=
numpy
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
f
=
self
.
_compile_and_check
([
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'valid'
)],
[
images
],
Images2Neibs
)
f
=
self
.
_compile_and_check
([
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'valid'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'valid'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'valid'
)],
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
5
,
4
)
images
=
numpy
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
f
=
self
.
_compile_and_check
([
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
5
,
3
)
images
=
numpy
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
f
=
self
.
_compile_and_check
([
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
6
,
7
)
images
=
numpy
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
f
=
self
.
_compile_and_check
([
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
2
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
2
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
5
,
10
)
images
=
numpy
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
f
=
self
.
_compile_and_check
([
x
],
[
images2neibs
(
x
,
neib_shape
=
(
3
,
3
),
mode
=
'wrap_centered'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
3
,
3
),
mode
=
'wrap_centered'
)],
[
images
],
Images2Neibs
)
if
__name__
==
'__main__'
:
unittest
.
main
()
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
d5944c96
...
...
@@ -25,7 +25,7 @@ from theano.tensor.nnet import (categorical_crossentropy,
CrossentropyCategorical1HotGrad
,
sigmoid
,
softplus
,
Softmax
,
softmax
,
softmax_op
,
softmax_graph
,
SoftmaxWithBias
,
softmax_with_bias
,
LogSoftmax
,
logsoftmax_op
,
softmax_with_bias
,
logsoftmax_op
,
softmax_grad
,
SoftmaxGrad
,
Prepend_scalar_constant_to_each_row
,
Prepend_scalar_to_each_row
,
...
...
@@ -240,7 +240,7 @@ class T_LogSoftmax(utt.InferShapeTester):
# while in the log-softmax case they don't
f3
=
theano
.
function
([
x
,
y
],
[
grad
])
grad_
=
f3
(
a
,
b
)
assert
n
umpy
.
all
(
numpy
.
isnan
(
grad_
)
==
False
)
assert
n
ot
numpy
.
any
(
numpy
.
isnan
(
grad_
)
)
def
test_isclose
(
self
):
def
f
(
a
):
...
...
@@ -372,10 +372,11 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
10
)
self
.
_compile_and_check
([
advec
,
admat
,
alvec
],
[
CrossentropySoftmax1HotWithBiasDx
()(
advec
,
admat
,
alvec
)],
[
advec_val
,
admat_val
,
alvec_val
],
CrossentropySoftmax1HotWithBiasDx
)
self
.
_compile_and_check
(
[
advec
,
admat
,
alvec
],
[
CrossentropySoftmax1HotWithBiasDx
()(
advec
,
admat
,
alvec
)],
[
advec_val
,
admat_val
,
alvec_val
],
CrossentropySoftmax1HotWithBiasDx
)
def
test_neg_idx
(
self
):
admat
=
matrix
()
...
...
@@ -417,9 +418,10 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
def
grad_on_softmax
(
x
,
b
):
return
self
.
op
(
x
,
b
,
y_idx
=
numpy
.
random
.
randint
(
low
=
0
,
high
=
n_classes
,
size
=
n_samples
))[
1
]
utt
.
verify_grad
(
grad_on_softmax
,
[
numpy
.
random
.
rand
(
n_samples
,
n_classes
),
numpy
.
random
.
rand
(
n_classes
)])
utt
.
verify_grad
(
grad_on_softmax
,
[
numpy
.
random
.
rand
(
n_samples
,
n_classes
),
numpy
.
random
.
rand
(
n_classes
)])
def
test_infer_shape
(
self
):
admat
=
matrix
()
...
...
@@ -429,10 +431,11 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
self
.
_compile_and_check
([
admat
,
advec
,
alvec
],
CrossentropySoftmaxArgmax1HotWithBias
()(
admat
,
advec
,
alvec
),
[
admat_val
,
advec_val
,
alvec_val
],
CrossentropySoftmaxArgmax1HotWithBias
)
self
.
_compile_and_check
(
[
admat
,
advec
,
alvec
],
CrossentropySoftmaxArgmax1HotWithBias
()(
admat
,
advec
,
alvec
),
[
admat_val
,
advec_val
,
alvec_val
],
CrossentropySoftmaxArgmax1HotWithBias
)
def
test_neg_idx
(
self
):
admat
=
matrix
()
...
...
@@ -475,15 +478,17 @@ class T_prepend(utt.InferShapeTester):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
adscal_val
=
numpy
.
asarray
(
rng
.
rand
(),
dtype
=
config
.
floatX
)
.
item
()
self
.
_compile_and_check
([
admat
],
[
Prepend_scalar_constant_to_each_row
(
adscal_val
)(
admat
)],
[
admat_val
],
Prepend_scalar_constant_to_each_row
)
self
.
_compile_and_check
(
[
admat
],
[
Prepend_scalar_constant_to_each_row
(
adscal_val
)(
admat
)],
[
admat_val
],
Prepend_scalar_constant_to_each_row
)
self
.
_compile_and_check
([
adscal
,
admat
],
[
Prepend_scalar_to_each_row
()(
adscal
,
admat
)],
[
adscal_val
,
admat_val
],
Prepend_scalar_to_each_row
)
self
.
_compile_and_check
(
[
adscal
,
admat
],
[
Prepend_scalar_to_each_row
()(
adscal
,
admat
)],
[
adscal_val
,
admat_val
],
Prepend_scalar_to_each_row
)
class
T_CrossentropyCategorical1HotGrad
(
utt
.
InferShapeTester
):
...
...
@@ -496,10 +501,11 @@ class T_CrossentropyCategorical1HotGrad(utt.InferShapeTester):
advec_val
=
rng
.
rand
(
3
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
alvec_val
=
[
0
,
1
,
0
]
self
.
_compile_and_check
([
advec
,
admat
,
alvec
],
[
CrossentropyCategorical1HotGrad
()(
advec
,
admat
,
alvec
)],
[
advec_val
,
admat_val
,
alvec_val
],
CrossentropyCategorical1HotGrad
)
self
.
_compile_and_check
(
[
advec
,
admat
,
alvec
],
[
CrossentropyCategorical1HotGrad
()(
advec
,
admat
,
alvec
)],
[
advec_val
,
admat_val
,
alvec_val
],
CrossentropyCategorical1HotGrad
)
class
T_CrossentropyCategorical1Hot
(
utt
.
InferShapeTester
):
...
...
@@ -510,8 +516,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
op
=
crossentropy_categorical_1hot
xe
=
op
(
x
,
one_of_n
)
f
=
theano
.
function
([
x
,
one_of_n
],
xe
)
x_val
=
numpy
.
asarray
([[
.
4
,
.
6
,
.
0
],
[
.
1
,
.
8
,
.
1
]],
dtype
=
config
.
floatX
)
x_val
=
numpy
.
asarray
(
[[
.
4
,
.
6
,
.
0
],
[
.
1
,
.
8
,
.
1
]],
dtype
=
config
.
floatX
)
xe_val
=
f
(
x_val
,
[
0
,
1
])
assert
numpy
.
allclose
(
xe_val
,
-
numpy
.
log
([
.
4
,
.
8
]))
...
...
@@ -526,24 +533,25 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
alvec_val
=
[
0
,
1
,
0
]
self
.
_compile_and_check
([
admat
,
alvec
],
[
CrossentropyCategorical1Hot
()(
admat
,
alvec
)],
[
admat_val
,
alvec_val
],
CrossentropyCategorical1Hot
)
self
.
_compile_and_check
(
[
admat
,
alvec
],
[
CrossentropyCategorical1Hot
()(
admat
,
alvec
)],
[
admat_val
,
alvec_val
],
CrossentropyCategorical1Hot
)
def
test_softmax_optimizations
(
self
):
x
=
tensor
.
matrix
(
'x'
)
one_of_n
=
tensor
.
lvector
(
'one_of_n'
)
op
=
crossentropy_categorical_1hot
xe
=
op
(
x
,
one_of_n
)
#
xe = op(x, one_of_n)
fgraph
=
gof
.
FunctionGraph
(
[
x
,
one_of_n
],
[
op
(
softmax_op
(
x
),
one_of_n
)])
[
x
,
one_of_n
],
[
op
(
softmax_op
(
x
),
one_of_n
)])
assert
fgraph
.
outputs
[
0
]
.
owner
.
op
==
op
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
assert
str
(
fgraph
.
outputs
[
0
]
.
owner
.
op
)
==
'OutputGuard'
assert
(
fgraph
.
outputs
[
0
]
.
owner
.
inputs
[
0
]
.
owner
.
op
==
...
...
@@ -554,12 +562,12 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
one_of_n
=
tensor
.
lvector
(
'one_of_n'
)
op
=
crossentropy_categorical_1hot
fgraph
=
gof
.
FunctionGraph
(
[
x
,
one_of_n
],
[
op
(
softmax_op
(
x
),
one_of_n
)])
[
x
,
one_of_n
],
[
op
(
softmax_op
(
x
),
one_of_n
)])
assert
fgraph
.
outputs
[
0
]
.
owner
.
op
==
op
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
assert
str
(
fgraph
.
outputs
[
0
]
.
owner
.
op
)
==
'OutputGuard'
assert
(
fgraph
.
outputs
[
0
]
.
owner
.
inputs
[
0
]
.
owner
.
op
==
crossentropy_softmax_argmax_1hot_with_bias
)
...
...
@@ -569,11 +577,11 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
b
=
tensor
.
vector
(
'b'
)
one_of_n
=
tensor
.
lvector
(
'one_of_n'
)
op
=
crossentropy_categorical_1hot
xe
=
op
(
x
,
one_of_n
)
#
xe = op(x, one_of_n)
fgraph
=
gof
.
FunctionGraph
(
[
x
,
b
,
one_of_n
],
[
op
(
softmax_op
(
x
+
b
),
one_of_n
)])
[
x
,
b
,
one_of_n
],
[
op
(
softmax_op
(
x
+
b
),
one_of_n
)])
assert
fgraph
.
outputs
[
0
]
.
owner
.
op
==
op
# print 'BEFORE'
...
...
@@ -583,7 +591,7 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# print '----'
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
# print 'AFTER'
# for node in fgraph.toposort():
...
...
@@ -604,8 +612,8 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
op
=
crossentropy_categorical_1hot
fgraph
=
gof
.
FunctionGraph
(
[
x
,
b
,
c
,
one_of_n
],
[
op
(
softmax_op
(
T
.
add
(
x
,
b
,
c
)),
one_of_n
)])
[
x
,
b
,
c
,
one_of_n
],
[
op
(
softmax_op
(
T
.
add
(
x
,
b
,
c
)),
one_of_n
)])
assert
fgraph
.
outputs
[
0
]
.
owner
.
op
==
op
# print 'BEFORE'
...
...
@@ -614,7 +622,7 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# print '----'
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
# print 'AFTER'
# for node in fgraph.toposort():
...
...
@@ -632,8 +640,8 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
one_of_n
=
tensor
.
lvector
(
'one_of_n'
)
op
=
crossentropy_categorical_1hot
fgraph
=
gof
.
FunctionGraph
(
[
x
,
b
,
one_of_n
],
[
op
(
softmax_op
(
x
+
b
),
one_of_n
)])
[
x
,
b
,
one_of_n
],
[
op
(
softmax_op
(
x
+
b
),
one_of_n
)])
assert
fgraph
.
outputs
[
0
]
.
owner
.
op
==
op
# print 'BEFORE'
# for node in fgraph.toposort():
...
...
@@ -642,7 +650,7 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# print '----'
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
# print 'AFTER'
# for node in fgraph.toposort():
# print node.op
...
...
@@ -660,8 +668,8 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
sum_xe
=
tensor
.
sum
(
xe
)
g_x
=
tensor
.
grad
(
sum_xe
,
x
)
fgraph
=
gof
.
FunctionGraph
(
[
x
,
one_of_n
],
[
g_x
])
[
x
,
one_of_n
],
[
g_x
])
assert
check_stack_trace
(
fgraph
,
ops_to_check
=
[
crossentropy_softmax_1hot_with_bias_dx
,
softmax_op
])
...
...
@@ -671,7 +679,7 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# print node.op, node.inputs
# print '----'
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
# print 'AFTER'
# for node in fgraph.toposort():
...
...
@@ -703,15 +711,15 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
sum_xe
=
tensor
.
sum
(
xe
)
g_x
=
tensor
.
grad
(
sum_xe
,
x
)
fgraph
=
gof
.
FunctionGraph
(
[
x
,
one_of_n
],
[
g_x
])
[
x
,
one_of_n
],
[
g_x
])
# print 'BEFORE'
# for node in fgraph.toposort():
# print node.op, node.inputs
# print '----'
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
# print 'AFTER'
# for node in fgraph.toposort():
...
...
@@ -752,11 +760,11 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Basic case
expressions
=
[
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]
),
T
.
sum
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])
]
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])
),
-
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])
]
for
expr
in
expressions
:
# Verify the optimizer worked on the expressions
f
=
theano
.
function
([
x
,
y
],
expr
,
mode
=
mode
)
...
...
@@ -796,10 +804,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Test that a biased softmax is optimized correctly
bias_expressions
=
[
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
for
expr
in
bias_expressions
:
f
=
theano
.
function
([
x
,
b
,
y
],
expr
,
mode
=
mode
)
...
...
@@ -835,10 +843,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Test that using "mean" instead of sum works, too
mean_expressions
=
[
T
.
mean
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
mean
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
T
.
mean
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
mean
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
for
expr
in
mean_expressions
:
f
=
theano
.
function
([
x
,
y
],
expr
,
mode
=
mode
)
...
...
@@ -867,7 +875,7 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
try
:
ops
=
[
node
.
op
for
node
in
g
.
maker
.
fgraph
.
toposort
()]
assert
len
(
ops
)
==
5
#there's an extra dimshuffle in there
#
there's an extra dimshuffle in there
# but I can't think of a good rule to get rid of it
assert
crossentropy_softmax_1hot_with_bias_dx
in
ops
assert
softmax_op
in
ops
...
...
@@ -878,10 +886,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
raise
mean_bias_expressions
=
[
T
.
mean
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
mean
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
T
.
mean
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
mean
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
mean
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
for
expr
in
mean_bias_expressions
:
f
=
theano
.
function
([
x
,
b
,
y
],
expr
,
mode
=
mode
)
...
...
@@ -928,11 +936,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
y
=
T
.
lvector
(
'y'
)
yi
=
T
.
cast
(
y
,
'int32'
)
expressions
=
[
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
yi
.
shape
[
0
]),
yi
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
yi
.
shape
[
0
]),
yi
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
yi
.
shape
[
0
]),
yi
]),
T
.
sum
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
yi
.
shape
[
0
]),
yi
])
]
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
yi
.
shape
[
0
]),
yi
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
yi
.
shape
[
0
]),
yi
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
yi
.
shape
[
0
]),
yi
]),
T
.
sum
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
yi
.
shape
[
0
]),
yi
])]
for
expr
in
expressions
:
# Verify the optimizer worked on the expressions
...
...
@@ -979,8 +986,8 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Test that a biased softmax is optimized correctly
bias_expressions
=
[
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
]))]
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
]))]
for
expr
in
bias_expressions
:
f
=
theano
.
function
([
x
,
y
],
expr
,
mode
=
mode
)
...
...
@@ -1026,10 +1033,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Test that a biased softmax is optimized correctly
bias_expressions
=
[
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
for
expr
in
bias_expressions
:
f
=
theano
.
function
([
x
,
b
,
y
],
expr
,
mode
=
mode
)
...
...
@@ -1087,10 +1094,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Test that a biased softmax is optimized correctly
bias_expressions
=
[
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
for
expr
in
bias_expressions
:
f
=
theano
.
function
([
x
,
b
,
y_
],
expr
,
mode
=
mode
)
...
...
@@ -1149,10 +1156,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Test that a biased softmax is optimized correctly
bias_expressions
=
[
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
T
.
sum
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
b
+
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
T
.
sum
(
T
.
log
(
softmax
(
x
+
b
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
T
.
sum
(
-
T
.
log
(
softmax
(
b
+
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])]
for
expr
in
bias_expressions
:
f
=
theano
.
function
([
x
,
b
,
y_
],
expr
,
mode
=
mode
)
...
...
@@ -1235,26 +1242,25 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# Cases to test
expressions
=
[
a
*
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
(
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
]))),
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
sum
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
-
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
a
*
(
-
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
a
*
T
.
mean
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
(
-
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
]))),
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
mean
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
-
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
a
*
(
-
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
]
a
*
T
.
sum
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
(
-
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
]))),
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
sum
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
-
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
a
*
(
-
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
sum
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
a
*
T
.
mean
(
-
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
-
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
(
-
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
]))),
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
mean
(
-
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
-
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
a
*
(
-
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
a
*
T
.
mean
(
T
.
log
(
softmax
(
x
))[
T
.
arange
(
y
.
shape
[
0
]),
y
]),
]
for
expr
in
expressions
:
# Verify the optimizer worked on the expressions
...
...
@@ -1278,8 +1284,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
raise
# Verify the gradient when providing output gradient
h
=
theano
.
function
([
x
,
y
,
a
],
T
.
grad
(
expr
,
x
,
known_grads
=
{
expr
:
a
*
x
.
sum
()}),
mode
=
mode
)
h
=
theano
.
function
(
[
x
,
y
,
a
],
T
.
grad
(
expr
,
x
,
known_grads
=
{
expr
:
a
*
x
.
sum
()}),
mode
=
mode
)
try
:
assert
6
<=
len
(
h
.
maker
.
fgraph
.
toposort
())
<=
8
validate_grad_graph
(
h
)
...
...
@@ -1294,17 +1301,17 @@ def test_argmax_pushdown():
for
sm
in
[
softmax_graph
,
softmax_op
]:
# test that the max_and_argmax is pushed down if the max is not used
out
=
tensor
.
max_and_argmax
(
sm
(
tensor
.
exp
(
tensor
.
tanh
(
sigmoid
(
x
)))),
axis
=-
1
)[
1
]
sm
(
tensor
.
exp
(
tensor
.
tanh
(
sigmoid
(
x
)))),
axis
=-
1
)[
1
]
fgraph
=
gof
.
FunctionGraph
(
[
x
],
[
out
])
[
x
],
[
out
])
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
# print 'AFTER'
# for node in fgraph.toposort():
# print node.op
# print node.op
assert
len
(
fgraph
.
toposort
())
==
2
# an output_guard is second
assert
fgraph
.
toposort
()[
0
]
.
op
==
tensor
.
basic
.
_max_and_argmax
assert
str
(
fgraph
.
toposort
()[
1
]
.
op
)
==
'OutputGuard'
...
...
@@ -1313,12 +1320,13 @@ def test_argmax_pushdown():
x
=
tensor
.
matrix
()
# test that the max_and_argmax is not pushed down if the max is used
out
=
tensor
.
max_and_argmax
(
sm
(
tensor
.
exp
(
tensor
.
tanh
(
sigmoid
(
x
)))),
axis
=-
1
)[
0
]
sm
(
tensor
.
exp
(
tensor
.
tanh
(
sigmoid
(
x
)))),
axis
=-
1
)[
0
]
fgraph
=
gof
.
FunctionGraph
(
[
x
],
[
out
])
[
x
],
[
out
])
assert
hasattr
(
fgraph
.
outputs
[
0
]
.
tag
,
'trace'
)
backup
=
config
.
warn
.
argmax_pushdown_bug
config
.
warn
.
argmax_pushdown_bug
=
False
try
:
...
...
@@ -1344,11 +1352,11 @@ def test_argmax_pushdown_bias():
out
=
tensor
.
argmax
(
softmax_with_bias
(
x
,
b
),
axis
=-
1
)
fgraph
=
gof
.
FunctionGraph
(
[
x
,
b
],
[
out
])
[
x
,
b
],
[
out
])
theano
.
compile
.
mode
.
optdb
.
query
(
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
theano
.
compile
.
mode
.
OPT_FAST_RUN
)
.
optimize
(
fgraph
)
# print 'AFTER'
# for node in fgraph.toposort():
...
...
@@ -1364,8 +1372,8 @@ def test_argmax_pushdown_bias():
b
=
tensor
.
vector
()
out
=
tensor
.
max_and_argmax
(
softmax_with_bias
(
x
,
b
),
axis
=-
1
)[
0
]
fgraph
=
gof
.
FunctionGraph
(
[
x
,
b
],
[
out
])
[
x
,
b
],
[
out
])
backup
=
config
.
warn
.
argmax_pushdown_bug
config
.
warn
.
argmax_pushdown_bug
=
False
...
...
@@ -1514,14 +1522,14 @@ class Test_softmax_opt:
p_y
=
T
.
exp
(
c
)
/
T
.
exp
(
c
)
.
sum
(
axis
=
0
)
# test that function contains softmax and no div.
f
=
theano
.
function
([
c
],
p_y
)
theano
.
function
([
c
],
p_y
)
# printing.debugprint(f)
# test that function contains softmax and no div.
backup
=
config
.
warn
.
sum_div_dimshuffle_bug
config
.
warn
.
sum_div_dimshuffle_bug
=
False
try
:
g
=
theano
.
function
([
c
],
T
.
grad
(
p_y
.
sum
(),
c
))
theano
.
function
([
c
],
T
.
grad
(
p_y
.
sum
(),
c
))
finally
:
config
.
warn
.
sum_div_dimshuffle_bug
=
backup
# printing.debugprint(g)
...
...
@@ -1533,14 +1541,14 @@ class Test_softmax_opt:
p_y
=
T
.
exp
(
c
)
/
T
.
exp
(
c
)
.
sum
()
# test that function contains softmax and no div.
f
=
theano
.
function
([
c
],
p_y
)
theano
.
function
([
c
],
p_y
)
# printing.debugprint(f)
# test that function contains softmax and no div.
backup
=
config
.
warn
.
sum_div_dimshuffle_bug
config
.
warn
.
sum_div_dimshuffle_bug
=
False
try
:
g
=
theano
.
function
([
c
],
T
.
grad
(
p_y
.
sum
(),
c
))
theano
.
function
([
c
],
T
.
grad
(
p_y
.
sum
(),
c
))
finally
:
config
.
warn
.
sum_div_dimshuffle_bug
=
backup
# printing.debugprint(g)
...
...
@@ -1614,15 +1622,14 @@ def test_relu():
dtype
=
config
.
floatX
)
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
,
alpha
:
A
})
assert
numpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
A
*
X
),
rtol
=
3e-5
)
# test that for alpha of ndarray don't cause upcast.
x
=
matrix
(
'x'
,
dtype
=
'float32'
)
rng
=
numpy
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
'float32'
)
alpha
=
numpy
.
asarray
(
.
123
,
dtype
=
'float32'
)
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
assert
numpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
y
.
dtype
==
'float32'
# test that for alpha of ndarray don't cause upcast.
x
=
matrix
(
'x'
,
dtype
=
'float32'
)
rng
=
numpy
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
'float32'
)
alpha
=
numpy
.
asarray
(
.
123
,
dtype
=
'float32'
)
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
assert
numpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
y
.
dtype
==
'float32'
def
test_h_softmax
():
...
...
@@ -1698,7 +1705,7 @@ def test_h_softmax():
# Verifies that the outputs computed by fun_output_tg are the same as those
# computed by fun_output.
utt
.
assert_allclose
(
all_outputs
[
numpy
.
arange
(
0
,
batch_size
),
y_mat
],
tg_output
)
all_outputs
[
numpy
.
arange
(
0
,
batch_size
),
y_mat
],
tg_output
)
def
test_elu
():
...
...
@@ -1733,7 +1740,7 @@ def test_binary_crossentropy_reshape():
SoftsignTester
=
makeBroadcastTester
(
op
=
softsign
,
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
inputs
,
inputs
/
(
1.0
+
numpy
.
fabs
(
inputs
)))),
inputs
,
inputs
/
(
1.0
+
numpy
.
fabs
(
inputs
)))),
good
=
_good_broadcast_unary_normal_float_no_complex
,
name
=
'SoftsignTester'
,
)
theano/tensor/nnet/tests/test_sigm.py
浏览文件 @
d5944c96
...
...
@@ -16,7 +16,7 @@ from theano.tensor.nnet.sigm import (
compute_mul
,
is_1pexp
,
parse_mul_tree
,
perform_sigm_times_exp
,
register_local_1msigmoid
,
simplify_mul
,
)
from
theano.tensor.tests.test_basic
import
(
makeBroadcastTester
,
rand
,
from
theano.tensor.tests.test_basic
import
(
makeBroadcastTester
,
check_floatX
,
upcast_int8_nfunc
,
_good_broadcast_unary_normal_no_complex
)
...
...
@@ -45,7 +45,7 @@ UltraFastSigmoidTester = makeBroadcastTester(
good
=
_good_broadcast_unary_normal_no_complex
,
# grad=_grad_broadcast_unary_normal,
name
=
'UltraFastSigmoidTester'
,
# This is an approx of the sigmoid. That is why we raise eps
# This is an approx of the sigmoid. That is why we raise eps
eps
=
5e-2
)
HardSigmoidTester
=
makeBroadcastTester
(
...
...
@@ -55,7 +55,7 @@ HardSigmoidTester = makeBroadcastTester(
good
=
_good_broadcast_unary_normal_no_complex
,
# grad=_grad_broadcast_unary_normal,
name
=
'HardSigmoidTester'
,
# This is an approx of the sigmoid. That is why we raise eps
# This is an approx of the sigmoid. That is why we raise eps
eps
=
1e-1
)
...
...
@@ -146,20 +146,20 @@ class T_sigmoid_opts(unittest.TestCase):
# todo: solve issue #4589 first
# assert check_stack_trace(
# f, ops_to_check=[sigmoid, theano.tensor.inplace.neg_inplace])
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
([
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
])
f
(
data
)
f
=
theano
.
function
([
x
],
T
.
fill
(
x
,
-
1.0
)
/
(
1
-
T
.
exp
(
-
x
)),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
f
=
theano
.
function
([
x
],
T
.
fill
(
x
,
-
1.0
)
/
(
2
+
T
.
exp
(
-
x
)),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
f
=
theano
.
function
([
x
],
T
.
fill
(
x
,
-
1.1
)
/
(
1
+
T
.
exp
(
-
x
)),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
# tests double inv_1_plus_exp with neg
...
...
@@ -170,33 +170,33 @@ class T_sigmoid_opts(unittest.TestCase):
((
1
+
T
.
exp
(
x
))
*
(
1
+
T
.
exp
(
-
x
))),
mode
=
m
)
# todo: solve issue #4589 first
# assert check_stack_trace(f, ops_to_check=[sigmoid, T.mul])
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
sigmoid
,
T
.
mul
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
sigmoid
,
T
.
mul
]
)
f
(
data
)
f
=
theano
.
function
([
x
],
(
T
.
fill
(
x
,
-
1.1
)
*
T
.
exp
(
x
))
/
((
1
+
T
.
exp
(
x
))
*
(
1
+
T
.
exp
(
-
x
))),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
f
=
theano
.
function
([
x
],
(
T
.
fill
(
x
,
-
1.0
)
*
T
.
exp
(
x
))
/
((
2
+
T
.
exp
(
x
))
*
(
1
+
T
.
exp
(
-
x
))),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
f
=
theano
.
function
([
x
],
(
T
.
fill
(
x
,
-
1.0
)
*
T
.
exp
(
x
))
/
((
1
+
T
.
exp
(
x
))
*
(
2
+
T
.
exp
(
-
x
))),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
f
=
theano
.
function
([
x
],
(
T
.
fill
(
x
,
-
1.0
)
*
T
.
exp
(
x
))
/
((
1
+
T
.
exp
(
x
))
*
(
1
+
T
.
exp
(
x
))),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
f
=
theano
.
function
([
x
],
(
T
.
fill
(
x
,
-
1.0
)
*
T
.
exp
(
x
))
/
((
1
+
T
.
exp
(
x
))
*
(
2
+
T
.
exp
(
-
x
))),
mode
=
m
)
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
!=
[
sigmoid
,
T
.
mul
,
theano
.
tensor
.
inplace
.
neg_inplace
]
)
f
(
data
)
finally
:
...
...
@@ -219,8 +219,8 @@ class T_sigmoid_opts(unittest.TestCase):
# tests inv_1_plus_exp
f
=
theano
.
function
([
x
],
1
-
T
.
fill
(
x
,
1.0
)
/
(
1
+
T
.
exp
(
-
x
)),
mode
=
m
)
assert
check_stack_trace
(
f
,
ops_to_check
=
[
tensor
.
neg
,
sigmoid_inplace
])
assert
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
tensor
.
neg
,
sigmoid_inplace
]
assert
(
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
==
[
tensor
.
neg
,
sigmoid_inplace
]
)
def
test_local_sigm_times_exp
(
self
):
"""
...
...
@@ -247,10 +247,9 @@ class T_sigmoid_opts(unittest.TestCase):
# assert check_stack_trace(f, ops_to_check=sigmoid)
f
=
theano
.
function
(
[
x
,
y
],
(
sigmoid
(
x
)
*
sigmoid
(
-
y
)
*
-
tensor
.
exp
(
-
x
)
*
tensor
.
exp
(
x
*
y
)
*
tensor
.
exp
(
y
)),
mode
=
m
)
[
x
,
y
],
(
sigmoid
(
x
)
*
sigmoid
(
-
y
)
*
-
tensor
.
exp
(
-
x
)
*
tensor
.
exp
(
x
*
y
)
*
tensor
.
exp
(
y
)),
mode
=
m
)
match
(
f
,
[
sigmoid
,
tensor
.
mul
,
tensor
.
neg
,
tensor
.
exp
,
sigmoid
,
tensor
.
mul
])
# assert check_stack_trace(f, ops_to_check=[sigmoid, tensor.mul,
...
...
@@ -271,8 +270,8 @@ class T_sigmoid_opts(unittest.TestCase):
perform_sigm_times_exp
(
trees
[
0
])
trees
[
0
]
=
simplify_mul
(
trees
[
0
])
good
=
theano
.
gof
.
graph
.
is_same_graph
(
compute_mul
(
trees
[
0
]),
compute_mul
(
trees
[
1
]))
compute_mul
(
trees
[
0
]),
compute_mul
(
trees
[
1
]))
if
not
good
:
print
(
trees
[
0
])
print
(
trees
[
1
])
...
...
@@ -286,11 +285,11 @@ class T_sigmoid_opts(unittest.TestCase):
-
x
*
sigmoid
(
-
x
)
*
(
y
*
(
-
1
*
z
)))
ok
(
-
sigmoid
(
-
x
)
*
(
exp
(
y
)
*
(
-
exp
(
-
z
)
*
3
*
-
exp
(
x
))
*
(
y
*
2
*
(
-
sigmoid
(
-
y
)
*
(
z
+
t
)
*
exp
(
z
))
*
sigmoid
(
z
)))
*
-
sigmoid
(
x
),
(
y
*
2
*
(
-
sigmoid
(
-
y
)
*
(
z
+
t
)
*
exp
(
z
))
*
sigmoid
(
z
)))
*
-
sigmoid
(
x
),
sigmoid
(
x
)
*
(
-
sigmoid
(
y
)
*
(
-
sigmoid
(
-
z
)
*
3
)
*
(
y
*
2
*
((
z
+
t
)
*
exp
(
z
))))
*
-
sigmoid
(
x
))
(
-
sigmoid
(
x
)
))
ok
(
exp
(
-
x
)
*
-
exp
(
-
x
)
*
(
-
sigmoid
(
x
)
*
-
sigmoid
(
x
)),
-
sigmoid
(
-
x
)
*
sigmoid
(
-
x
))
ok
(
-
exp
(
x
)
*
-
sigmoid
(
-
x
)
*
-
exp
(
-
x
),
...
...
@@ -333,7 +332,7 @@ class T_sigmoid_opts(unittest.TestCase):
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
topo
[
0
]
.
op
==
ultra_fast_sigmoid
assert
len
(
topo
)
==
1
ux_v
=
f
([[
-
50
,
-
10
,
-
4
,
-
1
,
0
,
1
,
4
,
10
,
50
]])
f
([[
-
50
,
-
10
,
-
4
,
-
1
,
0
,
1
,
4
,
10
,
50
]])
def
test_local_hard_sigmoid
(
self
):
x
=
tensor
.
matrix
(
'x'
)
...
...
@@ -350,7 +349,7 @@ class T_sigmoid_opts(unittest.TestCase):
f
=
theano
.
function
([
x
],
s
,
mode
=
mode
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
not
any
([
n
.
op
==
sigmoid
for
n
in
topo
])
ux_v
=
f
([[
-
50
,
-
10
,
-
4
,
-
1
,
0
,
1
,
4
,
10
,
50
]])
f
([[
-
50
,
-
10
,
-
4
,
-
1
,
0
,
1
,
4
,
10
,
50
]])
mode2
=
mode
.
excluding
(
'fusion'
)
.
excluding
(
'inplace'
)
f2
=
theano
.
function
([
x
],
s
,
mode
=
mode2
)
...
...
@@ -416,7 +415,7 @@ class T_softplus_opts(unittest.TestCase):
out
=
T
.
log
(
1
-
sigmoid
(
x
)
.
reshape
([
x
.
size
]))
f
=
theano
.
function
([
x
],
out
,
mode
=
self
.
m
)
topo
=
f
.
maker
.
fgraph
.
toposort
()
#assert len(topo) == 3
#
assert len(topo) == 3
assert
any
(
isinstance
(
node
.
op
,
T
.
Reshape
)
for
node
in
topo
)
assert
any
(
isinstance
(
getattr
(
node
.
op
,
'scalar_op'
,
None
),
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
...
...
@@ -454,7 +453,7 @@ class T_sigmoid_utils(unittest.TestCase):
mul_tree
=
parse_mul_tree
(
tree
)
assert
parse_mul_tree
(
compute_mul
(
mul_tree
))
==
mul_tree
assert
theano
.
gof
.
graph
.
is_same_graph
(
compute_mul
(
parse_mul_tree
(
tree
)),
tree
)
compute_mul
(
parse_mul_tree
(
tree
)),
tree
)
def
test_parse_mul_tree
(
self
):
x
,
y
,
z
=
tensor
.
vectors
(
'x'
,
'y'
,
'z'
)
...
...
@@ -463,7 +462,7 @@ class T_sigmoid_utils(unittest.TestCase):
assert
parse_mul_tree
(
-
x
*
y
)
==
[
False
,
[[
True
,
x
],
[
False
,
y
]]]
assert
parse_mul_tree
(
-
x
)
==
[
True
,
x
]
assert
parse_mul_tree
((
x
*
y
)
*
-
z
)
==
[
False
,
[[
False
,
[[
False
,
x
],
[
False
,
y
]]],
[
True
,
z
]]]
False
,
[[
False
,
[[
False
,
x
],
[
False
,
y
]]],
[
True
,
z
]]]
def
test_is_1pexp
(
self
):
backup
=
config
.
warn
.
identify_1pexp_bug
...
...
theano/tests/test_flake8.py
浏览文件 @
d5944c96
...
...
@@ -72,14 +72,6 @@ whitelist_flake8 = [
"tensor/tests/test_blas_scipy.py"
,
"tensor/tests/test_mpi.py"
,
"tensor/nnet/__init__.py"
,
"tensor/nnet/tests/__init__.py"
,
"tensor/nnet/tests/test_conv.py"
,
"tensor/nnet/tests/test_neighbours.py"
,
"tensor/nnet/tests/test_nnet.py"
,
"tensor/nnet/tests/test_conv3d2d.py"
,
"tensor/nnet/tests/test_conv3d.py"
,
"tensor/nnet/tests/speed_test_conv.py"
,
"tensor/nnet/tests/test_sigm.py"
,
"tensor/signal/__init__.py"
,
"tensor/signal/tests/__init__.py"
,
"scalar/__init__.py"
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论