Python chainer.utils() Examples

The following are 30 code examples of chainer.utils(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer , or try the search function .
Example #1
Source File: bernoulli.py    From chainer with MIT License 6 votes vote down vote up
def forward(self, inputs):
        logit, x = inputs
        self.retain_inputs((0, 1))
        xp = backend.get_array_module(x)
        y = logit * (x - 1) - xp.log(xp.exp(-logit) + 1)
        y = utils.force_array(y)

        # extreme logit
        logit_isinf = xp.isinf(logit)
        self.logit_ispinf = xp.bitwise_and(logit_isinf, logit > 0)
        self.logit_isminf = xp.bitwise_and(logit_isinf, logit <= 0)
        with numpy.errstate(divide='ignore', invalid='raise'):
            y = xp.where(self.logit_ispinf, xp.log(x), y)
            y = xp.where(self.logit_isminf, xp.log(1 - x), y)

        if self.binary_check:
            self.invalid = utils.force_array(xp.bitwise_and(x != 0, x != 1))
            y[self.invalid] = -xp.inf

        return utils.force_array(y, logit.dtype), 
Example #2
Source File: basic_math.py    From chainer with MIT License 6 votes vote down vote up
def forward(self, xs):
        self.len = len(xs)
        if len(xs) == 1:
            return xs
        if (intel64.should_use_ideep('>=auto')
                and intel64.inputs_all_ready(xs)
                and all(x.shape == xs[0].shape for x in xs[1:])):
            y = intel64.ideep.multi_add(xs)
        else:
            # The output should be a new array. Add the first 2 arrays
            # and get the result y. Then add the rest arrays to y.
            y = xs[0] + xs[1]
            for x in xs[2:]:
                if x.shape == y.shape:
                    y += x
                else:
                    y = x + y

        return utils.force_array(y), 
Example #3
Source File: erfc.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, x):
        global _erfc_cpu
        if _erfc_cpu is None:
            try:
                from scipy import special
                _erfc_cpu = special.erfc
            except ImportError:
                warnings.warn(
                    'SciPy is not available. Forward computation of erfc in'
                    ' CPU can be slow without SciPy.',
                    chainer.warnings.PerformanceWarning)
                _erfc_cpu = numpy.vectorize(math.erfc)
        self.retain_inputs((0,))
        return utils.force_array(_erfc_cpu(x[0]), dtype=x[0].dtype), 
Example #4
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
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 
Example #5
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, x):
        self.retain_inputs((0, 1))
        # may broadcast
        return utils.force_array(x[0] / x[1]), 
Example #6
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs):
        self.retain_inputs((0, 1, 2))
        x0, x1, gy = inputs
        gx0 = utils.force_array(gy / x1)
        gx1 = utils.force_array(-gx0 * x0 / x1)
        return utils.sum_to(gx0, x0.shape), utils.sum_to(gx1, x1.shape) 
Example #7
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, x):
        self.retain_inputs((0,))
        value = _preprocess_const(x[0], self.value)
        return utils.force_array(value / x[0]), 
Example #8
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs):
        self.retain_inputs((0, 1))
        x, gy = inputs
        value = _preprocess_const(x, self.value)
        return utils.force_array(-value * gy / (x ** 2)), 
Example #9
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, x):
        self.retain_inputs((0, 1))
        # may broadcast
        self.y = x[0] ** x[1]
        return utils.force_array(self.y), 
Example #10
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs):
        self.retain_inputs((0, 1, 2))
        x0, x1, gy = inputs

        one = x1.dtype.type(1)
        gx0 = utils.sum_to(
            utils.force_array(x1 * (x0 ** (x1 - one)) * gy), x0.shape)
        gx1 = utils.sum_to(
            utils.force_array(numpy.log(x0) * self.y * gy), x1.shape)
        return gx0, gx1 
Example #11
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward_gpu(self, inputs):
        self.retain_inputs((0, 1, 2))
        x0, x1, gy = inputs

        gx0, gx1 = cuda.elementwise(
            'T x0, T x1, T gy, T y', 'T gx0, T gx1',
            '''
            gx0 = x1 * pow(x0, x1 - 1) * gy;
            gx1 = log(x0) * y * gy;
            ''', 'pow_var_var_bwd')(x0, x1, gy, self.y)

        gx0 = utils.sum_to(gx0, x0.shape)
        gx1 = utils.sum_to(gx1, x1.shape)

        return gx0, gx1 
Example #12
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs):
        self.retain_inputs((0, 1))
        x, gy = inputs

        self.val_1 = _preprocess_const(x, self.value - 1)
        gx = utils.force_type(x.dtype, self.value) * (x ** self.val_1) * gy
        gx = utils.force_array(gx)
        return gx, 
Example #13
Source File: basic_math.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, x):
        value = _preprocess_const(x[0], self.value)
        return utils.force_array(value * x[0]), 
Example #14
Source File: lgamma.py    From chainer with MIT License 5 votes vote down vote up
def forward_gpu(self, x):
        self.retain_inputs((0,))
        return utils.force_array(
            cuda.cupyx.scipy.special.gammaln(x[0]), dtype=x[0].dtype), 
Example #15
Source File: lgamma.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, x):
        global _lgamma_cpu
        if _lgamma_cpu is None:
            try:
                from scipy import special
                _lgamma_cpu = special.gammaln
            except ImportError:
                raise ImportError('SciPy is not available. Forward computation'
                                  ' of lgamma can not be done.')
        self.retain_inputs((0,))
        return utils.force_array(_lgamma_cpu(x[0]), dtype=x[0].dtype), 
