Python chainer.testing.assert_allclose() Examples
The following are 30
code examples of chainer.testing.assert_allclose().
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.testing
, or try the search function
.
Example #1
Source File: test_zoneoutlstm.py From chainer with MIT License | 6 votes |
def check_backward(self, c_data, h_data, x_data, y_grad): x = chainer.Variable(x_data) y = self._forward(self.link, x) c = self.link.c d = {'c_creator': c.creator, 'y_creator': y.creator} y.grad = y_grad y.backward() def f(): c_creator = d['c_creator'] y_creator = d['y_creator'] c, y = _zoneoutlstm(self.link, c_data, h_data, x_data, c_creator, y_creator) return y, gx, = gradient_check.numerical_grad(f, (x.data,), (y_grad,)) testing.assert_allclose(gx, x.grad, atol=1e-3)
Example #2
Source File: test_reporter.py From chainer with MIT License | 6 votes |
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)
Example #3
Source File: test_reporter.py From chainer with MIT License | 6 votes |
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)
Example #4
Source File: test_function_node.py From chainer with MIT License | 6 votes |
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)
Example #5
Source File: test_function_node.py From chainer with MIT License | 6 votes |
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
Example #6
Source File: test_reporter.py From chainer with MIT License | 6 votes |
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)
Example #7
Source File: test_link_n_step_rnn.py From chainer with MIT License | 6 votes |
def check_param(self): weight_initializer, bias_initializer = self.get_initializers() link = self.link xp = link.xp dtype = self.dtype for ws_i in link.ws: for w in ws_i: assert w.dtype == dtype w_expected = xp.empty(w.shape, dtype) weight_initializer(w_expected) testing.assert_allclose( w.array, w_expected, atol=0, rtol=0) for bs_i in link.bs: for b in bs_i: assert b.dtype == dtype b_expected = xp.empty(b.shape, dtype) bias_initializer(b_expected) testing.assert_allclose( b.array, b_expected, atol=0, rtol=0)
Example #8
Source File: test_dilated_convolution_2d.py From chainer with MIT License | 6 votes |
def check_pickling(self, x_data): x = chainer.Variable(x_data) y = self.link(x) y_data1 = y.data del x, y pickled = pickle.dumps(self.link, -1) del self.link self.link = pickle.loads(pickled) x = chainer.Variable(x_data) y = self.link(x) y_data2 = y.data testing.assert_allclose(y_data1, y_data2, atol=0, rtol=0)
Example #9
Source File: test_link_tree_lstm.py From chainer with MIT License | 6 votes |
def check_forward(self, *inputs_data): inputs_variable = [chainer.Variable(v) for v in inputs_data] c, h = self.link(*inputs_variable) self.assertEqual(c.data.dtype, self.dtype) self.assertEqual(h.data.dtype, self.dtype) # Compute expected out if self.model_type == 'ChildSumTreeLSTM': c_expect, h_expect = _child_sum_tree_lstm(self.link, *inputs_data) elif self.model_type == 'NaryTreeLSTM': c_expect, h_expect = _nary_tree_lstm(self.link, *inputs_data) else: NotImplementedError() testing.assert_allclose( c_expect, c.data, **self.check_forward_options) testing.assert_allclose( h_expect, h.data, **self.check_forward_options)
Example #10
Source File: test_thin_stack.py From chainer with MIT License | 6 votes |
def check_backward(self, s_data, i_data, x_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 = chainer.Variable(x_data) t = thin_stack.thin_stack_set(s, i, x) t.grad = gt_data t.backward() for j, ind in enumerate(i_data): testing.assert_allclose(x.grad[j], gt_old[j, ind]) for k in range(self.shape[1]): if k == ind: testing.assert_allclose(s.grad[j, k], 0) 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)
Example #11
Source File: test_thin_stack.py From chainer with MIT License | 6 votes |
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)
Example #12
Source File: test_mellowmax.py From chainerrl with MIT License | 6 votes |
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)
Example #13
Source File: test_function_node.py From chainer with MIT License | 6 votes |
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)
Example #14
Source File: test_link_lstm.py From chainer with MIT License | 6 votes |
def check_forward(self, x_data): xp = self.link.xp x = chainer.Variable(x_data) if self.input_variable else x_data c1, h1 = self.link(None, None, x) device = backend.get_device_from_array(x_data) with chainer.using_device(device): c0 = chainer.Variable(xp.zeros((len(self.x), self.out_size), dtype=self.x.dtype)) c1_expect, h1_expect = functions.lstm(c0, self.link.upward(x)) testing.assert_allclose(h1.data, h1_expect.data) testing.assert_allclose(c1.data, c1_expect.data) c2, h2 = self.link(c1, h1, x) c2_expect, h2_expect = \ functions.lstm(c1_expect, self.link.upward(x) + self.link.lateral(h1)) testing.assert_allclose(h2.data, h2_expect.data) testing.assert_allclose(c2.data, c2_expect.data)
Example #15
Source File: test_backprop.py From chainer with MIT License | 6 votes |
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
Example #16
Source File: test_zoneoutlstm.py From chainer with MIT License | 6 votes |
def check_forward(self, c_data, h_data, x_data): x = chainer.Variable(x_data) h1 = self.link(x) c1 = self.link.c c1_expect, h1_expect = _zoneoutlstm(self.link, c_data, h_data, x_data, c1.creator, h1.creator) testing.assert_allclose(h1.data, h1_expect) testing.assert_allclose(self.link.c.data, c1_expect) testing.assert_allclose(self.link.h.data, h1_expect) h2 = self.link(x) c2 = self.link.c c2_expect, h2_expect = _zoneoutlstm(self.link, c1_expect, h1_expect, x_data, c2.creator, h2.creator) testing.assert_allclose(h2.data, h2_expect) testing.assert_allclose(self.link.c.data, c2_expect) testing.assert_allclose(self.link.h.data, h2_expect)
Example #17
Source File: test_variable.py From chainer with MIT License | 6 votes |
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)
Example #18
Source File: test_optimizers.py From chainer with MIT License | 6 votes |
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)
Example #19
Source File: test_optimizer.py From chainer with MIT License | 6 votes |
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)
Example #20
Source File: test_convolution_2d.py From chainer with MIT License | 6 votes |
def test_pickling(self, backend_config): x_data, = self.generate_inputs() link = self.create_link(self.generate_params()) link.to_device(backend_config.device) x = chainer.Variable(x_data) x.to_device(backend_config.device) y = link(x) y_data1 = y.data del x, y pickled = pickle.dumps(link, -1) del link link = pickle.loads(pickled) x = chainer.Variable(x_data) x.to_device(backend_config.device) y = link(x) y_data2 = y.data testing.assert_allclose(y_data1, y_data2, atol=0, rtol=0)
Example #21
Source File: test_variable_statistics_plot.py From chainer with MIT License | 6 votes |
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))
Example #22
Source File: test_multistep_shift.py From chainer with MIT License | 6 votes |
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])
Example #23
Source File: test_convolution_2d.py From chainer with MIT License | 6 votes |
def test_pickling(self, backend_config): x_data, = self.generate_inputs() link = self.create_link(self.generate_params()) link.to_device(backend_config.device) x = chainer.Variable(x_data) x.to_device(backend_config.device) y = link(x) y_data1 = y.data del x, y pickled = pickle.dumps(link, -1) del link link = pickle.loads(pickled) x = chainer.Variable(x_data) x.to_device(backend_config.device) y = link(x) y_data2 = y.data testing.assert_allclose(y_data1, y_data2, atol=0, rtol=0)
Example #24
Source File: test_convolution_nd.py From chainer with MIT License | 6 votes |
def test_pickling(self, backend_config): x_data, = self.generate_inputs() link = self.create_link(self.generate_params()) link.to_device(backend_config.device) x = chainer.Variable(x_data) x.to_device(backend_config.device) y = link(x) y_data1 = y.data del x, y pickled = pickle.dumps(link, -1) del link link = pickle.loads(pickled) x = chainer.Variable(x_data) x.to_device(backend_config.device) y = link(x) y_data2 = y.data testing.assert_allclose(y_data1, y_data2, atol=0, rtol=0)
Example #25
Source File: test_dilated_convolution_2d.py From chainer with MIT License | 6 votes |
def check_pickling(self, x_data): x = chainer.Variable(x_data) y = self.link(x) y_data1 = y.data del x, y pickled = pickle.dumps(self.link, -1) del self.link self.link = pickle.loads(pickled) x = chainer.Variable(x_data) y = self.link(x) y_data2 = y.data testing.assert_allclose(y_data1, y_data2, atol=0, rtol=0)
Example #26
Source File: test_function_node.py From chainer with MIT License | 6 votes |
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)
Example #27
Source File: test_dilated_convolution_2d.py From chainer with MIT License | 5 votes |
def check_forward_consistency(self): x_cpu = chainer.Variable(self.x) y_cpu = self.link(x_cpu) self.assertEqual(y_cpu.data.dtype, numpy.float32) with testing.assert_warns(DeprecationWarning): self.link.to_gpu() x_gpu = chainer.Variable(cuda.to_gpu(self.x)) y_gpu = self.link(x_gpu) self.assertEqual(y_gpu.data.dtype, numpy.float32) testing.assert_allclose(y_cpu.data, y_gpu.data.get())
Example #28
Source File: test_link_gru.py From chainer with MIT License | 5 votes |
def check_forward(self, h_data, x_data): h = chainer.Variable(h_data) x = chainer.Variable(x_data) y = self._forward(self.link, h, x) self.assertEqual(y.data.dtype, numpy.float32) y_expect = _gru(self.link, h_data, x_data) testing.assert_allclose(y_expect, y.data) if isinstance(self.link, links.StatefulGRU): testing.assert_allclose(self.link.h.data, y.data)
Example #29
Source File: test_scale.py From chainer with MIT License | 5 votes |
def check_forward(self, x_data, W_data, y_expected): x = chainer.Variable(x_data) if W_data is None: y = self.link(x) testing.assert_allclose(y_expected, y.data) else: W = chainer.Variable(W_data) y = self.link(x, W) testing.assert_allclose(y_expected, y.data)
Example #30
Source File: test_deconvolution_nd.py From chainer with MIT License | 5 votes |
def test_deconv3d(self): in_c, out_c, x = self._get_data(3) link_nd = deconvolution_nd.DeconvolutionND( 3, in_c, out_c, 2, initialW=1) link_3d = deconvolution_nd.Deconvolution3D( in_c, out_c, 2, initialW=1) testing.assert_allclose(link_nd(x).data, link_3d(x).data)