Python chainer.testing() Examples
The following are 30
code examples of chainer.testing().
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: test_link.py From chainer with MIT License | 6 votes |
def test_intel64_to_gpu(self): link = self.link with testing.assert_warns(DeprecationWarning): link.to_intel64() assert isinstance(link.device, backend.Intel64Device) with testing.assert_warns(DeprecationWarning): link.to_gpu() assert link.device.device == cuda.Device(0) # Arrays should be converted to cupy.ndarray # Initialized parameter assert isinstance(link.y.data, cuda.cupy.ndarray) _assert_variable_array_equal(link.y, self.y_array) # Uninitialized parameter assert link.v.data is None # Persistent ndarray assert isinstance(link.pa, cuda.ndarray) _assert_arrays_equal(link.pa, self.pa_array) # Persistent scalar assert link.ps == self.ps_scalar
Example #2
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 #3
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_to_cpu(self): self.set_count_parameters() with testing.assert_warns(DeprecationWarning): self.c2.to_gpu() with testing.assert_warns(DeprecationWarning): self.c2.to_cpu() self.assertIs(self.c2.xp, numpy) self.assertIs(self.c1.xp, numpy) self.assertIs(self.l1.xp, numpy) self.assertIs(self.l2.xp, numpy) self.assertIs(self.l3.xp, numpy) self.assertIsInstance(self.l1.x.data, numpy.ndarray) self.assertIsInstance(self.l1.x.grad, numpy.ndarray) self.assertIsInstance(self.l2.x.data, numpy.ndarray) self.assertIsInstance(self.l2.x.grad, numpy.ndarray) self.assertIsNone(self.l3.x.data) self.assertIsNone(self.l3.x.grad) self.l3.x.initialize(3) self.assertIsInstance(self.l3.x.data, numpy.ndarray) self.assertIsInstance(self.l3.x.grad, numpy.ndarray)
Example #4
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 #5
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_to_cpu_on_cpu(self): x1 = self.l1.x.data gx1 = self.l1.x.grad x2 = self.l2.x.data gx2 = self.l2.x.grad x3 = self.l3.x.data with testing.assert_warns(DeprecationWarning): self.c2.to_cpu() self.assertIs(self.l1.x.data, x1) self.assertIs(self.l1.x.grad, gx1) self.assertIs(self.l2.x.data, x2) self.assertIs(self.l2.x.grad, gx2) self.assertIs(self.l3.x.data, x3) with pytest.raises(RuntimeError): self.l3.x.grad
Example #6
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_to_cpu(self): with testing.assert_warns(DeprecationWarning): self.c2.to_gpu() with testing.assert_warns(DeprecationWarning): self.c2.to_cpu() self.assertIs(self.c2.xp, numpy) self.assertIs(self.c1.xp, numpy) self.assertIs(self.l1.xp, numpy) self.assertIs(self.l2.xp, numpy) self.assertIs(self.l3.xp, numpy) self.assertIsInstance(self.l1.x.data, numpy.ndarray) self.assertIsInstance(self.l1.x.grad, numpy.ndarray) self.assertIsInstance(self.l2.x.data, numpy.ndarray) self.assertIsInstance(self.l2.x.grad, numpy.ndarray) self.assertIsInstance(self.l3.x.data, numpy.ndarray) self.assertIsInstance(self.l3.x.grad, numpy.ndarray)
Example #7
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_repeat_with_share(self): ret = self.link.repeat(2, mode='share') self.assertEqual(len(ret), 2) # Both should be different objects from the original link self.assertIsNot(ret[0], self.link) self.assertIsNot(ret[1], self.link) # Object IDs of elements should be different self.assertIsNot(ret[0], ret[1]) self.assertIsNot(ret[0].x, ret[1].x) # But the array objects should be the same self.assertIs(ret[0].x.array, ret[1].x.array) # But shape, type, and value of paratmeres shuld be same self.assertEqual(ret[0].x.shape, self.link.x.shape) self.assertEqual(ret[0].x.dtype, self.link.x.dtype) self.assertEqual(ret[0].x.shape, ret[1].x.shape) self.assertEqual(ret[0].x.dtype, ret[1].x.dtype) numpy.testing.assert_array_equal(ret[0].x.array, ret[1].x.array)
Example #8
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_copyparams(self): l1 = chainer.Link() with l1.init_scope(): l1.x = chainer.Parameter(shape=(2, 3)) l1.y = chainer.Parameter() l2 = chainer.Link() with l2.init_scope(): l2.x = chainer.Parameter(shape=2) l3 = chainer.Link() with l3.init_scope(): l3.x = chainer.Parameter(shape=3) c1 = chainer.ChainList(l1, l2) c2 = chainer.ChainList(c1, l3) l1.x.data.fill(0) l2.x.data.fill(1) l3.x.data.fill(2) self.c2.copyparams(c2) numpy.testing.assert_array_equal(self.l1.x.data, l1.x.data) numpy.testing.assert_array_equal(self.l2.x.data, l2.x.data) numpy.testing.assert_array_equal(self.l3.x.data, l3.x.data)
Example #9
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_to_gpu(self): self.set_count_parameters() cupy = cuda.cupy with testing.assert_warns(DeprecationWarning): self.c2.to_gpu() self.assertIs(self.c2.xp, cupy) self.assertIs(self.c1.xp, cupy) self.assertIs(self.l1.xp, cupy) self.assertIs(self.l2.xp, cupy) self.assertIs(self.l3.xp, cupy) self.assertIsInstance(self.l1.x.data, cupy.ndarray) self.assertIsInstance(self.l1.x.grad, cupy.ndarray) self.assertIsInstance(self.l2.x.data, cupy.ndarray) self.assertIsInstance(self.l2.x.grad, cupy.ndarray) self.assertIsNone(self.l3.x.data) self.assertIsNone(self.l3.x.grad) self.l3.x.initialize(3) self.assertIsInstance(self.l3.x.data, cupy.ndarray) self.assertIsInstance(self.l3.x.grad, cupy.ndarray)
Example #10
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 #11
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_intel64_to_intel64(self): link = self.link with testing.assert_warns(DeprecationWarning): link.to_intel64() prev_y = link.y prev_v = link.v prev_pa = link.pa prev_ps = link.ps with testing.assert_warns(DeprecationWarning): link.to_intel64() assert isinstance(link.device, backend.Intel64Device) # Everything should be left untouched # Initialized parameter assert link.y is prev_y # Uninitialized parameter assert link.v is prev_v # Persistent ndarray assert link.pa is prev_pa # Persistent scalar assert link.ps is prev_ps
Example #12
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_gpu_to_intel64(self): link = self.link with testing.assert_warns(DeprecationWarning): link.to_gpu() assert link.device.device == cuda.Device(0) with testing.assert_warns(DeprecationWarning): link.to_intel64() assert isinstance(link.device, backend.Intel64Device) # Arrays should be converted to ideep.mdarray # Initialized parameter assert isinstance(link.y.data, intel64.ideep.mdarray) _assert_variable_array_equal(link.y, self.y_array) # Uninitialized parameter assert link.v.data is None # Persistent ndarray assert isinstance(link.pa, intel64.ideep.mdarray) _assert_arrays_equal(link.pa, self.pa_array) # Persistent scalar assert link.ps == self.ps_scalar
Example #13
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_intel64_to_cpu(self): link = self.link with testing.assert_warns(DeprecationWarning): link.to_intel64() assert isinstance(link.device, backend.Intel64Device) with testing.assert_warns(DeprecationWarning): link.to_cpu() assert isinstance(link.device, backend.CpuDevice) # Arrays should be converted to numpy.ndarray # Initialized parameter assert isinstance(link.y.data, numpy.ndarray) _assert_variable_array_equal(link.y, self.y_array) # Uninitialized parameter assert link.v.data is None # Persistent ndarray assert isinstance(link.pa, numpy.ndarray) _assert_arrays_equal(link.pa, self.pa_array) # Persistent scalar assert link.ps == self.ps_scalar
Example #14
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 #15
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_gpu_to_chx(self): link = self.link with testing.assert_warns(DeprecationWarning): link.to_gpu() assert link.device.device == cuda.Device(0) link.to_chx() assert link.device.device == chainerx.get_device('cuda:0') # Arrays should be converted to chainerx.ndarray # Initialized parameter assert isinstance(link.y.data, chainerx.ndarray) assert link.y.data.device.backend.name == 'cuda' assert link.y.data.device.index == 0 _assert_variable_array_equal(link.y, self.y_array) # Uninitialized parameter assert link.v.data is None # Persistent ndarray assert isinstance(link.pa, chainerx.ndarray) _assert_arrays_equal(link.pa, self.pa_array) # Persistent scalar assert link.ps == self.ps_scalar # TODO(niboshi): Add other test variations
Example #16
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 #17
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_copy_and_to_gpu_uninit_multi_gpu(self): cupy = cuda.cupy l0 = self.link l1 = l0.copy() l2 = l0.copy() self.assertIsNone(l0.u.data) self.assertIsNone(l1.u.data) self.assertIsNone(l2.u.data) with testing.assert_warns(DeprecationWarning): l1.to_gpu() l1.u.initialize((2, 3)) with testing.assert_warns(DeprecationWarning): l2.to_gpu() l2.u.initialize((2, 3)) self.assertIsNone(l0.u.data) self.assertIsInstance(l1.u.data, cupy.ndarray) self.assertIsInstance(l2.u.data, cupy.ndarray) self.assertNotEqual(l1.u.data.data, l2.u.data.data)
Example #18
Source File: test_distribution.py From chainerrl with MIT License | 6 votes |
def _test(self, gpu): if gpu >= 0: chainer.cuda.get_device_from_id(gpu).use() xp = chainer.cuda.cupy else: xp = np batch_probs = xp.asarray([[0.3, 0.7], [0.8, 0.2], [0.5, 0.5], [0.0, 1.0], [1.0, 0.0]], dtype=np.float32) counter = np.zeros(batch_probs.shape, dtype=batch_probs.dtype) for _ in range(1000): batch_indices = chainer.cuda.to_cpu( distribution.sample_discrete_actions(batch_probs)) for i in range(batch_probs.shape[0]): counter[i][batch_indices[i]] += 1 np.testing.assert_allclose( counter / 1000, chainer.cuda.to_cpu(batch_probs), atol=0.05)
Example #19
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_to_cpu_on_cpu(self): x = self.link.x.data gx = self.link.x.grad y = self.link.y.data gy = self.link.y.grad p = self.link.p with testing.assert_warns(DeprecationWarning): self.link.to_cpu() self.assertIs(self.link.x.data, x) self.assertIs(self.link.x.grad, gx) self.assertIs(self.link.y.data, y) self.assertIs(self.link.y.grad, gy) self.assertIsNone(self.link.u.data) u = self.link.u with pytest.raises(RuntimeError): u.grad self.assertIs(self.link.p, p)
Example #20
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_to_cpu(self): with testing.assert_warns(DeprecationWarning): self.link.to_gpu() with testing.assert_warns(DeprecationWarning): self.link.to_cpu() self.link.v.initialize((2, 3)) self.assertIs(self.link.xp, numpy) self.assertIsInstance(self.link.x.data, numpy.ndarray) self.assertIsInstance(self.link.x.grad, numpy.ndarray) self.assertIsInstance(self.link.y.data, numpy.ndarray) self.assertIsInstance(self.link.y.grad, numpy.ndarray) self.assertIsNone(self.link.u.data) u = self.link.u with pytest.raises(RuntimeError): u.grad self.assertIsInstance(self.link.v.data, numpy.ndarray) self.assertIsInstance(self.link.v.grad, numpy.ndarray) self.assertIsInstance(self.link.p, numpy.ndarray)
Example #21
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_to_gpu(self): cupy = cuda.cupy with testing.assert_warns(DeprecationWarning): self.link.to_gpu() self.link.v.initialize((2, 3)) self.assertIs(self.link.xp, cupy) self.assertIsInstance(self.link.x.data, cupy.ndarray) self.assertIsInstance(self.link.x.grad, cupy.ndarray) self.assertIsInstance(self.link.y.data, cupy.ndarray) self.assertIsInstance(self.link.y.grad, cupy.ndarray) self.assertIsNone(self.link.u.data) u = self.link.u with pytest.raises(RuntimeError): u.grad self.assertIsInstance(self.link.v.data, cupy.ndarray) self.assertIsInstance(self.link.v.grad, cupy.ndarray) self.assertIsInstance(self.link.p, cupy.ndarray)
Example #22
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_zerograds(self): gx_expect = numpy.zeros_like(self.link.x.data) gy_expect = numpy.zeros_like(self.link.y.data) with testing.assert_warns(DeprecationWarning): self.link.zerograds() numpy.testing.assert_array_equal(self.link.x.grad, gx_expect) numpy.testing.assert_array_equal(self.link.y.grad, gy_expect) self.link.u.initialize((2, 3)) self.link.v.initialize((2, 3)) gu_expect = numpy.zeros_like(self.link.u.data) gv_expect = numpy.zeros_like(self.link.v.data) numpy.testing.assert_array_equal(self.link.u.grad, gu_expect) numpy.testing.assert_array_equal(self.link.v.grad, gv_expect)
Example #23
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_to_gpu_override(self): with testing.assert_warns(DeprecationWarning): self.create_link('to_gpu')
Example #24
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_to_device_gpu(self): with testing.assert_warns(DeprecationWarning): cls = self.create_link('to_gpu') l = cls() l.to_device('@cupy:0') assert l.child.to_method_called == 1
Example #25
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_repeat_with_copy(self): ret = self.link.repeat(2, mode='copy') self.assertEqual(len(ret), 2) # Both should be different objects from the original link self.assertIsNot(ret[0], self.link) self.assertIsNot(ret[1], self.link) # Object IDs of elements should be different self.assertIsNot(ret[0], ret[1]) self.assertIsNot(ret[0].x, ret[1].x) # But shape, type, and value of paratmeres shuld be same self.assertEqual(ret[0].x.shape, self.link.x.shape) self.assertEqual(ret[0].x.dtype, self.link.x.dtype) self.assertEqual(ret[0].x.shape, ret[1].x.shape) self.assertEqual(ret[0].x.dtype, ret[1].x.dtype) numpy.testing.assert_array_equal(ret[0].x.array, ret[1].x.array)
Example #26
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_copy_and_send_to_gpu_2(self): c2 = self.c2.copy() with testing.assert_warns(DeprecationWarning): c2.to_gpu() self.assertIsInstance(self.c2[0][0].x.data, numpy.ndarray) self.assertIsInstance(self.c2[0][1].x.data, numpy.ndarray) self.assertIsInstance(c2[0][0].x.data, cuda.cupy.ndarray) self.assertIsInstance(c2[0][1].x.data, cuda.cupy.ndarray)
Example #27
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_copyparams_no_copy_persistent(self): orig_p = self.link.p.copy() l, gs = self._setup_test_copyparams() numpy.testing.assert_array_equal(False, orig_p == l.p) self.link.copyparams(l, copy_persistent=False) self._check_copyparams(l, gs) numpy.testing.assert_array_equal(self.link.p, orig_p)
Example #28
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_copyparams(self): l, gs = self._setup_test_copyparams() self.link.copyparams(l) self._check_copyparams(l, gs) numpy.testing.assert_array_equal(self.link.p, l.p)
Example #29
Source File: test_link.py From chainer with MIT License | 5 votes |
def _check_copyparams(self, l, gs): gx, gy, gu = gs numpy.testing.assert_array_equal(self.link.x.data, l.x.data) numpy.testing.assert_array_equal(self.link.x.grad, gx) numpy.testing.assert_array_equal(self.link.y.data, l.y.data) numpy.testing.assert_array_equal(self.link.y.grad, gy) numpy.testing.assert_array_equal(self.link.u.data, l.u.data) numpy.testing.assert_array_equal(self.link.u.grad, gu) numpy.testing.assert_array_equal(self.link.v.data, l.v.data) numpy.testing.assert_array_equal(self.link.v.grad, None)
Example #30
Source File: test_link.py From chainer with MIT License | 5 votes |
def test_to_gpu_current_device(self): cuda.Device(1).use() with testing.assert_warns(DeprecationWarning): self.link.to_gpu() self.assertEqual(self.link.device.device, cuda.Device(1))