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


Python testing.assert_allclose方法代码示例

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


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

示例1: check_forward

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def check_forward(self, x_data):
        xp = cuda.get_array_module(x_data)
        y = maximum_entropy_mellowmax(x_data)
        self.assertEqual(y.array.dtype, self.dtype)

        print('y', y.array)

        # Outputs must be positive
        xp.testing.assert_array_less(xp.zeros_like(y.array), y.array)

        # Sums must be ones
        sums = xp.sum(y.array, axis=1)
        testing.assert_allclose(sums, xp.ones_like(sums))

        # Expectations must be equal to memllowmax's outputs
        testing.assert_allclose(
            xp.sum(y.array * x_data, axis=1), mellowmax(x_data, axis=1).array) 
开发者ID:chainer,项目名称:chainerrl,代码行数:19,代码来源:test_mellowmax.py

示例2: check_backward

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def check_backward(self, s_data, i_data, gx_data, gt_data):
        # We cannot use check_backward method as a thin stack reuses ndarray.
        gt_old = gt_data.copy()
        s = chainer.Variable(s_data)
        i = chainer.Variable(i_data)
        x, t = thin_stack.thin_stack_get(s, i)
        x.grad = gx_data
        t.grad = gt_data

        t.backward()
        for j, ind in enumerate(i_data):
            for k in range(self.shape[1]):
                if k == ind:
                    testing.assert_allclose(
                        s.grad[j, k], gt_old[j, k] + gx_data[j])
                else:
                    testing.assert_allclose(
                        s.grad[j, k], gt_old[j, k])

        self.assertIsNone(i.grad)
        # Thin stack reueses the same gradient array.
        self.assertIs(s.grad, gt_data) 
开发者ID:chainer,项目名称:chainer,代码行数:24,代码来源:test_thin_stack.py

示例3: check_serialize

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def check_serialize(self, value1, value2, value3):
        xp = chainer.backend.get_array_module(value1, value2, value3)
        self.summary.add(value1)
        self.summary.add(value2)

        summary = chainer.reporter.Summary()
        testing.save_and_load_npz(self.summary, summary)
        summary.add(value3)

        expected_mean = (value1 + value2 + value3) / 3.
        expected_std = xp.sqrt(
            (value1**2 + value2**2 + value3**2) / 3. - expected_mean**2)

        mean = summary.compute_mean()
        testing.assert_allclose(mean, expected_mean)

        mean, std = summary.make_statistics()
        testing.assert_allclose(mean, expected_mean)
        testing.assert_allclose(std, expected_std) 
开发者ID:chainer,项目名称:chainer,代码行数:21,代码来源:test_reporter.py

示例4: test_serialize_backward_compat

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def test_serialize_backward_compat(self):
        with tempfile.NamedTemporaryFile(delete=False) as f:
            # old version does not save anything
            numpy.savez(f, dummy=0)
            with testing.assert_warns(UserWarning):
                chainer.serializers.load_npz(f.name, self.summary)

        self.summary.add(2.)
        self.summary.add(3.)

        mean = self.summary.compute_mean()
        testing.assert_allclose(mean, 2.5)

        mean, std = self.summary.make_statistics()
        testing.assert_allclose(mean, 2.5)
        testing.assert_allclose(std, 0.5) 
开发者ID:chainer,项目名称:chainer,代码行数:18,代码来源:test_reporter.py

示例5: check

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def check(self, summary, data):
        mean = summary.compute_mean()
        self.assertEqual(set(mean.keys()), set(data.keys()))
        for name in data.keys():
            m = sum(data[name]) / float(len(data[name]))
            testing.assert_allclose(mean[name], m)

        stats = summary.make_statistics()
        self.assertEqual(
            set(stats.keys()),
            set(data.keys()).union(name + '.std' for name in data.keys()))
        for name in data.keys():
            m = sum(data[name]) / float(len(data[name]))
            s = numpy.sqrt(
                sum(x * x for x in data[name]) / float(len(data[name]))
                - m * m)
            testing.assert_allclose(stats[name], m)
            testing.assert_allclose(stats[name + '.std'], s) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_reporter.py

