Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d01b5839
提交
d01b5839
authored
11月 25, 2016
作者:
Gijs van Tulder
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Helper functions to compute conv gradWeights shape.
上级
65f5ae84
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
124 行增加
和
0 行删除
+124
-0
abstract_conv.py
theano/tensor/nnet/abstract_conv.py
+106
-0
test_abstract_conv.py
theano/tensor/nnet/tests/test_abstract_conv.py
+18
-0
没有找到文件。
theano/tensor/nnet/abstract_conv.py
浏览文件 @
d01b5839
...
...
@@ -137,6 +137,112 @@ def get_conv_shape_1axis(image_shape, kernel_shape, border_mode,
return
out_shp
def
get_conv_gradweights_shape
(
image_shape
,
top_shape
,
border_mode
,
subsample
,
filter_dilation
=
None
):
"""
This function tries to compute the kernel shape of convolution gradWeights.
The weights shape can only be computed exactly when subsample is 1 and
border_mode is not 'half'. If subsample is not 1 or border_mode is 'half',
this function will return None.
Parameters
----------
image_shape: tuple of int corresponding to the input image shape. Its
four (or five) elements must correspond respectively to: batch size,
number of output channels, height and width of the image. None where
undefined.
top_shape: tuple of int (symbolic or numeric) corresponding to the top
image shape. Its four (or five) element must correspond respectively
to: batch size, number of output channels, height and width (and
possibly depth) of the image. None where undefined.
border_mode: string, int (symbolic or numeric) or tuple of int (symbolic
or numeric). If it is a string, it must be 'valid', 'half' or 'full'.
If it is a tuple, its two (or three) elements respectively correspond
to the padding on height and width (and possibly depth) axis.
subsample: tuple of int (symbolic or numeric). Its two or three elements
respectively correspond to the subsampling on height and width (and
possibly depth) axis.
filter_dilation: tuple of int (symbolic or numeric). Its two or three
elements correspond respectively to the dilation on height and
width axis.
Returns
-------
kernel_shape: tuple of int (symbolic or numeric) corresponding to the
kernel shape. Its four (or five) elements correspond respectively
to: number of output channels, number of input channels, height and
width (and possibly depth) of the kernel. None where undefined.
"""
nkern
,
imshp
=
image_shape
[
1
],
image_shape
[
2
:]
nchan
,
topshp
=
top_shape
[
1
],
top_shape
[
2
:]
if
filter_dilation
is
None
:
filter_dilation
=
numpy
.
ones
(
len
(
subsample
),
dtype
=
'int'
)
if
isinstance
(
border_mode
,
tuple
):
out_shp
=
tuple
(
get_conv_gradweights_shape_1axis
(
imshp
[
i
],
topshp
[
i
],
border_mode
[
i
],
subsample
[
i
],
filter_dilation
[
i
])
for
i
in
range
(
len
(
subsample
)))
else
:
out_shp
=
tuple
(
get_conv_gradweights_shape_1axis
(
imshp
[
i
],
topshp
[
i
],
border_mode
,
subsample
[
i
],
filter_dilation
[
i
])
for
i
in
range
(
len
(
subsample
)))
return
(
nchan
,
nkern
)
+
out_shp
def
get_conv_gradweights_shape_1axis
(
image_shape
,
top_shape
,
border_mode
,
subsample
,
dilation
):
"""
This function tries to compute the image shape of convolution gradWeights.
The weights shape can only be computed exactly when subsample is 1 and
border_mode is not 'half'. If subsample is not 1 or border_mode is 'half',
this function will return None.
Parameters
----------
image_shape: int or None. Corresponds to the input image shape on a
given axis. None if undefined.
top_shape: int or None. Corresponds to the top shape on a given axis.
None if undefined.
border_mode: string or int. If it is a string, it must be
'valid', 'half' or 'full'. If it is an integer, it must correspond to
the padding on the considered axis.
subsample: int. It must correspond to the subsampling on the
considered axis.
dilation: int. It must correspond to the dilation on the
considered axis.
Returns
-------
kernel_shape: int or None. Corresponds to the kernel shape on a given
axis. None if undefined.
"""
if
None
in
[
image_shape
,
top_shape
,
border_mode
,
subsample
,
dilation
]:
return
None
if
subsample
!=
1
or
border_mode
==
"half"
:
return
None
if
border_mode
==
"full"
:
kernel_shape
=
top_shape
-
image_shape
elif
border_mode
==
"valid"
:
kernel_shape
=
image_shape
-
top_shape
else
:
if
border_mode
<
0
:
raise
ValueError
(
"border_mode must be >= 0"
)
kernel_shape
=
(
image_shape
+
2
*
border_mode
-
top_shape
)
if
dilation
>
1
:
kernel_shape
=
kernel_shape
/
dilation
return
kernel_shape
+
1
def
get_conv_gradinputs_shape
(
kernel_shape
,
top_shape
,
border_mode
,
subsample
,
filter_dilation
=
None
):
...
...
theano/tensor/nnet/tests/test_abstract_conv.py
浏览文件 @
d01b5839
...
...
@@ -11,6 +11,7 @@ from theano.gof.opt import check_stack_trace
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.nnet
import
corr
,
corr3d
,
abstract_conv
as
conv
from
theano.tensor.nnet.abstract_conv
import
(
get_conv_output_shape
,
get_conv_gradweights_shape
,
get_conv_gradinputs_shape
,
check_conv_gradinputs_shape
)
from
theano.tensor.nnet.abstract_conv
import
AbstractConv2d
...
...
@@ -192,6 +193,23 @@ class TestConvGradInputsShape(unittest.TestCase):
image_shape_with_None
=
image_shape
[:
2
]
+
(
None
,
None
)
self
.
assertEqual
(
computed_image_shape
,
image_shape_with_None
)
# compute the kernel_shape given this output_shape
computed_kernel_shape
=
get_conv_gradweights_shape
(
image_shape
,
output_shape
,
b
,
(
1
,
1
),
(
d
,
d
))
# if border_mode == 'half', the shape should be None
if
b
==
'half'
:
kernel_shape_with_None
=
kernel_shape
[:
2
]
+
(
None
,
None
)
self
.
assertEqual
(
computed_kernel_shape
,
kernel_shape_with_None
)
else
:
self
.
assertEqual
(
computed_kernel_shape
,
kernel_shape
)
# if subsample > 1, the shape should be None
computed_kernel_shape
=
get_conv_gradweights_shape
(
kernel_shape
,
output_shape
,
b
,
(
2
,
3
),
(
d
,
d
))
kernel_shape_with_None
=
kernel_shape
[:
2
]
+
(
None
,
None
)
self
.
assertEqual
(
computed_kernel_shape
,
kernel_shape_with_None
)
class
BaseTestConv
(
object
):
def
get_output_shape
(
self
,
inputs_shape
,
filters_shape
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论