当前位置: 首页>>代码示例>>Python>>正文


Python chainer.grad方法代码示例

本文整理汇总了Python中chainer.grad方法的典型用法代码示例。如果您正苦于以下问题:Python chainer.grad方法的具体用法?Python chainer.grad怎么用?Python chainer.grad使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainer的用法示例。


在下文中一共展示了chainer.grad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_type_check

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def test_type_check(self):
        x = chainer.Variable(numpy.random.uniform(-1, 1, (2, 3)).astype('f'))
        y = x * x
        gx = chainer.Variable(numpy.random.uniform(-1, 1, (2, 3)).astype('f'))
        gy = chainer.Variable(numpy.random.uniform(-1, 1, (2, 3)).astype('f'))

        chainer.grad([y], [x], [gx], [gy])
        chainer.grad((y,), (x,), (gx,), (gy,))

        with self.assertRaises(TypeError):
            chainer.grad(y, [x], [gx], [gy])
        with self.assertRaises(TypeError):
            chainer.grad([y], x, [gx], [gy])
        with self.assertRaises(TypeError):
            chainer.grad([y], [x], gx, [gy])
        with self.assertRaises(TypeError):
            chainer.grad([y], [x], [gx], gy) 
开发者ID:chainer,项目名称:chainer,代码行数:19,代码来源:test_function_node.py

示例2: check_double_grad

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def check_double_grad(self):
        self.forward()
        ys = [getattr(self, name) for name in self.y_names]
        gxs = chainer.grad(ys, self.xs, self.gys, self.gxs,
                           enable_double_backprop=True,
                           loss_scale=self.loss_scale)
        y = sum(gxs)
        ggxs = chainer.grad([y], self.xs)

        expected = self.expected_double_grad()
        self.assertEqual(len(ggxs), len(expected))
        try:
            for a, e in zip(ggxs, expected):
                testing.assert_allclose(self._get_value(a), self._get_value(e))
        except Exception:
            self._print_inputs()
            self._print_variables('gxs            ', gxs)
            self._print_variables('ggxs (actual)  ', ggxs)
            self._print_variables('ggxs (expected)', expected)
            raise 
开发者ID:chainer,项目名称:chainer,代码行数:22,代码来源:test_function_node.py

示例3: test_retain_output

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def test_retain_output(self):
        xp = numpy
        x_array = xp.random.randn(3)
        y1_grad = xp.random.randn(3)
        x_grad_grad = xp.random.randn(3)

        x = chainer.Variable(x_array, name='x')
        y0, y1 = exp_and_expm1(x)
        del y0

        # (x: Variable) requires grad
        # (y1_grad: ndarray) does not require grad
        gx, = chainer.grad([y1], [x], [y1_grad], enable_double_backprop=True)

        # assert gx == exp(x) * y1_grad
        xp.testing.assert_allclose(
            gx.array,
            xp.exp(x.array) * y1_grad)

        gx_, = chainer.grad([gx], [x], [x_grad_grad])
        xp.testing.assert_allclose(
            gx_.array,
            gx.array * x_grad_grad) 
开发者ID:chainer,项目名称:chainer,代码行数:25,代码来源:test_function_node.py

示例4: check

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def check(self, option, grads_before, grads_after):
        vs = []
        v = self._var(0.5)
        for _ in range(4):
            vs.append(v)
            v += v
            vs.append(v)
            v *= 1.
        _, x1, _, x2, _, y1, _, y2 = vs
        gx1 = self._var(1000.)
        gx2 = self._var(100.)
        gy1 = self._var(10.)
        gy2 = self._var(1.)
        for v, g in zip(vs, grads_before):
            if g is not None:
                v.grad_var = self._var(g)
        grads = chainer.grad(
            [y1, y2], [x1, x2], [gy1, gy2], [gx1, gx2], **option)
        numpy.testing.assert_allclose(grads[0].array, 1248.)
        numpy.testing.assert_allclose(grads[1].array, 124.)
        for v, ans in zip(vs, grads_after):
            if ans is None:
                self.assertIsNone(v.grad)
            else:
                numpy.testing.assert_allclose(v.grad, ans) 
开发者ID:chainer,项目名称:chainer,代码行数:27,代码来源:test_function_node.py

示例5: check_backward_accumulate

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def check_backward_accumulate(self, xp):
        inputs = self._get_inputs()
        a, b, c = [inputs[i] for i in self.var_mapping]
        y = muladd(a, b, c)
        y.grad = self.gy
        y.backward()

        inputs2 = self._get_inputs()
        a2, b2, c2 = [inputs2[i] for i in self.var_mapping]
        y2 = chainer.as_variable(a2 * b2 + c2)
        y2.grad = self.gy
        y2.backward()

        tol = {'atol': 1e-4, 'rtol': 1e-4}
        for x, x2, (isvar, _) in zip(
                inputs, inputs2, self.inputs_isvar_hasgrad):
            if isvar:
                xp.testing.assert_allclose(x.grad, x2.grad, **tol) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_variable.py

