本文整理汇总了Python中chainer.testing方法的典型用法代码示例。如果您正苦于以下问题:Python chainer.testing方法的具体用法?Python chainer.testing怎么用?Python chainer.testing使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer
的用法示例。
在下文中一共展示了chainer.testing方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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)
示例2: check_backward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [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)
示例3: check_serialize
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [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)
示例4: test_serialize_backward_compat
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [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)
示例5: check
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [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)
示例6: test_copy_and_to_gpu_uninit_multi_gpu
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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)
示例7: test_to_cpu_on_cpu
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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)
示例8: test_to_cpu
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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)
示例9: test_to_gpu
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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)
示例10: test_repeat_with_share
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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)
示例11: test_copyparams
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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)
示例12: test_intel64_to_intel64
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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
示例13: test_gpu_to_intel64
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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
示例14: test_intel64_to_gpu
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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
示例15: test_intel64_to_cpu
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import testing [as 别名]
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