示例6: check_double_grad

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [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

示例7: test_retain_output

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [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

示例8: test_unchain_split

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def test_unchain_split(self):
        x = chainer.Variable(numpy.arange(4).astype('f').reshape(2, 2))
        h0, h1 = chainer.functions.split_axis(x, [1], axis=0)
        y = chainer.functions.sum(h0)
        z = chainer.functions.sum(h1)
        w = y + z
        h0.unchain()

        dy_dh0 = numpy.array([[1., 1.]])
        dz_dh1 = numpy.array([[1., 1.]])
        dy_dx = None
        dz_dx = numpy.array([[0., 0.], [1., 1.]])
        dw_dx = numpy.array([[0., 0.], [1., 1.]])

        testing.assert_allclose(chainer.grad([y], [h0])[0].array, dy_dh0)
        testing.assert_allclose(chainer.grad([z], [h1])[0].array, dz_dh1)
        assert chainer.grad([y], [x])[0] is dy_dx
        testing.assert_allclose(chainer.grad([z], [x])[0].array, dz_dx)
        testing.assert_allclose(chainer.grad([w], [x])[0].array, dw_dx) 
开发者ID:chainer,项目名称:chainer,代码行数:21,代码来源:test_function_node.py

示例9: check

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [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

示例10: test_multiple_output_call_count

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def test_multiple_output_call_count(self):
        x = chainer.Variable(np.array([1, 2], np.float32))

        f = chainer.FunctionNode()
        f.forward = mock.MagicMock(
            side_effect=lambda xs: tuple(x * 2 for x in xs))
        f.backward = mock.MagicMock(
            side_effect=lambda _, gys: tuple(gy * 2 for gy in gys))

        h, = f.apply((x,))
        y0 = h * 3
        y1 = h * 4
        y0.grad = np.array([1, 10], np.float32)
        y1.grad = np.array([100, 1000], np.float32)
        chainer.backward([y0, y1])
        testing.assert_allclose(x.grad, np.array([806, 8060], np.float32))
        assert f.backward.call_count == 1 
开发者ID:chainer,项目名称:chainer,代码行数:19,代码来源:test_backprop.py

示例11: _run_trainer

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def _run_trainer(self, extension, expect, optimizer=None):
        if optimizer is None:
            optimizer = self.optimizer
        extension.initialize(self.trainer)
        actual = []
        for _ in expect:
            self.trainer.updater.update()
            actual.append(optimizer.x)
            if self.trigger(self.trainer):
                extension(self.trainer)

        testing.assert_allclose(actual[0], expect[0])
        testing.assert_allclose(actual[1], expect[1])
        testing.assert_allclose(actual[2], expect[2])
        testing.assert_allclose(actual[3], expect[3])
        testing.assert_allclose(actual[4], expect[4])
        testing.assert_allclose(actual[5], expect[5]) 
开发者ID:chainer,项目名称:chainer,代码行数:19,代码来源:test_multistep_shift.py

示例12: test_statistician_percentile

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def test_statistician_percentile(self):
        self.percentile_sigmas = (0., 50., 100.)  # min, median, max
        self.statistician = extensions.variable_statistics_plot.Statistician(
            collect_mean=True, collect_std=True,
            percentile_sigmas=self.percentile_sigmas)
        stat = self.statistician(self.x, axis=None, dtype=self.x.dtype)

        for s in six.itervalues(stat):
            assert s.dtype == self.x.dtype

        testing.assert_allclose(stat['mean'], numpy.mean(self.x))
        testing.assert_allclose(stat['std'], numpy.std(self.x))

        percentile = stat['percentile']
        assert len(percentile) == 3

        testing.assert_allclose(percentile[0], numpy.min(self.x))
        testing.assert_allclose(percentile[1], numpy.median(self.x))
        testing.assert_allclose(percentile[2], numpy.max(self.x)) 
开发者ID:chainer,项目名称:chainer,代码行数:21,代码来源:test_variable_statistics_plot.py

示例13: test_update

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def test_update(self, backend_config):
        if backend_config.xp is chainerx:
            # ChainerX performs the loss scaling on its own backward
            # method, the optimizer should not divide back the parameters
            # This test is not actually creating a ChainerX
            # computation graph so no actual loss scale is being done
            self.optimizer.lr = 1.0
        target = self.target
        optimizer = self.optimizer
        target.to_device(backend_config.device)
        optimizer.setup(target)
        optimizer.update()
        xp = backend.get_array_module(target[0].param)
        expected_data = xp.zeros(self.shape, dtype=self.dtype)
        rtol, atol = 1e-4, 1e-5
        if self.dtype is np.float16:
            rtol, atol = 1e-1, 1e-2
        for i in range(2):
            testing.assert_allclose(
                target[i].param.data, expected_data,
                rtol=rtol, atol=atol) 
开发者ID:chainer,项目名称:chainer,代码行数:23,代码来源:test_optimizer.py

示例14: test_adam_w

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [as 别名]
def test_adam_w(self, backend_config):
        xp = backend_config.xp
        device = backend_config.device

        link = chainer.Link(x=(1,))
        link.to_device(device)

        opt = optimizers.Adam(eta=0.5, weight_decay_rate=0.1)
        opt.setup(link)

        link.x.data.fill(1)
        link.x.grad = device.send(xp.ones_like(link.x.data))

        opt.update()

        # compare against the value computed with v5 impl
        testing.assert_allclose(link.x.data, np.array([0.9495]),
                                atol=1e-7, rtol=1e-7) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_optimizers.py

示例15: check_backward_accumulate

# 需要导入模块: from chainer import testing [as 别名]
# 或者: from chainer.testing import assert_allclose [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


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