示例#1
0
    def from_params(cls, W, b=None, stride=1, pad=0, nobias=False, **kwargs):
        """from_params(cls, W, b=None, stride=1, pad=0, \
nobias=False, *, dilate=1, groups=1)

        Initialize a :class:`~chainer.links.Convolution2D` with given
        parameters.

        This method uses ``W`` and optional ``b`` to initialize
        a 2D convolution layer.

        Args:
            W (:class:`~chainer.Variable` or :ref:`ndarray`):
                The weight parameter.
            b (:class:`~chainer.Variable`, :ref:`ndarray`, or ``None``):
                The bias parameter.
            stride (int or pair of ints): Stride of filter applications.
                ``stride=s`` and ``stride=(s, s)`` are equivalent.
            pad (int or pair of ints): Spatial padding width for input arrays.
                ``pad=p`` and ``pad=(p, p)`` are equivalent.
            nobias (bool): If ``True``, then this link does not use
                the bias term in spite of whether ``b`` is given or not.
            dilate (int or pair of ints):
                Dilation factor of filter applications.
                ``dilate=d`` and ``dilate=(d, d)`` are equivalent.
            groups (:class:`int`): Number of groups of channels. If the number
                is greater than 1, input tensor :math:`W` is divided into some
                blocks by this value channel-wise. For each tensor blocks,
                convolution operation will be executed independently.
                Input channel size ``in_channels`` and output channel size
                ``out_channels`` must be exactly divisible by this value.
        """
        # TODO(crcrpar): Support the below conditions.
        # - W (and b) of cupy on non-default GPUs like id=1.
        # - W (and b) of chainerx on cuda.
        dilate, groups = argument.parse_kwargs(kwargs, ('dilate', 1),
                                               ('groups', 1))
        out_channels, _in_channels, kw, kh = W.shape
        in_channels = _in_channels * groups
        if b is not None:
            if out_channels != b.size:
                raise ValueError(
                    '`out_channels` does not match the size of `b`')

        link = cls(in_channels,
                   out_channels, (kw, kh),
                   stride,
                   pad,
                   nobias,
                   initialW=variable.as_array(W),
                   initial_bias=variable.as_array(b),
                   dilate=dilate,
                   groups=groups)
        return link
    def from_params(cls, W, b=None, nobias=False):
        """Initialize a :class:`~chainer.links.Linear` with given parameters.

        This method uses ``W`` and optional ``b`` to initialize a linear layer.

        Args:
            W (:class:`~chainer.Variable` or :ref:`ndarray`):
                The weight parameter.
            b (:class:`~chainer.Variable`, :ref:`ndarray`, or ``None``):
                The bias parameter.
            nobias (bool): If ``True``, the argument of ``b`` is ignored
                in spite of whether it's given or not.
        """
        out_size, in_size = W.shape
        if b is not None:
            if out_size != b.size:
                raise ValueError('`out_size` does not match the size of `b`')
        link = cls(in_size,
                   out_size,
                   nobias,
                   initialW=variable.as_array(W),
                   initial_bias=variable.as_array(b))
        return link
示例#3
0
def _chainerx_preprocess_const(x, value, label):
    # Allow mixing of numpy/cupy array and chainerx array as long as
    # conversion without copy is possible.
    if isinstance(value, (numpy.ndarray, cuda.ndarray)):
        # TODO(niboshi): force zero-copy
        return backend.to_chx(value)

    if isinstance(value, (six.integer_types, float)):
        return value
    if isinstance(value, numpy.generic):
        return value.item()
    if isinstance(value, variable.Variable):
        value = variable.as_array(value)
    utils._check_arrays_forward_compatible((x, value), label)
    return value
示例#4
0
    def from_params(cls, W, ignore_label=None):
        """Initialize `~chainer.links.EmbedID` with the given parameter.

        Args:
            W (:class:`~chainer.Variable` or :ref:`ndarray`):
                The weight parameter.
            ignore_label (int or None): If ``ignore_label`` is an int value,
                ``i``-th column of return value is filled with ``0``.
        """
        in_size, out_size = W.shape
        link = cls(in_size,
                   out_size,
                   initialW=variable.as_array(W),
                   ignore_label=ignore_label)
        return link