Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
33d35144
提交
33d35144
authored
7月 27, 2015
作者:
Nicolas Ballas
提交者:
Pascal Lamblin
10月 14, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update test
上级
12cc6f02
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
194 行增加
和
132 行删除
+194
-132
abstract_conv2d.py
theano/tensor/nnet/abstract_conv2d.py
+41
-48
test_abstractconv.py
theano/tensor/nnet/tests/test_abstractconv.py
+153
-84
没有找到文件。
theano/tensor/nnet/abstract_conv2d.py
浏览文件 @
33d35144
...
@@ -539,7 +539,6 @@ def local_conv2d_gradinputs_corrmm(node):
...
@@ -539,7 +539,6 @@ def local_conv2d_gradinputs_corrmm(node):
@local_optimizer
([
AbstractConv2d
])
@local_optimizer
([
AbstractConv2d
])
def
local_conv2d_cpu
(
node
):
def
local_conv2d_cpu
(
node
):
import
pdb
;
pdb
.
set_trace
()
if
not
isinstance
(
node
.
op
,
AbstractConv2d
):
if
not
isinstance
(
node
.
op
,
AbstractConv2d
):
return
None
return
None
...
@@ -559,24 +558,29 @@ register_specialize_device(local_conv2d_cpu)
...
@@ -559,24 +558,29 @@ register_specialize_device(local_conv2d_cpu)
@local_optimizer
([
AbstractConv2d_gradWeights
])
@local_optimizer
([
AbstractConv2d_gradWeights
])
def
local_conv2d_gradweight_cpu
(
node
):
def
local_conv2d_gradweight_cpu
(
node
):
import
pdb
;
pdb
.
set_trace
()
## len is 4 all the time
img
,
topgrad
,
shape
=
node
.
inputs
img
,
topgrad
,
shape
=
node
.
inputs
if
isinstance
(
img
.
type
,
CudaNdarrayType
)
or
\
if
isinstance
(
img
.
type
,
CudaNdarrayType
)
or
\
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
return
None
return
None
if
node
.
op
.
border_mode
not
in
[
'full'
,
'valid'
]:
return
None
if
(
node
.
op
.
border_mode
==
'valid'
and
node
.
op
.
subsample
!=
(
1
,
1
))
or
\
node
.
op
.
imshp
is
None
or
node
.
op
.
kshp
is
None
:
if
node
.
op
.
border_mode
==
'valid'
and
\
(
node
.
op
.
subsample
!=
(
1
,
1
)
or
node
.
op
.
imshp
is
None
or
node
.
op
.
kshp
is
None
):
# Use the gradient as defined in conv3D, because the implementation
# Use the gradient as defined in conv3D, because the implementation
# by Conv is slow (about 3x slower than conv3D, and probably 10x
# by Conv is slow (about 3x slower than conv3D, and probably 10x
# slower than it could be), nad incorrect when subsample > 2.
# slower than it could be), nad incorrect when subsample > 2.
# build a "node", that should be equivalent to the one given by
# build a "node", that should be equivalent to the one given by
# self.make_node, but using convGrad3D instead.
# self.make_node, but using convGrad3D instead.
if
not
node
.
op
.
filter_flip
:
topgrad
=
topgrad
[:,
:,
::
-
1
,
::
-
1
]
# flip them
shuffled_img
=
img
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
shuffled_img
=
img
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
shuffled_topgrad
=
topgrad
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
shuffled_topgrad
=
topgrad
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
print
shape
rval
=
convGrad3D
(
V
=
shuffled_img
,
rval
=
convGrad3D
(
V
=
shuffled_img
,
d
=
(
node
.
op
.
subsample
[
0
],
node
.
op
.
subsample
[
1
],
1
),
d
=
(
node
.
op
.
subsample
[
0
],
node
.
op
.
subsample
[
1
],
1
),
WShape
=
(
shape
[
0
],
shape
[
2
],
shape
[
3
],
1
,
shape
[
1
]),
WShape
=
(
shape
[
0
],
shape
[
2
],
shape
[
3
],
1
,
shape
[
1
]),
...
@@ -585,10 +589,11 @@ def local_conv2d_gradweight_cpu(node):
...
@@ -585,10 +589,11 @@ def local_conv2d_gradweight_cpu(node):
rval
=
theano
.
tensor
.
addbroadcast
(
rval
,
3
)
rval
=
theano
.
tensor
.
addbroadcast
(
rval
,
3
)
return
[
rval
.
dimshuffle
(
0
,
4
,
1
,
2
)]
return
[
rval
.
dimshuffle
(
0
,
4
,
1
,
2
)]
if
node
.
op
.
imshp
is
None
or
node
.
op
.
kshp
is
None
:
return
None
####### Determine gradient on kernels ########
####### Determine gradient on kernels ########
assert
len
(
node
.
op
.
imshp
)
==
4
and
len
(
node
.
op
.
kshp
)
==
4
assert
len
(
node
.
op
.
imshp
)
==
4
and
len
(
node
.
op
.
kshp
)
==
4
print
"here0"
,
node
.
op
.
imshp
[
2
:],
node
.
op
.
kshp
[
2
:]
import
pdb
;
pdb
.
set_trace
()
outshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
outshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
node
.
op
.
kshp
[
2
:],
node
.
op
.
subsample
,
node
.
op
.
kshp
[
2
:],
node
.
op
.
subsample
,
...
@@ -596,23 +601,19 @@ def local_conv2d_gradweight_cpu(node):
...
@@ -596,23 +601,19 @@ def local_conv2d_gradweight_cpu(node):
fulloutshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
fulloutshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
node
.
op
.
kshp
[
2
:],
(
1
,
1
),
node
.
op
.
kshp
[
2
:],
(
1
,
1
),
node
.
op
.
border_mode
)
node
.
op
.
border_mode
)
print
outshp
,
fulloutshp
#newimg = img.dimshuffle((1, 0, 2, 3))
newimg
=
img
.
dimshuffle
((
1
,
0
,
2
,
3
))
#newtopgrad = topgrad.dimshuffle((1, 0, 2, 3))
newtopgrad
=
topgrad
.
dimshuffle
((
1
,
0
,
2
,
3
))
newimg
=
img
newtopgrad
=
topgrad
if
node
.
op
.
border_mode
==
'valid'
:
if
node
.
op
.
border_mode
==
'valid'
:
print
"here1"
,
node
.
op
.
imshp
,
node
.
op
.
kshp
,
fulloutshp
(
img
,
filters
)
=
(
newimg
,
newtopgrad
)
(
img
,
filters
)
=
(
newimg
,
newtopgrad
)
kshp_logical
=
fulloutshp
kshp_logical
=
fulloutshp
kshp_logical_top_aligned
=
False
kshp_logical_top_aligned
=
False
imshp_logical
=
None
imshp_logical
=
None
(
bsize
,
nkern
)
=
(
node
.
op
.
imshp
[
0
],
node
.
op
.
kshp
[
0
])
(
bsize
,
nkern
)
=
(
node
.
op
.
imshp
[
1
],
node
.
op
.
kshp
[
0
])
imshp
=
(
bsize
,
node
.
op
.
imshp
[
1
],
node
.
op
.
imshp
[
2
])
imshp
=
(
node
.
op
.
imshp
[
0
],
node
.
op
.
imshp
[
2
],
node
.
op
.
imshp
[
3
])
kshp
=
node
.
op
.
kshp
[
2
:]
kshp
=
outshp
elif
node
.
op
.
border_mode
==
'full'
:
elif
node
.
op
.
border_mode
==
'full'
:
(
img
,
filters
)
=
(
newtopgrad
,
newimg
)
(
img
,
filters
)
=
(
newtopgrad
,
newimg
)
kshp_logical
=
None
kshp_logical
=
None
...
@@ -622,25 +623,20 @@ def local_conv2d_gradweight_cpu(node):
...
@@ -622,25 +623,20 @@ def local_conv2d_gradweight_cpu(node):
fulloutshp
[
1
])
fulloutshp
[
1
])
(
bsize
,
nkern
)
=
(
node
.
op
.
kshp
[
0
],
node
.
op
.
imshp
[
1
])
(
bsize
,
nkern
)
=
(
node
.
op
.
kshp
[
0
],
node
.
op
.
imshp
[
1
])
imshp
=
(
node
.
op
.
imshp
[
0
],
outshp
[
0
],
outshp
[
1
])
imshp
=
(
node
.
op
.
imshp
[
0
],
outshp
[
0
],
outshp
[
1
])
kshp
=
node
.
op
.
imshp
[
1
:]
kshp
=
node
.
op
.
imshp
[
2
:]
else
:
else
:
raise
NotImplementedError
(
raise
NotImplementedError
(
'Only [full,valid] modes are currently supported.'
)
'Only [full,valid] modes are currently supported.'
)
print
"here2"
,
node
.
op
.
imshp
,
node
.
op
.
kshp
,
fulloutshp
if
node
.
op
.
filter_flip
:
if
node
.
op
.
filter_flip
:
filters
=
filters
[:,
:,
::
-
1
,
::
-
1
]
# flip them
filters
=
filters
[:,
:,
::
-
1
,
::
-
1
]
# flip them
dw
=
ConvOp
(
imshp
,
kshp
,
nkern
,
bsize
,
1
,
1
,
output_mode
=
'valid'
,
dw
=
ConvOp
(
imshp
,
kshp
,
nkern
,
bsize
,
1
,
1
,
output_mode
=
'valid'
,
unroll_batch
=
None
,
unroll_kern
=
None
,
unroll_patch
=
None
,
unroll_batch
=
None
,
unroll_kern
=
None
,
unroll_patch
=
None
,
imshp_logical
=
imshp_logical
,
imshp_logical
=
imshp_logical
,
kshp_logical
=
kshp_logical
,
kshp_logical
=
kshp_logical
,
kshp_logical_top_aligned
=
kshp_logical_top_aligned
,
kshp_logical_top_aligned
=
kshp_logical_top_aligned
,
direction_hint
=
'bprop weights'
)
direction_hint
=
'bprop weights'
)
#dw = ConvOp(output_mode='valid')
res
=
dw
(
img
,
filters
)
res
=
dw
(
img
,
filters
)
print
"here3"
,
node
.
op
.
imshp
,
node
.
op
.
kshp
,
fulloutshp
res
=
res
.
dimshuffle
((
1
,
0
,
2
,
3
))
res
=
res
.
dimshuffle
((
1
,
0
,
2
,
3
))
return
[
res
]
return
[
res
]
register_specialize_device
(
local_conv2d_gradweight_cpu
)
register_specialize_device
(
local_conv2d_gradweight_cpu
)
...
@@ -649,53 +645,50 @@ register_specialize_device(local_conv2d_gradweight_cpu)
...
@@ -649,53 +645,50 @@ register_specialize_device(local_conv2d_gradweight_cpu)
@local_optimizer
([
AbstractConv2d_gradInputs
])
@local_optimizer
([
AbstractConv2d_gradInputs
])
def
local_conv2d_gradinputs_cpu
(
node
):
def
local_conv2d_gradinputs_cpu
(
node
):
import
pdb
;
pdb
.
set_trace
()
kern
,
topgrad
,
shape
=
node
.
inputs
kern
,
topgrad
,
shape
=
node
.
inputs
if
isinstance
(
kern
.
type
,
CudaNdarrayType
)
or
\
if
isinstance
(
kern
.
type
,
CudaNdarrayType
)
or
\
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
return
None
return
None
print
"here4a"
,
node
.
op
.
imshp
,
node
.
op
.
kshp
if
node
.
op
.
border_mode
not
in
[
'full'
,
'valid'
]:
return
None
if
node
.
op
.
border_mode
==
'valid'
and
node
.
op
.
subsample
!=
(
1
,
1
):
### Conv 3d implementation, needed when subsample > 2
# Use the gradient as defined in conv3D, because the implementation
if
node
.
op
.
border_mode
==
'valid'
and
\
# by Conv is slow (about 3x slower than conv3D, and probably 10x
(
node
.
op
.
subsample
!=
(
1
,
1
)
or
node
.
op
.
imshp
is
None
or
node
.
op
.
kshp
is
None
):
# slower than it could be), nad incorrect when subsample > 2.
if
node
.
op
.
filter_flip
:
# build a "node", that should be equivalent to the one given by
kern
=
kern
[:,
:,
::
-
1
,
::
-
1
]
# self.make_node, but using convGrad3D instead.
shuffled_kern
=
kern
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
shuffled_kern
=
kern
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
shuffled_topgrad
=
topgrad
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
shuffled_topgrad
=
topgrad
.
dimshuffle
(
0
,
2
,
3
,
'x'
,
1
)
b
=
T
.
zeros
((
kern
.
shape
[
1
])
)
b
=
theano
.
tensor
.
zeros_like
(
shuffled_kern
[
0
,
0
,
0
,
0
,
:]
)
rval
=
C
onvTransp3D
(
W
=
shuffled_kern
,
b
=
b
,
rval
=
c
onvTransp3D
(
W
=
shuffled_kern
,
b
=
b
,
d
=
(
op
.
subsample
[
0
],
op
.
subsample
[
1
],
1
),
d
=
(
node
.
op
.
subsample
[
0
],
node
.
op
.
subsample
[
1
],
1
),
H
=
shuffled_topgrad
,
H
=
shuffled_topgrad
,
RShape
=
(
shape
[
0
],
shape
[
1
],
1
))
RShape
=
(
shape
[
2
],
shape
[
3
],
1
))
rval
=
theano
.
tensor
.
addbroadcast
(
rval
,
3
)
return
[
rval
.
dimshuffle
(
0
,
4
,
1
,
2
)]
return
[
rval
.
dimshuffle
(
0
,
4
,
1
,
2
)]
####### Determine gradient on inputs ########
### Conv2d Implementation
if
node
.
op
.
imshp
is
None
or
node
.
op
.
kshp
is
None
:
return
None
mode
=
'valid'
mode
=
'valid'
if
not
node
.
op
.
border_mode
==
'full'
:
if
not
node
.
op
.
border_mode
==
'full'
:
mode
=
'full'
mode
=
'full'
filters
=
kern
.
dimshuffle
((
1
,
0
,
2
,
3
))
filters
=
kern
.
dimshuffle
((
1
,
0
,
2
,
3
))
if
node
.
op
.
filter_flip
:
filters
=
filters
[:,
:,
::
-
1
,
::
-
1
]
outshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
outshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
node
.
op
.
kshp
[
2
:],
node
.
op
.
subsample
,
node
.
op
.
kshp
[
2
:],
node
.
op
.
subsample
,
node
.
op
.
border_mode
)
node
.
op
.
border_mode
)
fulloutshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
fulloutshp
=
ConvOp
.
getOutputShape
(
node
.
op
.
imshp
[
2
:],
node
.
op
.
kshp
[
2
:],
(
1
,
1
),
node
.
op
.
kshp
[
2
:],
(
1
,
1
),
node
.
op
.
border_mode
)
node
.
op
.
border_mode
)
nkern
=
node
.
op
.
kshp
[
1
]
nkern
=
node
.
op
.
imshp
[
1
]
imshp
=
(
nkern
,
outshp
[
0
],
outshp
[
1
])
imshp
=
(
node
.
op
.
kshp
[
0
],
outshp
[
0
],
outshp
[
1
])
imshp_logical
=
(
nkern
,
fulloutshp
[
0
],
fulloutshp
[
1
])
imshp_logical
=
(
node
.
op
.
kshp
[
0
],
fulloutshp
[
0
],
fulloutshp
[
1
])
if
node
.
op
.
filter_flip
:
filters
=
filters
[:,
:,
::
-
1
,
::
-
1
]
print
"here4"
,
imshp
,
node
.
op
.
kshp
,
nkern
din
=
ConvOp
(
imshp
,
din
=
ConvOp
(
imshp
,
node
.
op
.
kshp
[
2
:],
node
.
op
.
kshp
[
2
:],
nkern
,
nkern
,
...
...
theano/tensor/nnet/tests/test_abstractconv.py
浏览文件 @
33d35144
...
@@ -9,43 +9,54 @@ from nose.plugins.skip import SkipTest
...
@@ -9,43 +9,54 @@ from nose.plugins.skip import SkipTest
import
theano.tensor.nnet.conv
as
conv_ref
import
theano.tensor.nnet.conv
as
conv_ref
import
theano.tensor.nnet.abstract_conv2d
as
conv
import
theano.tensor.nnet.abstract_conv2d
as
conv
from
theano.sandbox.cuda
import
float32_shared_constructor
as
shared
from
theano.sandbox.cuda
import
float32_shared_constructor
as
gpu_shared
from
theano.compile
import
shared
as
cpu_shared
from
theano.sandbox.cuda.tests.test_conv_cuda_ndarray
import
py_conv
from
theano.sandbox.cuda.tests.test_conv_cuda_ndarray
import
py_conv
#from theano.sandbox.cuda.dnn import dnn_available
#from theano.sandbox.cuda.dnn import dnn_available
if
theano
.
config
.
mode
==
'FAST_COMPILE'
:
if
theano
.
config
.
mode
==
'FAST_COMPILE'
:
#
mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu')
mode_with_gpu
=
theano
.
compile
.
mode
.
get_mode
(
'FAST_RUN'
)
.
including
(
'gpu'
)
mode_without_gpu
=
theano
.
compile
.
mode
.
get_mode
(
'FAST_RUN'
)
.
excluding
(
'gpu'
)
mode_without_gpu
=
theano
.
compile
.
mode
.
get_mode
(
'FAST_RUN'
)
.
excluding
(
'gpu'
)
else
:
else
:
#
mode_with_gpu = theano.compile.mode.get_default_mode().including('gpu')
mode_with_gpu
=
theano
.
compile
.
mode
.
get_default_mode
()
.
including
(
'gpu'
)
mode_without_gpu
=
theano
.
compile
.
mode
.
get_mode
(
'FAST_RUN'
)
.
excluding
(
'gpu'
)
mode_without_gpu
=
theano
.
compile
.
get_default_mode
(
)
.
excluding
(
'gpu'
)
class
TestConv2d
(
unittest
.
TestCase
):
class
TestConv2d
(
unittest
.
TestCase
):
def
run_conv
(
self
,
def
run_fwd
(
self
,
inputs_shape
,
inputs_shape
,
filters_shape
,
filters_shape
,
subsample
=
(
1
,
1
),
subsample
=
(
1
,
1
),
verify_grad
=
True
,
verify_grad
=
True
,
mode
=
mode_without_gpu
):
mode
=
mode_without_gpu
,
border_mode
=
'valid'
,
device
=
'gpu'
,
provide_shape
=
False
):
inputs_val
=
numpy
.
random
.
random
(
inputs_shape
)
.
astype
(
'float32'
)
inputs_val
=
numpy
.
random
.
random
(
inputs_shape
)
.
astype
(
'float32'
)
filters_val
=
numpy
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
filters_val
=
numpy
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
### FIXME (CPU vs GPU)
if
device
==
'gpu'
:
inputs
=
theano
.
tensor
.
shared
(
inputs_val
)
inputs
=
gpu_shared
(
inputs_val
)
filters
=
theano
.
tensor
.
shared
(
filters_val
)
filters
=
gpu_shared
(
filters_val
)
else
:
inputs
=
cpu_shared
(
inputs_val
)
filters
=
cpu_shared
(
filters_val
)
if
provide_shape
:
imshp
=
inputs_shape
kshp
=
filters_shape
else
:
imshp
=
None
kshp
=
None
c_ref
=
conv_ref
.
conv2d
(
inputs
,
filters
,
c_ref
=
conv_ref
.
conv2d
(
inputs
,
filters
,
border_mode
=
"valid"
,
border_mode
=
border_mode
,
subsample
=
subsample
)
subsample
=
subsample
)
c
=
conv
.
conv2d
(
inputs
,
filters
,
c
=
conv
.
conv2d
(
inputs
,
filters
,
border_mode
=
"valid"
,
subsample
=
subsample
)
border_mode
=
border_mode
,
subsample
=
subsample
)
f_ref
=
theano
.
function
([],
c_ref
,
mode
=
mode
)
f_ref
=
theano
.
function
([],
c_ref
,
mode
=
mode
)
f
=
theano
.
function
([],
c
,
mode
)
f
=
theano
.
function
([],
c
,
mode
)
...
@@ -56,8 +67,8 @@ class TestConv2d(unittest.TestCase):
...
@@ -56,8 +67,8 @@ class TestConv2d(unittest.TestCase):
utt
.
assert_allclose
(
res_ref
,
res
)
utt
.
assert_allclose
(
res_ref
,
res
)
if
verify_grad
:
if
verify_grad
:
utt
.
verify_grad
(
conv
.
AbstractConv2d
(
border_mode
=
"valid"
,
utt
.
verify_grad
(
conv
.
AbstractConv2d
(
border_mode
=
"valid"
,
imshp
=
i
nputs_shape
,
imshp
=
i
mshp
,
kshp
=
filters_shape
,
kshp
=
kshp
,
bsize
=
inputs_shape
[
0
],
bsize
=
inputs_shape
[
0
],
subsample
=
subsample
),
subsample
=
subsample
),
[
inputs_val
,
filters_val
])
[
inputs_val
,
filters_val
])
...
@@ -70,6 +81,7 @@ class TestConv2d(unittest.TestCase):
...
@@ -70,6 +81,7 @@ class TestConv2d(unittest.TestCase):
subsample
=
(
1
,
1
),
subsample
=
(
1
,
1
),
verify_grad
=
True
,
verify_grad
=
True
,
mode
=
mode_without_gpu
,
mode
=
mode_without_gpu
,
border_mode
=
'valid'
,
device
=
'gpu'
,
device
=
'gpu'
,
provide_shape
=
False
):
provide_shape
=
False
):
...
@@ -77,29 +89,30 @@ class TestConv2d(unittest.TestCase):
...
@@ -77,29 +89,30 @@ class TestConv2d(unittest.TestCase):
output_val
=
numpy
.
random
.
random
(
output_shape
)
.
astype
(
'float32'
)
output_val
=
numpy
.
random
.
random
(
output_shape
)
.
astype
(
'float32'
)
if
device
==
'gpu'
:
if
device
==
'gpu'
:
inputs
=
shared
(
inputs_val
)
inputs
=
gpu_
shared
(
inputs_val
)
filters
=
shared
(
filters
_val
)
output
=
gpu_shared
(
output
_val
)
else
:
else
:
inputs
=
theano
.
tensor
.
shared
(
inputs_val
)
inputs
=
cpu_
shared
(
inputs_val
)
output
=
theano
.
tensor
.
shared
(
output_val
)
output
=
cpu_
shared
(
output_val
)
if
provide_shape
:
if
provide_shape
:
imshp
=
inputs_shape
imshp
=
inputs_shape
kshp
=
filters_shape
kshp
=
filters_shape
else
:
else
:
imshp
=
None
,
imshp
=
None
kshp
=
None
kshp
=
None
c
=
conv
.
AbstractConv2d_gradWeights
(
border_mode
=
"valid"
,
c
=
conv
.
AbstractConv2d_gradWeights
(
border_mode
=
border_mode
,
subsample
=
subsample
,
subsample
=
subsample
,
imshp
=
imshp
,
kshp
=
kshp
)
imshp
=
imshp
,
kshp
=
kshp
)
c
=
c
(
inputs
,
output
,
filters_shape
)
c
=
c
(
inputs
,
output
,
filters_shape
)
f
=
theano
.
function
([],
c
,
mode
)
f
=
theano
.
function
([],
c
,
mode
)
res_ref
=
py_conv
(
inputs_val
.
transpose
((
1
,
0
,
2
,
3
)),
res_ref
=
py_conv
(
inputs_val
.
transpose
((
1
,
0
,
2
,
3
)),
output_val
.
transpose
((
1
,
0
,
2
,
3
)),
output_val
.
transpose
((
1
,
0
,
2
,
3
))
[:,
:,
::
-
1
,
::
-
1
]
,
'valid'
,
subsample
)
.
transpose
((
1
,
0
,
2
,
3
))
'valid'
,
subsample
)
.
transpose
((
1
,
0
,
2
,
3
))
print
res_ref
.
shape
,
numpy
.
array
(
f
())
.
shape
res
=
numpy
.
array
(
f
())
res
=
numpy
.
array
(
f
())
print
res_ref
.
shape
,
res
.
shape
utt
.
assert_allclose
(
res_ref
,
res
)
utt
.
assert_allclose
(
res_ref
,
res
)
if
verify_grad
:
if
verify_grad
:
utt
.
verify_grad
(
conv
.
AbstractConv2d
(
border_mode
=
"valid"
,
utt
.
verify_grad
(
conv
.
AbstractConv2d
(
border_mode
=
"valid"
,
...
@@ -110,37 +123,58 @@ class TestConv2d(unittest.TestCase):
...
@@ -110,37 +123,58 @@ class TestConv2d(unittest.TestCase):
def
run_gradinput
(
self
,
def
run_gradinput
(
self
,
inputs_shape
,
inputs_shape
,
filters_shape
,
filters_shape
,
output_shape
,
subsample
=
(
1
,
1
),
subsample
=
(
1
,
1
),
verify_grad
=
True
,
verify_grad
=
True
,
mode
=
mode_without_gpu
):
mode
=
mode_without_gpu
,
border_mode
=
'valid'
,
device
=
'gpu'
,
provide_shape
=
False
):
inputs_val
=
numpy
.
random
.
random
(
inputs
_shape
)
.
astype
(
'float32'
)
output_val
=
numpy
.
random
.
random
(
output
_shape
)
.
astype
(
'float32'
)
filters_val
=
numpy
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
filters_val
=
numpy
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
inputs
=
shared
(
inputs_val
)
filters
=
shared
(
filters_val
.
transpose
(
1
,
0
,
2
,
3
)[:,
:,
::
-
1
,
::
-
1
])
if
device
==
'gpu'
:
output
=
gpu_shared
(
output_val
)
filters
=
gpu_shared
(
filters_val
)
else
:
output
=
cpu_shared
(
output_val
)
filters
=
cpu_shared
(
filters_val
)
if
provide_shape
:
imshp
=
inputs_shape
kshp
=
filters_shape
else
:
imshp
=
None
kshp
=
None
c
=
conv
.
AbstractConv2d_gradInputs
(
border_mode
=
"valid"
,
c
=
conv
.
AbstractConv2d_gradInputs
(
border_mode
=
"valid"
,
subsample
=
subsample
)
subsample
=
subsample
,
c
=
c
(
filters
,
inputs
,
inputs_shape
)
imshp
=
imshp
,
kshp
=
kshp
)
c
=
c
(
filters
,
output
,
inputs_shape
)
f
=
theano
.
function
([],
c
,
mode
)
f
=
theano
.
function
([],
c
,
mode
)
res_ref
=
py_conv
(
inputs_val
,
filters_val
,
'full'
,
subsample
)
res_ref
=
py_conv
(
output_val
,
res
=
numpy
.
array
(
f
())
#.transpose((1, 0, 2, 3))
filters_val
.
transpose
(
1
,
0
,
2
,
3
)[:,
:,
::
-
1
,
::
-
1
],
'full'
,
subsample
)
print
filters_val
.
shape
,
output_val
.
shape
,
inputs_shape
res
=
numpy
.
array
(
f
())
print
"2, "
,
res_ref
.
shape
,
res
.
shape
print
"2, "
,
res_ref
.
shape
,
res
.
shape
utt
.
assert_allclose
(
res_ref
,
res
)
utt
.
assert_allclose
(
res_ref
,
res
)
if
verify_grad
:
if
verify_grad
:
utt
.
verify_grad
(
conv
.
AbstractConv2d
(
border_mode
=
"valid"
,
utt
.
verify_grad
(
conv
.
AbstractConv2d_gradInputs
(
border_mode
=
border_mode
,
subsample
=
subsample
),
subsample
=
subsample
),
[
inputs_val
,
filters_val
])
[
filters_val
,
output_val
,
numpy
.
array
(
inputs_shape
)
.
astype
(
'float32'
)])
#
def test_corrmm(self):
#def test_corrmm(self):
#
mode = mode_with_gpu
# mode = mode_with_gpu
#
mode = mode.excluding('cudnn')
# mode = mode.excluding('cudnn')
#
self.run_conv
(inputs_shape=(16, 1, 2, 2),
#
self.run_fwd
(inputs_shape=(16, 1, 2, 2),
#
filters_shape=(10, 1, 2, 2),
# filters_shape=(10, 1, 2, 2),
#
verify_grad=False, mode=mode)
# verify_grad=False, mode=mode)
# self.run_gradweight(inputs_shape=(16, 1, 2, 2),
# self.run_gradweight(inputs_shape=(16, 1, 2, 2),
# filters_shape=(10, 1, 2, 2),
# filters_shape=(10, 1, 2, 2),
# verify_grad=False, mode=mode)
# verify_grad=False, mode=mode)
...
@@ -149,50 +183,85 @@ class TestConv2d(unittest.TestCase):
...
@@ -149,50 +183,85 @@ class TestConv2d(unittest.TestCase):
# verify_grad=False, mode=mode)
# verify_grad=False, mode=mode)
#def test_cpu(self):
#self.run_conv(inputs_shape=(16, 1, 2, 2),
def
test_cpu_conv
(
self
):
# filters_shape=(10, 1, 2, 2),
# verify_grad=False,
inputs_shapes
=
[(
16
,
1
,
2
,
2
),
(
16
,
1
,
8
,
8
),
(
16
,
1
,
4
,
4
)]
# mode=mode_without_gpu)
filters_shapes
=
[(
10
,
1
,
2
,
2
),
(
10
,
1
,
2
,
2
),
(
10
,
1
,
2
,
2
),]
# self.run_gradinput(inputs_shape=(1, 1, 2, 2),
output_shapes
=
[(
16
,
10
,
1
,
1
),
(
16
,
10
,
7
,
7
),
(
16
,
10
,
3
,
3
)]
# filters_shape=(10, 1, 2, 2),
subsamples
=
[(
1
,
1
),
(
1
,
1
),
(
1
,
1
)]
# verify_grad=False, mode=mode_without_gpu)
border_mode
=
'valid'
# mode = mode_without_gpu
for
i
,
f
,
o
,
s
in
zip
(
inputs_shapes
[
0
:
1
],
filters_shapes
[
0
:
1
],
output_shapes
[
0
:
1
],
subsamples
[
0
:
1
]):
# self.run_conv(inputs_shape=(16, 1, 2, 2),
for
provide_shape
in
[
True
]:
# filters_shape=(10, 1, 2, 2),
self
.
run_fwd
(
inputs_shape
=
i
,
filters_shape
=
f
,
subsample
=
s
,
# verify_grad=False, mode=mode)
verify_grad
=
True
,
mode
=
mode_without_gpu
,
device
=
'cpu'
,
# self.run_gradweight(inputs_shape=(16, 1, 2, 2),
provide_shape
=
provide_shape
,
border_mode
=
border_mode
)
# filters_shape=(10, 1, 2, 2),
return
# verify_grad=False, mode=mode)
### No reference implementation of full available yet
# self.run_gradinput(inputs_shape=(1, 1, 2, 2),
border_mode
=
'full'
# filters_shape=(10, 1, 2, 2),
provide_shape
=
True
# verify_grad=False, mode=mode)
self
.
run_gradweight
(
inputs_shape
=
(
16
,
1
,
2
,
2
),
filters_shape
=
(
10
,
1
,
2
,
2
),
output_shape
=
(
16
,
10
,
3
,
3
),
subsample
=
(
1
,
1
),
# # self.run_conv(inputs_shape=(16, 1, 8, 8),
verify_grad
=
True
,
mode
=
mode_without_gpu
,
device
=
'cpu'
,
# # filters_shape=(10, 1, 4, 4),
provide_shape
=
provide_shape
,
border_mode
=
border_mode
)
# # subsample=(2, 2),
# # verify_grad=False,mode=mode)
# # self.run_conv(inputs_shape=(16, 1, 2, 2),
# # filters_shape=(10, 1, 2, 2),
# # verify_grad=True,mode=mode)
# # self.run_conv(inputs_shape=(16, 1, 8, 8),
# # filters_shape=(10, 1, 2, 2),
# # subsample=(2, 2),
# # verify_grad=True,mode=mode)
def
test_cpu_grad_weight
(
self
):
def
test_cpu_grad_weight
(
self
):
### FIXME subsample
inputs_shapes
=
[(
16
,
1
,
2
,
2
),
(
16
,
1
,
8
,
8
),
(
16
,
1
,
4
,
4
)]
filters_shapes
=
[(
10
,
1
,
2
,
2
),
(
10
,
1
,
2
,
2
),
(
10
,
1
,
2
,
2
),]
output_shapes
=
[(
16
,
10
,
1
,
1
),
(
16
,
10
,
7
,
7
),
(
16
,
10
,
3
,
3
)]
subsamples
=
[(
1
,
1
),
(
1
,
1
),
(
1
,
1
)]
border_mode
=
'valid'
for
i
,
f
,
o
,
s
in
zip
(
inputs_shapes
[:],
filters_shapes
[:],
output_shapes
[:],
subsamples
[:]):
for
provide_shape
in
[
False
,
True
]:
self
.
run_gradweight
(
inputs_shape
=
i
,
filters_shape
=
f
,
output_shape
=
o
,
subsample
=
s
,
verify_grad
=
False
,
mode
=
mode_without_gpu
,
device
=
'cpu'
,
provide_shape
=
provide_shape
,
border_mode
=
border_mode
)
return
### No reference implementation of full available yet
border_mode
=
'full'
provide_shape
=
True
self
.
run_gradweight
(
inputs_shape
=
(
16
,
1
,
2
,
2
),
self
.
run_gradweight
(
inputs_shape
=
(
16
,
1
,
2
,
2
),
filters_shape
=
(
10
,
1
,
2
,
2
),
filters_shape
=
(
10
,
1
,
2
,
2
),
output_shape
=
(
16
,
10
,
1
,
1
),
output_shape
=
(
16
,
10
,
3
,
3
),
verify_grad
=
False
,
mode
=
mode_without_gpu
,
device
=
'cpu'
)
subsample
=
(
1
,
1
),
verify_grad
=
True
,
mode
=
mode_without_gpu
,
device
=
'cpu'
,
provide_shape
=
provide_shape
,
border_mode
=
border_mode
)
def
test_cpu_grad_input
(
self
):
### FIXME subsample
inputs_shapes
=
[(
16
,
1
,
2
,
2
),
(
16
,
1
,
8
,
8
),
(
16
,
1
,
4
,
4
)]
filters_shapes
=
[(
10
,
1
,
2
,
2
),
(
10
,
1
,
2
,
2
),
(
10
,
1
,
2
,
2
),]
output_shapes
=
[(
16
,
10
,
1
,
1
),
(
16
,
10
,
7
,
7
),
(
16
,
10
,
3
,
3
)]
subsamples
=
[(
1
,
1
),
(
1
,
1
),
(
1
,
1
)]
border_mode
=
'valid'
for
i
,
f
,
o
,
s
in
zip
(
inputs_shapes
[:],
filters_shapes
[:],
output_shapes
[:],
subsamples
[:]):
for
provide_shape
in
[
True
,
False
]:
self
.
run_gradinput
(
inputs_shape
=
i
,
filters_shape
=
f
,
output_shape
=
o
,
subsample
=
s
,
verify_grad
=
False
,
mode
=
mode_without_gpu
,
device
=
'cpu'
,
provide_shape
=
provide_shape
,
border_mode
=
border_mode
)
return
### No reference implementation of full available yet
border_mode
=
'full'
provide_shape
=
True
self
.
run_gradweight
(
inputs_shape
=
(
16
,
1
,
2
,
2
),
self
.
run_gradweight
(
inputs_shape
=
(
16
,
1
,
2
,
2
),
filters_shape
=
(
10
,
1
,
2
,
2
),
filters_shape
=
(
10
,
1
,
2
,
2
),
output_shape
=
(
16
,
10
,
1
,
1
),
output_shape
=
(
16
,
10
,
3
,
3
),
verify_grad
=
False
,
subsample
=
(
1
,
1
)
,
mode
=
mode_without_gpu
,
device
=
'cpu'
,
verify_grad
=
False
,
mode
=
mode_without_gpu
,
device
=
'cpu'
,
provide_shape
=
Tru
e
)
provide_shape
=
provide_shape
,
border_mode
=
border_mod
e
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论