示例6: test_grad

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def test_grad(self, backend_config):
        x = backend_config.get_array(
            np.random.uniform(-1, 1, self.shape).astype(np.float32))
        g = backend_config.get_array(
            np.random.uniform(0.1, 10, self.shape).astype(np.float32))
        v = chainer.Variable(x, requires_grad=self.requires_grad)
        expected_error = (
            backend_config.xp is chainerx
            and not self.requires_grad)

        if expected_error:
            with pytest.raises(Exception):
                v.grad = g
        else:
            v.grad = g

            assert v.grad_var.requires_grad is True
            assert v.grad is not None
            assert v.requires_grad == self.requires_grad
            backend_config.xp.testing.assert_array_equal(v.grad, g) 
开发者ID:chainer,项目名称:chainer,代码行数:22,代码来源:test_variable.py

示例7: check_grad_var

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def check_grad_var(self, backend_config, grad_var_requires_grad):
        x = backend_config.get_array(
            np.random.uniform(-1, 1, self.shape).astype(np.float32))
        g = backend_config.get_array(
            np.random.uniform(0.1, 10, self.shape).astype(np.float32))
        v = chainer.Variable(x, requires_grad=self.requires_grad)
        gv = chainer.Variable(g, requires_grad=grad_var_requires_grad)
        expected_error = (
            backend_config.xp is chainerx
            and not self.requires_grad)

        if expected_error:
            with pytest.raises(Exception):
                v.grad_var = gv
        else:
            v.grad_var = gv

            assert v.requires_grad == self.requires_grad
            backend_config.xp.testing.assert_array_equal(v.grad, g)

            # Same instance should be returned each time.
            assert v.grad_var is gv 
开发者ID:chainer,项目名称:chainer,代码行数:24,代码来源:test_variable.py

示例8: test_from_chx

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def test_from_chx(self, backend_config):
        x = backend_config.get_array(self.x)
        x_var = chainer.Variable(x, requires_grad=False)
        x_var.from_chx()

        expected_xp, expected_device = self.infer_expected_xp_and_device(x)

        assert x_var.xp is expected_xp
        assert x_var._has_chainerx_array is (expected_xp is chainerx)
        assert x_var.node is not None
        assert isinstance(x_var.array, expected_xp.ndarray)
        assert expected_device is None or x_var.array.device == expected_device
        assert x.shape == x_var.shape
        assert x.dtype == x_var.dtype
        assert x_var.grad is None
        assert x_var.grad_var is None
        np.testing.assert_array_equal(
            backend.CpuDevice().send(x_var.array), backend.CpuDevice().send(x)) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_variable.py

示例9: _compute_backward

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def _compute_backward(self, x, gamma, beta, y, gy):
        assert isinstance(x, chainer.Variable)
        assert isinstance(gamma, chainer.Variable)
        assert isinstance(beta, chainer.Variable)
        assert isinstance(y, chainer.Variable)
        assert isinstance(gy, chainer.Variable)

        if x.xp is chainerx:
            # TODO(niboshi): ChainerX does not support grad yet
            y.grad = gy.array.copy()
            y.backward()
            gx = x.grad_var
            ggamma = gamma.grad_var
            gbeta = beta.grad_var
        else:
            gx, ggamma, gbeta = chainer.grad([y], [x, gamma, beta], [gy])
        return gx.array, ggamma.array, gbeta.array 
开发者ID:chainer,项目名称:chainer,代码行数:19,代码来源:test_batch_renormalization.py

示例10: _test_replace_func

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def _test_replace_func(self, fn, xs, set_grad=False):
        def make_list(v):
            if isinstance(v, (list, tuple)):
                return list(v)
            else:
                return [v]

        xvs = [x for x in xs if isinstance(x, chainer.Variable)]
        rfn = as_funcnode('fn')(fn)
        eys = make_list(fn(*xs))
        egxs = chainer.grad(eys, xvs, set_grad=set_grad)
        ays = make_list(rfn(*xs))
        agxs = chainer.grad(ays, xvs, set_grad=set_grad)
        assert len(eys) == len(ays)
        for ay, ey in zip(ays, eys):
            np.testing.assert_allclose(ay.array, ey.array)
        assert len(egxs) == len(agxs)
        for agx, egx in zip(agxs, egxs):
            if egx is None:
                assert egx is None
            else:
                np.testing.assert_allclose(agx.array, egx.array) 
