Skip to content

Commit

Permalink
Merge pull request #4564 from okuta/pr-4059
Browse files Browse the repository at this point in the history
Fix group argument of {de,}convolution_2d to keyword-only argument.
  • Loading branch information
kmaehashi committed Apr 5, 2018
2 parents d0ebd36 + 6e878e5 commit 8a9d034
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
17 changes: 10 additions & 7 deletions chainer/functions/connection/convolution_2d.py
Expand Up @@ -25,7 +25,7 @@ class Convolution2DFunction(function_node.FunctionNode):

_use_ideep = False

def __init__(self, stride=1, pad=0, cover_all=False, groups=1, **kwargs):
def __init__(self, stride=1, pad=0, cover_all=False, **kwargs):
argument.check_unexpected_kwargs(
kwargs,
deterministic="deterministic argument is not supported anymore. "
Expand All @@ -36,7 +36,8 @@ def __init__(self, stride=1, pad=0, cover_all=False, groups=1, **kwargs):
"the gradient w.r.t. x is automatically decided during "
"backpropagation."
)
dilate, = argument.parse_kwargs(kwargs, ('dilate', 1))
dilate, groups = argument.parse_kwargs(kwargs,
('dilate', 1), ('groups', 1))

self.sy, self.sx = _pair(stride)
self.ph, self.pw = _pair(pad)
Expand Down Expand Up @@ -426,9 +427,8 @@ def backward(self, indexes, grad_outputs):
return ret


def convolution_2d(x, W, b=None, stride=1, pad=0, cover_all=False, groups=1,
**kwargs):
"""convolution_2d(x, W, b=None, stride=1, pad=0, cover_all=False, *, dilate=1)
def convolution_2d(x, W, b=None, stride=1, pad=0, cover_all=False, **kwargs):
"""convolution_2d(x, W, b=None, stride=1, pad=0, cover_all=False, *, dilate=1, groups=1)
Two-dimensional convolution function.
Expand Down Expand Up @@ -517,6 +517,8 @@ def convolution_2d(x, W, b=None, stride=1, pad=0, cover_all=False, groups=1,
into some output pixels.
dilate (int or pair of ints): Dilation factor of filter applications.
``dilate=d`` and ``dilate=(d, d)`` are equivalent.
groups (int): The number of groups to use grouped convolution. The
default is one, where grouped convolution is not used.
Returns:
~chainer.Variable:
Expand Down Expand Up @@ -554,13 +556,14 @@ def convolution_2d(x, W, b=None, stride=1, pad=0, cover_all=False, groups=1,
>>> y.shape == (n, c_o, h_o, w_o + 1)
True
"""
""" # NOQA
argument.check_unexpected_kwargs(
kwargs, deterministic="deterministic argument is not "
"supported anymore. "
"Use chainer.using_config('cudnn_deterministic', value) "
"context where value is either `True` or `False`.")
dilate, = argument.parse_kwargs(kwargs, ('dilate', 1))
dilate, groups = argument.parse_kwargs(kwargs,
('dilate', 1), ('groups', 1))

fnode = Convolution2DFunction(stride, pad, cover_all, dilate=dilate,
groups=groups)
Expand Down
15 changes: 9 additions & 6 deletions chainer/functions/connection/deconvolution_2d.py
Expand Up @@ -27,7 +27,7 @@ class Deconvolution2DFunction(function_node.FunctionNode):
cover_all = None
_use_ideep = False

def __init__(self, stride=1, pad=0, outsize=None, groups=1, **kwargs):
def __init__(self, stride=1, pad=0, outsize=None, **kwargs):
argument.check_unexpected_kwargs(
kwargs,
deterministic="deterministic argument is not supported anymore. "
Expand All @@ -38,7 +38,8 @@ def __init__(self, stride=1, pad=0, outsize=None, groups=1, **kwargs):
"the gradient w.r.t. x is automatically decided during "
"backpropagation."
)
dilate, = argument.parse_kwargs(kwargs, ('dilate', 1))
dilate, groups = argument.parse_kwargs(kwargs,
('dilate', 1), ('groups', 1))

self.sy, self.sx = _pair(stride)
self.ph, self.pw = _pair(pad)
Expand Down Expand Up @@ -297,9 +298,8 @@ def _set_cover_all(self, x, W):
self.pw, d=self.dx))