Example #16
Source File: digamma.py    From chainer with MIT License 5 votes vote down vote up
def forward_gpu(self, x):
        self.retain_inputs((0,))
        return utils.force_array(
            cuda.cupyx.scipy.special.digamma(x[0]), dtype=x[0].dtype), 
Example #17
Source File: digamma.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, x):
        global _digamma_cpu
        if _digamma_cpu is None:
            try:
                from scipy import special
                _digamma_cpu = special.digamma
            except ImportError:
                raise ImportError('SciPy is not available. Forward computation'
                                  ' of digamma can not be done.')
        self.retain_inputs((0,))
        return utils.force_array(_digamma_cpu(x[0]), dtype=x[0].dtype), 
Example #18
Source File: average.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs):
        x, = inputs
        if self.axis is None:
            self.multiplier = 1.0 / x.size
        else:
            divider = 1
            for axis in self.axis:
                divider *= x.shape[axis]
            self.multiplier = 1.0 / divider
        ret = utils.force_array(
            x.mean(axis=self.axis, keepdims=self.keepdims))
        return ret, 
Example #19
Source File: erfcinv.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, x):
        if not available_cpu:
            raise ImportError('SciPy is not available. Forward computation'
                              ' of erfcinv in CPU cannot be done. ' +
                              str(_import_error))
        self.retain_outputs((0,))
        return utils.force_array(special.erfcinv(x[0]), dtype=x[0].dtype), 
Example #20
Source File: ndtr.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, x):
        global _ndtr_cpu
        if _ndtr_cpu is None:
            try:
                from scipy import special
                _ndtr_cpu = special.ndtr
            except ImportError:
                warnings.warn(
                    'SciPy is not available. Forward computation of ndtr in'
                    ' CPU can be slow without SciPy.',
                    chainer.warnings.PerformanceWarning)
                _ndtr_cpu = numpy.vectorize(_slow_ndtr_cpu)
        self.retain_inputs((0,))
        return utils.force_array(_ndtr_cpu(x[0]), dtype=x[0].dtype), 
Example #21
Source File: sparse_matmul.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs):
        self.retain_inputs((0, 1))
        a, b = inputs
        c = _coo_matmul_gradsp(a, b, self.sp_row, self.sp_col, self.sp_shape,
                               self.transa, self.transb, self.transc,
                               self.dtype)
        return utils.force_array(c), 
Example #22
Source File: sparse_matmul.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs):
        self.retain_inputs((0, 1))
        sp, dn = inputs
        c = _coo_matmul(sp, self.sp_row, self.sp_col, self.sp_shape,
                        self.sp_order, dn,
                        self.transa, self.transb, self.transc, self.dtype)
        return utils.force_array(c, self.dtype), 
Example #23
Source File: inv.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, x):
        self.retain_outputs((0,))
        try:
            invx = utils.force_array(numpy.linalg.inv(x[0]))
        except numpy.linalg.LinAlgError:
            raise ValueError('Input has singular matrices.')
        return invx, 
Example #24
Source File: maximum.py    From chainer with MIT License 5 votes vote down vote up
def backward(self, indexes, grad_outputs):
        return chainer.functions.where(
            utils.force_array(self.cond), grad_outputs[0], grad_outputs[1]), 
Example #25
Source File: maximum.py    From chainer with MIT License 5 votes vote down vote up
def forward_gpu(self, inputs):
        gy, = inputs
        gx1, gx2 = cuda.elementwise(
            'S cond, T gy', 'T gx1, T gx2',
            '''
            gx1 = cond ? gy : (T)0.0;
            gx2 = cond ? (T)0.0 : gy;
            ''',
            'maximum_bwd1')(self.cond, gy)
        return (
            utils.sum_to(gx1, self.x1_shape),
            utils.sum_to(gx2, self.x2_shape)) 
Example #26
Source File: maximum.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs):
        gy, = inputs
        gx1 = utils.force_array(numpy.where(self.cond, gy, gy.dtype.type(0)))
        gx2 = utils.force_array(numpy.where(self.cond, gy.dtype.type(0), gy))
        return (
            utils.sum_to(gx1, self.x1_shape),
            utils.sum_to(gx2, self.x2_shape)) 
Example #27
Source File: maximum.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs):
        # may broadcast
        self.retain_inputs((0, 1))
        x1, x2 = inputs
        y = numpy.maximum(x1, x2)
        return utils.force_array(y), 
Example #28
Source File: gaussian.py    From chainer with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs):
        self.retain_inputs((1,))

        mean, ln_var = inputs
        if self.eps is None:
            self.eps = (
                numpy.random.standard_normal(ln_var.shape)
                .astype(mean.dtype, copy=False)
            )

        self.noise = numpy.exp(ln_var * mean.dtype.type(0.5)) * self.eps
        return utils.force_array(mean + self.noise), 
Example #29
Source File: functions.py    From chainer-PGGAN with MIT License 5 votes vote down vote up
def forward(self, x):
        self.retain_outputs((0,))
        xp = cuda.get_array_module(*x)
        return utils.force_array(xp.sqrt(x[0], dtype=x[0].dtype)), 
Example #30
Source File: get_item.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, xs):
        slices = tuple([
            backend.from_chx(s) if isinstance(s, chainerx.ndarray) else s
            for s in self.slices])
        return utils.force_array(xs[0][slices]),