Beispiel #1
0
    def _setup(self, device):
        y_shape = self.y_shape
        x_shape = self.x_shape
        y1 = make_array(1, y_shape, numpy.float32, device)
        y2 = make_array(2, y_shape, numpy.float32, device)
        gx1 = chainer.Variable(make_array(1, x_shape, numpy.float32, device))
        gx2 = None
        gy1 = make_array(1, y_shape, numpy.float32, device)
        gy2 = make_array(1, y_shape, numpy.float32, device)

        f = chainer.FunctionNode()
        f.check_type_forward = mock.MagicMock()
        f.forward_cpu = mock.MagicMock()
        f.forward_gpu = mock.MagicMock()
        f.backward = mock.MagicMock(return_value=(gx1, gx2))
        self.f = f

        self.x1 = make_array(0, x_shape, numpy.float32, device)
        self.x2 = make_array(0, x_shape, numpy.int32, device)
        self.y1 = y1
        self.y2 = y2
        self.gx1 = gx1
        self.gx2 = gx2
        self.gx1_orig = chainer.Variable(
            make_array(3, x_shape, numpy.float32, device))
        self.gx2_orig = chainer.Variable(
            make_array(2, x_shape, numpy.float32, device))
        self.gx1_accum = gx1 + self.gx1_orig
        self.gy1 = gy1
        self.gy2 = gy2
Beispiel #2
0
 def setUp(self):
     self.original_debug = chainer.is_debug()
     chainer.set_debug(True)
     self.one = numpy.array(1, numpy.float32)
     self.f = chainer.FunctionNode()
     self.return_value = tuple(None if x is None else chainer.Variable(x)
                               for x in self.return_data)
Beispiel #3
0
 def test_set_fresh_creator_node(self):
     v = chainer.Variable()
     f = chainer.FunctionNode()
     v.creator_node = f
     self.assertIs(v.creator, f)
     self.assertIs(v.creator_node, f)
     self.assertEqual(v.rank, 1)
Beispiel #4
0
    def setUp(self):
        y1 = numpy.arange(4).astype(numpy.float32)
        y2 = numpy.arange(4).astype(numpy.float32) + 1
        gx1 = chainer.Variable(numpy.arange(3).astype(numpy.float32))
        gx2 = None
        gy1 = numpy.arange(4).astype(numpy.float32)
        gy2 = numpy.arange(4).astype(numpy.float32)

        f = chainer.FunctionNode()
        f.check_type_forward = mock.MagicMock()
        f.forward_cpu = mock.MagicMock(return_value=(y1, y2))
        f.forward_gpu = mock.MagicMock()
        f.backward = mock.MagicMock(return_value=(gx1, gx2))
        self.f = f

        self.x1 = numpy.arange(3).astype(numpy.float32)
        self.x2 = numpy.arange(3).astype(numpy.int32)
        self.y1 = y1
        self.y2 = y2
        self.gx1 = gx1
        self.gx2 = gx2
        self.gx1_orig = chainer.Variable(
            numpy.arange(3, 6).astype(numpy.float32))
        self.gx2_orig = chainer.Variable(
            numpy.arange(2, 5).astype(numpy.float32))
        self.gx1_accum = gx1 + self.gx1_orig
        self.gy1 = gy1
        self.gy2 = gy2
Beispiel #5
0
    def check_zerograd(self, a_data, fill=False):
        xp = cuda.get_array_module(a_data)
        a = chainer.Variable(a_data)
        if fill:
            a.grad_var = chainer.Variable(xp.full_like(a_data, np.nan))
            a.grad_var.creator_node = chainer.FunctionNode()

        a.zerograd()
        self.assertIsNot(a.grad, None)
        if fill:
            self.assertIsNone(a.grad_var.creator_node)
        g_expect = xp.zeros_like(a.data)
        xp.testing.assert_array_equal(a.grad, g_expect)
Beispiel #6
0
    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
Beispiel #7
0
 def setUp(self):
     self.f = chainer.FunctionNode()
Beispiel #8
0
 def setUp(self):
     self.original_debug = chainer.is_debug()
     chainer.set_debug(True)
     self.one = numpy.array([1], numpy.float32)
     self.f = chainer.FunctionNode()