def deconvolution_2d(x, W, b=None, stride=1, pad=0, outsize=None, groups=1,
**kwargs):
"""deconvolution_2d(x, W, b=None, stride=1, pad=0, outsize=None)
def deconvolution_2d(x, W, b=None, stride=1, pad=0, outsize=None, **kwargs):
"""deconvolution_2d(x, W, b=None, stride=1, pad=0, outsize=None, *, groups=1)
Two dimensional deconvolution function.
Expand Down Expand Up @@ -371,6 +371,8 @@ def deconvolution_2d(x, W, b=None, stride=1, pad=0, outsize=None, groups=1,
It should be pair of height and width :math:`(h_O, w_O)`.
Default value is ``None`` and the outsize is estimated by
input size, stride and pad.
groups (int): The number of groups to use grouped deconvolution. The
default is one, where grouped deconvolution is not used.
Returns:
~chainer.Variable:
Expand Down Expand Up @@ -409,7 +411,8 @@ def deconvolution_2d(x, W, b=None, stride=1, pad=0, outsize=None, groups=1,
"supported anymore. "
"Use chainer.using_config('cudnn_deterministic', value) "
"context where value is either `True` or `False`.")
dilate, = argument.parse_kwargs(kwargs, ('dilate', 1))
dilate, groups = argument.parse_kwargs(kwargs,
('dilate', 1), ('groups', 1))

func = Deconvolution2DFunction(stride, pad, outsize, dilate=dilate,
groups=groups)
Expand Down
10 changes: 6 additions & 4 deletions chainer/links/connection/convolution_2d.py
Expand Up @@ -7,7 +7,7 @@

class Convolution2D(link.Link):

"""__init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None)
"""__init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, *, groups=1)
Two-dimensional convolutional layer.
Expand Down Expand Up @@ -48,6 +48,8 @@ class Convolution2D(link.Link):
initial_bias (:ref:`initializer <initializer>`): Initializer to
initialize the bias. If ``None``, the bias will be initialized to
zero. When it is :class:`numpy.ndarray`, its ``ndim`` should be 1.
groups (int): The number of groups to use grouped convolution. The
default is one, where grouped convolution is not used.
.. seealso::
See :func:`chainer.functions.convolution_2d` for the definition of
Expand Down Expand Up @@ -103,16 +105,16 @@ class Convolution2D(link.Link):
""" # NOQA

def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0,
nobias=False, initialW=None, initial_bias=None, groups=1,
**kwargs):
nobias=False, initialW=None, initial_bias=None, **kwargs):
super(Convolution2D, self).__init__()

argument.check_unexpected_kwargs(
kwargs, deterministic="deterministic argument is not "
"supported anymore. "
"Use chainer.using_config('cudnn_deterministic', value) "
"context where value is either `True` or `False`.")
dilate, = argument.parse_kwargs(kwargs, ('dilate', 1))
dilate, groups = argument.parse_kwargs(kwargs,
('dilate', 1), ('groups', 1))

if ksize is None:
out_channels, ksize, in_channels = in_channels, out_channels, None
Expand Down
8 changes: 5 additions & 3 deletions chainer/links/connection/deconvolution_2d.py
Expand Up @@ -10,7 +10,7 @@

class Deconvolution2D(link.Link):

"""__init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, outsize=None, initialW=None, initial_bias=None)
"""__init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, outsize=None, initialW=None, initial_bias=None, *, groups=1)
Two dimensional deconvolution function.
Expand Down Expand Up @@ -52,6 +52,8 @@ class Deconvolution2D(link.Link):
initial_bias (:ref:`initializer <initializer>`): Initializer to
initialize the bias. If ``None``, the bias will be initialized to
zero. When it is :class:`numpy.ndarray`, its ``ndim`` should be 1.
groups (int): The number of groups to use grouped deconvolution. The
default is one, where grouped deconvolution is not used.
The filter weight has four dimensions :math:`(c_I, c_O, k_H, k_W)`
which indicate the number of input channels, output channels,
Expand Down Expand Up @@ -127,15 +129,15 @@ class Deconvolution2D(link.Link):

def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0,
nobias=False, outsize=None, initialW=None, initial_bias=None,
groups=1, **kwargs):
**kwargs):
super(Deconvolution2D, self).__init__()

argument.check_unexpected_kwargs(
kwargs, deterministic="deterministic argument is not "
"supported anymore. "
"Use chainer.using_config('cudnn_deterministic', value) "
"context where value is either `True` or `False`.")
argument.assert_kwargs_empty(kwargs)
groups, = argument.parse_kwargs(kwargs, ('groups', 1))

if ksize is None:
out_channels, ksize, in_channels = in_channels, out_channels, None
Expand Down

0 comments on commit 8a9d034

Please sign in to comment.