Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0c3ab9a5
提交
0c3ab9a5
authored
9月 10, 2014
作者:
f0k
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make ConvOp propagate any available shape information to instantiated gradient ops
上级
ff91a554
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
121 行增加
和
140 行删除
+121
-140
conv.py
theano/tensor/nnet/conv.py
+121
-140
没有找到文件。
theano/tensor/nnet/conv.py
浏览文件 @
0c3ab9a5
...
...
@@ -242,34 +242,30 @@ class ConvOp(OpenMPOp):
speed_unroll_patch_shape
=
[
1.2967290878295898
,
5.5283889770507812
]
@staticmethod
def
has_all_shape
(
imshp
,
kshp
,
nkern
,
bsize
):
all_shape
=
(
imshp
is
not
None
and
kshp
is
not
None
and
nkern
is
not
None
and
bsize
is
not
None
)
if
(
all_shape
and
(
any
([
True
for
sh
in
imshp
if
sh
is
None
])
or
any
([
True
for
sh
in
kshp
if
sh
is
None
]))):
all_shape
=
False
return
all_shape
def
has_all_shape
(
imshp
,
kshp
,
nkern
=
1
,
bsize
=
1
):
return
(
nkern
is
not
None
and
bsize
is
not
None
and
all
(
shp
is
not
None
for
shp
in
imshp
)
and
all
(
shp
is
not
None
for
shp
in
kshp
))
@staticmethod
def
getOutputShape
(
inshp
,
kshp
,
stride
=
(
1
,
1
),
mode
=
'valid'
):
"""
Computes the output dimensions of convolving an image of shape "inshp"
with kernels of shape "kshp".
with kernels of shape "kshp". Accepts symbolic or integer shapes.
Propagates `None`s (for unknown shapes).
:param inshp: (rows,cols) of input image
:param kshp: (rows,cols) of filters
:param mode: 'valid' or 'full' (see 'border_mode' in conv2d's doc)
:return: (rows,cols) of output image
"""
dx
,
dy
=
stride
if
mode
==
'valid'
:
s
=
-
1
else
:
s
=
1
inshp
,
kshp
=
numpy
.
array
(
inshp
),
numpy
.
array
(
kshp
)
return
numpy
.
int64
(
numpy
.
ceil
((
inshp
+
s
*
kshp
-
s
*
1
)
/
numpy
.
array
([
dx
,
dy
],
dtype
=
'float'
)))
# The formula would be ceil((i + s * k - s * 1) / float(d)),
# with s=1 for mode=='full' and s=-1 for mode=='valid'.
# To support symbolic shapes, we express this with integer arithmetics.
return
tuple
(
None
if
i
is
None
or
k
is
None
else
((
i
-
k
)
//
d
+
1
)
if
mode
==
'valid'
else
((
i
+
k
+
d
-
2
)
//
d
)
for
i
,
k
,
d
in
zip
(
inshp
,
kshp
,
stride
))
def
__init__
(
self
,
imshp
=
None
,
kshp
=
None
,
nkern
=
None
,
bsize
=
None
,
dx
=
1
,
dy
=
1
,
...
...
@@ -287,7 +283,7 @@ class ConvOp(OpenMPOp):
"""
Initializes a ConvOp with given output_mode (full/valid). All other
parameters are optional and are only used to generate more optimized c
code.
code
, or to enable graph optimizers to optimally replace the ConvOp
.
NOTES ON OPTIMIZATION:
Their is two type of optimization. The first is the selection of the
...
...
@@ -368,13 +364,31 @@ class ConvOp(OpenMPOp):
Set to False in the grad again the weight when the
output_mode is full.
"""
# De
s
activate fft_optimization at the op level if specified
# Deactivate fft_optimization at the op level if specified
if
version
==
"no_fft"
:
self
.
fft_opt
=
False
version
=
-
1
else
:
self
.
fft_opt
=
True
# Expand unknown image / kernel shapes into tuples of Nones
if
imshp
is
None
:
imshp
=
(
None
,
None
)
else
:
imshp
=
tuple
(
imshp
)
if
kshp
is
None
:
kshp
=
(
None
,
None
)
else
:
kshp
=
tuple
(
kshp
)
# Check imshp and kshp dimensionality
if
len
(
imshp
)
==
2
:
imshp
=
(
1
,)
+
imshp
elif
len
(
imshp
)
!=
3
:
raise
ValueError
(
"len(imshp) must be 2 or 3, got
%
d"
%
len
(
imshp
))
if
len
(
kshp
)
!=
2
:
raise
ValueError
(
"len(kshp) must be 2, got
%
d"
%
len
(
kshp
))
# We must continue to consider None as 1 for backward compatibility.
if
dx
is
None
:
dx
=
1
...
...
@@ -390,32 +404,17 @@ class ConvOp(OpenMPOp):
dy
=
int
(
dy
)
all_shape
=
self
.
has_all_shape
(
imshp
,
kshp
,
nkern
,
bsize
)
if
(
unroll_batch
or
unroll_kern
)
and
not
all_shape
:
raise
Exception
(
"In ConvOp, when using unroll_batch and"
" unroll_nkern, all shape are needed"
)
#Init the openmp attribute
super
(
ConvOp
,
self
)
.
__init__
(
openmp
=
openmp
)
if
not
all_shape
or
self
.
openmp
:
# Only this version is parallelized
unroll_patch
=
True
if
imshp
is
not
None
:
imshp
=
tuple
(
imshp
)
if
len
(
imshp
)
==
2
:
imshp
=
(
1
,)
+
imshp
elif
len
(
imshp
)
==
3
:
imshp
=
imshp
else
:
raise
Exception
(
"bad len for imshp"
)
self
.
imshp
=
imshp
if
kshp
is
not
None
:
kshp
=
tuple
(
kshp
)
self
.
kshp
=
kshp
self
.
nkern
=
nkern
self
.
bsize
=
bsize
...
...
@@ -425,16 +424,24 @@ class ConvOp(OpenMPOp):
self
.
version
=
version
# a triple
self
.
imshp_logical
=
self
.
imshp
if
imshp_logical
is
not
None
:
self
.
imshp_logical
=
tuple
(
imshp_logical
)
assert
((
self
.
imshp
is
None
and
self
.
imshp_logical
is
None
)
or
(
len
(
self
.
imshp
)
==
len
(
self
.
imshp_logical
)))
if
imshp_logical
is
None
:
self
.
imshp_logical
=
self
.
imshp
else
:
imshp_logical
=
tuple
(
imshp_logical
)
if
len
(
imshp_logical
)
!=
3
:
raise
ValueError
(
"len(imshp_logical) must be 3, got
%
d"
%
len
(
imshp_logical
))
self
.
imshp_logical
=
imshp_logical
# a pair
self
.
kshp_logical
=
self
.
kshp
if
kshp_logical
is
not
None
:
self
.
kshp_logical
=
tuple
(
kshp_logical
)
if
kshp_logical
is
None
:
self
.
kshp_logical
=
self
.
kshp
else
:
kshp_logical
=
tuple
(
kshp_logical
)
if
len
(
kshp_logical
)
!=
2
:
raise
ValueError
(
"len(kshp_logical) must be k, got
%
d"
%
len
(
kshp_logical
))
self
.
kshp_logical
=
kshp_logical
# a bool
self
.
kshp_logical_top_aligned
=
kshp_logical_top_aligned
self
.
unroll_batch
=
unroll_batch
...
...
@@ -485,23 +492,19 @@ class ConvOp(OpenMPOp):
_logger
.
warn
(
warnstr
,
self
.
unroll_kern
,
self
.
nkern
,
new
)
self
.
unroll_kern
=
new
if
all_shape
:
self
.
outshp
=
ConvOp
.
getOutputShape
(
self
.
imshp_logical
[
1
:],
self
.
kshp_logical
,
(
dx
,
dy
),
self
.
outshp
=
ConvOp
.
getOutputShape
(
self
.
imshp_logical
[
1
:],
self
.
kshp_logical
,
(
dx
,
dy
),
output_mode
)
self
.
fulloutshp
=
ConvOp
.
getOutputShape
(
self
.
imshp_logical
[
1
:],
self
.
kshp_logical
,
(
1
,
1
),
output_mode
)
self
.
fulloutshp
=
ConvOp
.
getOutputShape
(
self
.
imshp_logical
[
1
:],
self
.
kshp_logical
,
(
1
,
1
),
output_mode
)
else
:
self
.
outshp
=
None
self
.
fulloutshp
=
None
self
.
out_mode
=
output_mode
if
not
self
.
out_mode
in
[
"valid"
,
"full"
]:
raise
Exception
(
"Mode
%
s not implemented"
%
self
.
out_mode
)
if
self
.
outshp
is
not
None
and
not
(
self
.
outshp
>
0
)
.
all
(
):
if
any
((
shp
is
not
None
)
and
(
shp
<=
0
)
for
shp
in
self
.
outshp
):
raise
Exception
(
"Bad size for the output shape. Verify that [post-"
"supersampling] input shape (
%
s) and kern"
" shape(
%
s) are ok. (Hint: kerns must fit inside"
...
...
@@ -518,14 +521,10 @@ class ConvOp(OpenMPOp):
elif
self
.
bsize
is
not
None
and
self
.
nkern
is
not
None
:
bsize
=
self
.
bsize
nkern
=
self
.
nkern
if
bsize
is
None
:
bsize
=
1
if
nkern
is
None
:
nkern
=
1
mode_idx
=
0
if
self
.
out_mode
!=
"valid"
:
mode_idx
=
1
if
all_shape
:
if
self
.
has_all_shape
(
self
.
imshp
,
self
.
kshp
)
:
time_unroll_patch
=
self
.
speed_unroll_patch_shape
[
mode_idx
]
else
:
time_unroll_patch
=
self
.
speed_unroll_patch_noshape
[
...
...
@@ -619,10 +618,7 @@ class ConvOp(OpenMPOp):
raise
NotImplementedError
(
"The image and the kernel must have the same type."
"inputs(
%
s), kerns(
%
s)"
%
(
_inputs
.
dtype
,
_kerns
.
dtype
))
if
self
.
outshp
is
not
None
:
bcastable23
=
[
self
.
outshp
[
0
]
==
1
,
self
.
outshp
[
1
]
==
1
]
else
:
bcastable23
=
[
False
,
False
]
bcastable23
=
[
self
.
outshp
[
0
]
==
1
,
self
.
outshp
[
1
]
==
1
]
output
=
theano
.
tensor
.
tensor
(
dtype
=
_inputs
.
type
.
dtype
,
broadcastable
=
[
_inputs
.
broadcastable
[
0
],
_kerns
.
broadcastable
[
0
]]
+
...
...
@@ -631,32 +627,31 @@ class ConvOp(OpenMPOp):
return
Apply
(
self
,
[
_inputs
,
_kerns
],
[
output
])
def
infer_shape
(
self
,
node
,
input_shapes
):
imshp
=
input_shapes
[
0
]
kshp
=
input_shapes
[
1
]
batch_size
=
imshp
[
0
]
fmo
=
kshp
[
0
]
if
self
.
imshp
is
not
None
and
self
.
kshp
is
not
None
:
imshp
=
self
.
imshp
kshp
=
self
.
kshp
if
self
.
imshp_logical
:
imshp
=
self
.
imshp_logical
if
self
.
kshp_logical
:
kshp
=
self
.
kshp_logical
try
:
fmshp
=
ConvOp
.
getOutputShape
(
imshp
[
1
:],
kshp
,
(
self
.
dx
,
self
.
dy
),
self
.
out_mode
)
except
TypeError
:
raise
theano
.
tensor
.
ShapeError
()
outshp
=
(
batch_size
,
fmo
)
+
tuple
(
fmshp
)
return
[
outshp
]
else
:
# Haven't implemented this case. imshp and kshp may be symbollic
# and ConvOp.getOutputShape doesn't handle this. In this case
# we simply let the default function do its work.
imshp
=
input_shapes
[
0
]
# 4D image shape
kshp
=
input_shapes
[
1
]
# 4D filter shape
bsize
,
imshp
=
imshp
[
0
],
list
(
imshp
[
1
:])
nkern
,
kshp
=
kshp
[
0
],
list
(
kshp
[
2
:])
# replace symbolic shapes with known shapes
if
self
.
bsize
:
bsize
=
self
.
bsize
if
self
.
imshp_logical
[
0
]:
imshp
[
0
]
=
self
.
imshp_logical
[
0
]
if
self
.
imshp_logical
[
1
]:
imshp
[
1
]
=
self
.
imshp_logical
[
1
]
if
self
.
imshp_logical
[
2
]:
imshp
[
2
]
=
self
.
imshp_logical
[
2
]
if
self
.
nkern
:
nkern
=
self
.
nkern
if
self
.
kshp_logical
[
0
]:
kshp
[
0
]
=
self
.
kshp_logical
[
0
]
if
self
.
kshp_logical
[
1
]:
kshp
[
1
]
=
self
.
kshp_logical
[
1
]
# infer output shape from what we have
outshp
=
ConvOp
.
getOutputShape
(
imshp
[
1
:],
kshp
,
(
self
.
dx
,
self
.
dy
),
self
.
out_mode
)
if
not
self
.
has_all_shape
(
self
.
imshp_logical
,
self
.
kshp_logical
,
self
.
bsize
,
self
.
nkern
):
# FIXME: Not sure why this is needed. I think the shape is inferred
# correctly no matter what, but if we return a partially symbolic
# shape here, test_conv_cuda_ndarray:test_gemm_grads fails. (@f0k)
raise
theano
.
tensor
.
ShapeError
()
# FIXME: Actually, test_conv_cuda_ndarray:test_gemm_grads only passes if
# we completely disable shape inference. (@f0k)
raise
theano
.
tensor
.
ShapeError
()
return
[(
bsize
,
nkern
)
+
outshp
]
def
perform
(
self
,
node
,
inp
,
out
):
"""
...
...
@@ -674,10 +669,10 @@ class ConvOp(OpenMPOp):
# TODO: move these back out to global scope when they no longer
# cause an atexit error
imshp
=
self
.
imshp
if
imshp
is
None
or
any
([
x
is
None
for
x
in
imshp
]
):
if
any
(
x
is
None
for
x
in
imshp
):
imshp
=
tuple
(
img2d
.
shape
[
1
:])
kshp
=
self
.
kshp
if
kshp
is
None
or
any
([
x
is
None
for
x
in
kshp
]
):
if
any
(
x
is
None
for
x
in
kshp
):
kshp
=
tuple
(
filtersflipped
.
shape
[
2
:])
bsize
=
self
.
bsize
if
bsize
is
None
:
...
...
@@ -687,24 +682,22 @@ class ConvOp(OpenMPOp):
nkern
=
filtersflipped
.
shape
[
0
]
imshp_logical
=
self
.
imshp_logical
if
imshp_logical
is
None
:
imshp_logical
=
imshp
if
numpy
.
any
([
x
is
None
for
x
in
imshp_logical
]):
imshp_logical
=
tuple
(
img2d
.
shape
[
1
:])
if
imshp_logical
[
0
]
is
None
:
imshp_logical
=
(
imshp
[
0
],)
+
imshp_logical
[
1
:]
if
imshp_logical
[
1
]
is
None
:
imshp_logical
=
(
imshp_logical
[
0
],
imshp
[
1
],
imshp_logical
[
2
])
if
imshp_logical
[
2
]
is
None
:
imshp_logical
=
imshp_logical
[:
2
]
+
(
imshp
[
2
],)
assert
all
(
x
is
not
None
for
x
in
imshp_logical
)
kshp_logical
=
self
.
kshp_logical
if
kshp_logical
is
None
:
kshp_logical
=
kshp
else
:
if
kshp_logical
[
0
]
is
None
:
kshp_logical
=
(
kshp
[
0
],
kshp_logical
[
1
])
if
kshp_logical
[
1
]
is
None
:
kshp_logical
=
(
kshp_logical
[
0
],
kshp
[
1
])
if
numpy
.
any
([
x
is
None
for
x
in
kshp_logical
]):
kshp
=
tuple
(
filtersflipped
.
shape
[
2
:])
if
kshp_logical
[
0
]
is
None
:
kshp_logical
=
(
kshp
[
0
],
kshp_logical
[
1
])
if
kshp_logical
[
1
]
is
None
:
kshp_logical
=
(
kshp_logical
[
0
],
kshp
[
1
])
assert
all
(
x
is
not
None
for
x
in
kshp_logical
)
if
self
.
fulloutshp
is
not
None
:
if
all
(
shp
is
not
None
for
shp
in
self
.
fulloutshp
)
:
fulloutshp
=
tuple
(
self
.
fulloutshp
)
else
:
fulloutshp
=
tuple
(
ConvOp
.
getOutputShape
(
imshp_logical
[
...
...
@@ -843,19 +836,14 @@ class ConvOp(OpenMPOp):
newin
=
inputs
.
dimshuffle
((
1
,
0
,
2
,
3
))
newgz
=
gz
.
dimshuffle
((
1
,
0
,
2
,
3
))
(
bsize
,
nkern
)
=
None
,
None
imshp
=
None
kshp
=
None
un_p
=
self
.
unroll_patch
imshp_logical
=
None
if
self
.
out_mode
==
'valid'
:
(
img
,
filters
)
=
(
newin
,
newgz
)
kshp_logical
=
self
.
fulloutshp
kshp_logical_top_aligned
=
False
i
f
all_shape
:
(
bsize
,
nkern
)
=
(
self
.
imshp
[
0
],
self
.
nkern
)
imshp
=
(
self
.
bsize
,
self
.
imshp
[
1
],
self
.
imshp
[
2
])
i
mshp_logical
=
None
(
bsize
,
nkern
)
=
(
self
.
imshp
[
0
],
self
.
nkern
)
imshp
=
(
self
.
bsize
,
self
.
imshp
[
1
],
self
.
imshp
[
2
])
kshp
=
self
.
outshp
un_b
=
self
.
unroll_batch
un_k
=
self
.
unroll_kern
...
...
@@ -863,13 +851,12 @@ class ConvOp(OpenMPOp):
(
img
,
filters
)
=
(
newgz
,
newin
)
kshp_logical
=
None
kshp_logical_top_aligned
=
True
if
all_shape
:
imshp_logical
=
(
self
.
bsize
,
self
.
fulloutshp
[
0
],
self
.
fulloutshp
[
1
])
(
bsize
,
nkern
)
=
(
self
.
nkern
,
self
.
imshp
[
0
])
imshp
=
(
self
.
bsize
,
self
.
outshp
[
0
],
self
.
outshp
[
1
])
kshp
=
self
.
imshp
[
1
:]
imshp_logical
=
(
self
.
bsize
,
self
.
fulloutshp
[
0
],
self
.
fulloutshp
[
1
])
(
bsize
,
nkern
)
=
(
self
.
nkern
,
self
.
imshp
[
0
])
imshp
=
(
self
.
bsize
,
self
.
outshp
[
0
],
self
.
outshp
[
1
])
kshp
=
self
.
imshp
[
1
:]
un_b
=
self
.
unroll_kern
un_k
=
self
.
unroll_batch
else
:
...
...
@@ -920,7 +907,7 @@ class ConvOp(OpenMPOp):
dw
=
dw
(
img
,
filters
)
if
all_shape
:
assert
(
dw
.
owner
.
op
.
outshp
==
self
.
kshp
)
.
all
(
)
assert
all
(
o
==
k
for
o
,
k
in
zip
(
dw
.
owner
.
op
.
outshp
,
self
.
kshp
)
)
if
self
.
out_mode
==
'valid'
:
# before DimShuffle, dw is of shape visdim x nkern x kshp[0] x kshp[1]
dw
=
dw
.
dimshuffle
((
1
,
0
,
2
,
3
))
...
...
@@ -933,16 +920,11 @@ class ConvOp(OpenMPOp):
filters
=
kerns
.
dimshuffle
((
1
,
0
,
2
,
3
))
filters
=
filters
[:,
:,
::
-
1
,
::
-
1
]
nkern
=
None
imshp
=
None
imshp_logical
=
None
kshp
=
None
if
all_shape
:
nkern
=
self
.
imshp
[
0
]
imshp
=
(
self
.
nkern
,
self
.
outshp
[
0
],
self
.
outshp
[
1
])
imshp_logical
=
(
self
.
nkern
,
self
.
fulloutshp
[
0
],
self
.
fulloutshp
[
1
])
nkern
=
self
.
imshp
[
0
]
imshp
=
(
self
.
nkern
,
self
.
outshp
[
0
],
self
.
outshp
[
1
])
imshp_logical
=
(
self
.
nkern
,
self
.
fulloutshp
[
0
],
self
.
fulloutshp
[
1
])
if
0
:
# hard-code c generation parameters
din
=
ConvOp
(
imshp
,
self
.
kshp
,
nkern
,
self
.
bsize
,
...
...
@@ -965,9 +947,8 @@ class ConvOp(OpenMPOp):
din
=
din
(
gz
,
filters
)
assert
(
din
.
owner
.
op
.
outshp
is
None
and
self
.
imshp
is
None
)
or
\
(
din
.
owner
.
op
.
outshp
is
None
)
or
\
(
din
.
owner
.
op
.
outshp
==
self
.
imshp
[
1
:])
.
all
()
assert
(
all
(
shp
is
None
for
shp
in
din
.
owner
.
op
.
outshp
)
or
all
(
o
==
i
for
o
,
i
in
zip
(
din
.
owner
.
op
.
outshp
,
self
.
imshp
[
1
:])))
# din and dw should have the same broadcasting pattern as the
# parameters they are the gradient of (resp. inputs and kerns).
...
...
@@ -1075,23 +1056,23 @@ using namespace std;
d
[
"self_kshp1"
]
=
"PyArray_DIMS(
%(filtersflipped)
s)[3]"
%
d
# Override the default value if we have it
if
self
.
kshp
is
not
None
and
self
.
kshp
[
0
]
:
if
self
.
kshp
[
0
]
is
not
None
:
d
[
"self_kshp0"
]
=
self
.
kshp
[
0
]
if
self
.
kshp
is
not
None
and
self
.
kshp
[
1
]
:
if
self
.
kshp
[
1
]
is
not
None
:
d
[
"self_kshp1"
]
=
self
.
kshp
[
1
]
if
self
.
outshp
is
not
None
and
self
.
outshp
[
0
]
:
if
self
.
outshp
[
0
]
is
not
None
:
d
[
"self_outshp0"
]
=
self
.
outshp
[
0
]
if
self
.
outshp
is
not
None
and
self
.
outshp
[
1
]
:
if
self
.
outshp
[
1
]
is
not
None
:
d
[
"self_outshp1"
]
=
self
.
outshp
[
1
]
if
self
.
imshp
is
not
None
and
self
.
imshp
[
0
]
:
if
self
.
imshp
[
0
]
is
not
None
:
d
[
"self_imshp0"
]
=
self
.
imshp
[
0
]
if
self
.
imshp
is
not
None
and
self
.
imshp
[
1
]
:
if
self
.
imshp
[
1
]
is
not
None
:
d
[
"self_imshp1"
]
=
self
.
imshp
[
1
]
if
self
.
imshp
is
not
None
and
self
.
imshp
[
2
]
:
if
self
.
imshp
[
2
]
is
not
None
:
d
[
"self_imshp2"
]
=
self
.
imshp
[
2
]
if
self
.
bsize
:
if
self
.
bsize
is
not
None
:
d
[
"self_bsize"
]
=
self
.
bsize
if
self
.
nkern
:
if
self
.
nkern
is
not
None
:
d
[
"self_nkern"
]
=
self
.
nkern
# Other hard coded stuff only if we have all shapes
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论