<commit_before>from chainer import cuda
from chainer import function
from chainer.utils import type_check


class CReLU(function.Function):

    """Concatenated Rectified Linear Unit."""

    def __init__(self, axis=1):
        if not isinstance(axis, int):
            raise TypeError('axis must be an integer value')
        self.axis = axis

    def check_type_forward(self, in_types):
        type_check.expect(
            in_types.size() == 1,
            in_types[0].dtype.kind == 'f',
            in_types[0].ndim > self.axis,
            in_types[0].ndim >= -self.axis
        )

    def get_output_shape(self, input_shape):
        output_shape = list(input_shape)
        output_shape[self.axis] *= 2
        return tuple(output_shape)

    def forward(self, x):
        x, = x
        xp = cuda.get_array_module(x)
        y = xp.empty(self.get_output_shape(x.shape), dtype=x.dtype)
        y_former, y_latter = xp.split(y, 2, axis=self.axis)
        zero = x.dtype.type(0)
        xp.maximum(zero, x, out=y_former)
        xp.maximum(zero, -x, out=y_latter)
        return y,

    def backward(self, x, gy):
        x, = x
        xp = cuda.get_array_module(x)
        gy, = gy
        gy_former, gy_latter = xp.split(gy, 2, axis=self.axis)
        return gy_former * (x > 0) - gy_latter * (-x > 0),


def crelu(x, axis=1):
    """Concatenated Rectified Linear Unit function.

    This function is expressed as follows

     .. math:: f(x) = (\\max(0, x), \\max(0, -x)).

    Here, two output values are concatenated along an axis.

    See: https://arxiv.org/abs/1603.05201

    Args:
        x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
        :class:`cupy.ndarray`):
            Input variable. A :math:`(s_1, s_2, ..., s_n)`-shaped float array.
        axis (int): Axis that the output values are concatenated along.
            Default is 1.

    Returns:
        ~chainer.Variable: Output variable of concatenated array.
        If the axis is 1, A :math:`(s_1, s_2 \\times 2, ..., s_n)`-shaped float
        array.

    .. admonition:: Example

        >>> x = np.random.uniform(-10, 10, (3, 2)).astype('f')
        >>> x
        array([[ 0.97627008,  4.30378723],
               [ 2.05526757,  0.89766365],
               [-1.52690399,  2.9178822 ]], dtype=float32)
        >>> y = F.crelu(x, axis=1)<commit_msg>changed shape expression<commit_after>from chainer import cuda
from chainer import function
from chainer.utils import type_check


class CReLU(function.Function):

    """Concatenated Rectified Linear Unit."""

    def __init__(self, axis=1):
        if not isinstance(axis, int):
            raise TypeError('axis must be an integer value')
        self.axis = axis

    def check_type_forward(self, in_types):
        type_check.expect(
            in_types.size() == 1,
            in_types[0].dtype.kind == 'f',
            in_types[0].ndim > self.axis,
            in_types[0].ndim >= -self.axis
        )

    def get_output_shape(self, input_shape):
        output_shape = list(input_shape)
        output_shape[self.axis] *= 2
        return tuple(output_shape)

    def forward(self, x):
        x, = x
        xp = cuda.get_array_module(x)
        y = xp.empty(self.get_output_shape(x.shape), dtype=x.dtype)
        y_former, y_latter = xp.split(y, 2, axis=self.axis)
        zero = x.dtype.type(0)
        xp.maximum(zero, x, out=y_former)
        xp.maximum(zero, -x, out=y_latter)
        return y,

    def backward(self, x, gy):
        x, = x
        xp = cuda.get_array_module(x)
        gy, = gy
        gy_former, gy_latter = xp.split(gy, 2, axis=self.axis)
        return gy_former * (x > 0) - gy_latter * (-x > 0),


def crelu(x, axis=1):
    """Concatenated Rectified Linear Unit function.

    This function is expressed as follows

     .. math:: f(x) = (\\max(0, x), \\max(0, -x)).

    Here, two output values are concatenated along an axis.

    See: https://arxiv.org/abs/1603.05201

    Args:
        x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
        :class:`cupy.ndarray`):
            Input variable. A :math:`(s_1, s_2, ..., s_N)`-shaped float array.
        axis (int): Axis that the output values are concatenated along.
            Default is 1.

    Returns:
        ~chainer.Variable: Output variable of concatenated array.
        If the axis is 1, A :math:`(s_1, s_2 \\times 2, ..., s_N)`-shaped float
        array.

    .. admonition:: Example

        >>> x = np.random.uniform(-10, 10, (3, 2)).astype('f')
        >>> x
        array([[ 0.97627008,  4.30378723],
               [ 2.05526757,  0.89766365],
               [-1.52690399,  2.9178822 ]], dtype=float32)
        >>> y = F.crelu(x, axis=1)
{'fraction_non_alphanumeric': 0.13755041392485673, 'fraction_numerical': 0.03905752494162598, 'mean_word_length': 2.14342895263509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4136074', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1597, 'n_words': 401}