def test_backward(self): x0 = Variable(np.array(1.0)) x1 = Variable(np.array(1.0)) t = add(x0, x1) y = add(x0, t) y.backward() self.assertEqual(x0.grad.data, np.array(2.0)) self.assertEqual(x1.grad.data, np.array(1.0))
def test_retain_grad_only_the_firts_inputs(self): x0 = Variable(np.array(2.0)) x1 = Variable(np.array(3.0)) t = add(x0, x1) y = add(x0, t) y.backward() self.assertIsNone(y.grad) self.assertIsNone(t.grad) self.assertEqual(x0.grad.data, 2.0) self.assertEqual(x1.grad.data, 1.0)
def test_backward_sets_grad_on_inputs(self): x0 = Variable(np.array(2.0)) x1 = Variable(np.array(3.0)) y = add(x0, x1) y.backward() self.assertEqual(x0.grad.data, np.array(1.0)) self.assertEqual(x1.grad.data, np.array(1.0))
def test_generation(self): x = Variable(np.array(2.0)) a = F.square(x) y = add(F.square(a), F.square(a)) y.backward() self.assertEqual(y.data, np.array(32.0)) self.assertEqual(x.grad.data, np.array(64.0))
def test_forward(self): x = Variable(np.array(2.0)) y = Variable(np.array(3.0)) z = add(F.square(x), F.square(y)) z.backward() self.assertEqual(z.data, np.array(13.0)) self.assertTrue(isinstance(x.grad, Variable)) a = Variable(np.array(2.0)) b = add(a, a) b.backward() a.cleargrad() b = add(add(a, a), a) b.backward()
def test_add_backward(self, culc_object): x0 = culc_object[0] x1 = culc_object[1] y = add(x0, x1) y.backward() assert x0.grad.data == 1 and x1.grad.data == 1
def test_add(self, culc_object): assert add(culc_object[0], culc_object[1]).data == 5
def test_two_branch_diff(self): x = Variable(np.array(2.0)) a = square(x) y = add(square(a), square(a)) y.backward() self.assertEqual(x.grad.data, np.array(64.0))
def test_forward(self): x0 = Variable(np.array(2.0)) x1 = Variable(np.array(3.0)) y = add(x0, x1) self.assertEqual(y.data, np.array(5.0))