开发者ID:chainer,项目名称:chainer,代码行数:24,代码来源:test_replace_func.py

示例11: test_backward_no_side_effect

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def test_backward_no_side_effect(self):
        a = chainer.Variable(np.array(2.3))
        b = chainer.Variable(np.array(4.2))
        x0 = a * b
        x1 = chainer.Variable(np.array(3.7))
        self._test_replace_func(lambda a, b: a * b, [x0, x1])
        # No side-effect to `grad`.
        assert x0.grad is None
        assert x1.grad is None
        assert a.grad is None
        assert b.grad is None
        # Gradient computation must stop at `x0` and `x1`.
        self._test_replace_func(lambda a, b: a * b, [x0, x1], set_grad=True)
        assert x0.grad is not None
        assert x1.grad is not None
        assert a.grad is None
        assert b.grad is None 
开发者ID:chainer,项目名称:chainer,代码行数:19,代码来源:test_replace_func.py

示例12: run_param_post_hooks

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def run_param_post_hooks(self):
        """Update parameter attributes after schedule is executed.

        If any dynamically-allocated arrays in the schedule correspond to
        a parameter attribute, it must be updated after the schedule is
        run.
        """
        if self.verbosity_level >= 2:
            print('run_param_post_hooks()...')
        for hook in self.param_post_hooks:
            (unique_array_index, param_attribute_location) = hook
            (params_list_index, attribute_location) = param_attribute_location
            if attribute_location == 'data':
                self.params_list[params_list_index].data = \
                    self.unique_arrays[unique_array_index]
            elif attribute_location == 'grad':
                self.params_list[params_list_index].grad = \
                    self.unique_arrays[unique_array_index] 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:static_graph.py

示例13: compute_hessian_vector_product

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def compute_hessian_vector_product(y, params, vec):
    grads = chainer.grad(
        [y], params, enable_double_backprop=True)
    flat_grads = trpo._flatten_and_concat_variables(grads)
    return trpo._hessian_vector_product(flat_grads, params, vec) 
开发者ID:chainer,项目名称:chainerrl,代码行数:7,代码来源:test_trpo.py

示例14: compute_hessian

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def compute_hessian(y, params):
    grads = chainer.grad(
        [y], params, enable_double_backprop=True)
    flat_grads = trpo._flatten_and_concat_variables(grads)
    hessian_rows = []
    for i in range(len(flat_grads)):
        ggrads = chainer.grad([flat_grads[i]], params)
        assert all(ggrad is not None for ggrad in ggrads)
        ggrads_data = [ggrad.array for ggrad in ggrads]
        flat_ggrads_data = trpo._flatten_and_concat_ndarrays(ggrads_data)
        hessian_rows.append(flat_ggrads_data)
    return np.asarray(hessian_rows) 
开发者ID:chainer,项目名称:chainerrl,代码行数:14,代码来源:test_trpo.py

示例15: compute_loss_with_kl_constraint

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import grad [as 别名]
def compute_loss_with_kl_constraint(distrib, another_distrib, original_loss,
                                    delta):
    """Compute loss considering a KL constraint.

    Args:
        distrib (Distribution): Distribution to optimize
        another_distrib (Distribution): Distribution used to compute KL
        original_loss (chainer.Variable): Loss to minimize
        delta (float): Minimum KL difference
    Returns:
        loss (chainer.Variable)
    """
    for param in distrib.params:
        assert param.shape[0] == 1
        assert param.requires_grad
    # Compute g: a direction to minimize the original loss
    g = [grad.array[0] for grad in
         chainer.grad([F.squeeze(original_loss)], distrib.params)]

    # Compute k: a direction to increase KL div.
    kl = F.squeeze(another_distrib.kl(distrib))
    k = [grad.array[0] for grad in
         chainer.grad([-kl], distrib.params)]

    # Compute z: combination of g and k to keep small KL div.
    kg_dot = sum(np.dot(kp.ravel(), gp.ravel())
                 for kp, gp in zip(k, g))
    kk_dot = sum(np.dot(kp.ravel(), kp.ravel()) for kp in k)
    if kk_dot > 0:
        k_factor = max(0, ((kg_dot - delta) / kk_dot))
    else:
        k_factor = 0
    z = [gp - k_factor * kp for kp, gp in zip(k, g)]
    loss = 0
    for p, zp in zip(distrib.params, z):
        loss += F.sum(p * zp)
    return F.reshape(loss, original_loss.shape), float(kl.array) 
开发者ID:chainer,项目名称:chainerrl,代码行数:39,代码来源:acer.py


注:本文中的chainer.grad方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。