def test_forward_backward(self): x = Parameter(2.) y = exp(x) self.assertEqual(y.value, np.exp(2)) y.backward() self.assertEqual(x.grad, np.exp(2)) x = np.random.rand(5, 3) p = Parameter(x) y = exp(p) self.assertTrue((y.value == np.exp(x)).all()) y.backward(np.ones((5, 3))) self.assertTrue((p.grad == np.exp(x)).all())
def test_forward_backward(self): x = Parameter(2.) y = 2**x self.assertEqual(y.value, 4) y.backward() self.assertEqual(x.grad, 4 * np.log(2)) x = np.random.rand(10, 2) xp = Parameter(x) y = xp**3 self.assertTrue((y.value == x**3).all()) y.backward(np.ones((10, 2))) self.assertTrue((xp.grad == 3 * x**2).all())
def test_forward_backward(self): x = Parameter(2) z = x + 5 self.assertEqual(z.value, 7) z.backward() self.assertEqual(x.grad, 1) x = np.random.rand(5, 4) y = np.random.rand(4) p = Parameter(y) z = x + p self.assertTrue((z.value == x + y).all()) z.backward(np.ones((5, 4))) self.assertTrue((p.grad == np.ones(4) * 5).all())
def test_forward_backward(self): x = Parameter(2) y = x * 5 self.assertEqual(y.value, 10) y.backward() self.assertEqual(x.grad, 5) x = np.random.rand(5, 4) y = np.random.rand(4) yp = Parameter(y) z = x * yp self.assertTrue((z.value == x * y).all()) z.backward(np.ones((5, 4))) self.assertTrue((yp.grad == x.sum(axis=0)).all())
def test_forward_backward(self): x = np.random.rand(10, 3) y = np.random.rand(3, 5) g = np.random.rand(10, 5) xp = Parameter(x) z = xp @ y self.assertTrue((z.value == x @ y).all()) z.backward(g) self.assertTrue((xp.grad == g @ y.T).all()) yp = Parameter(y) z = x @ yp self.assertTrue((z.value == x @ y).all()) z.backward(g) self.assertTrue((yp.grad == x.T @ g).all())
def test_backward(self): a = Parameter(1) a.backward(delta=2) self.assertEqual(a.grad, 2) a = Parameter(np.ones((3, 4))) a.backward(delta=np.ones((3, 4))) self.assertTrue((a.grad == np.ones((3, 4))).all())
def test_forward_backward(self): x = Parameter(np.ones((1, 1))) shape = (5, 2, 3) y = broadcast_to(x, shape) self.assertEqual(y.shape, shape) y.backward(np.ones(shape)) self.assertTrue((x.grad == np.ones((1, 1)) * 30).all())
def test_forward_backward(self): x = Parameter(10.) z = x / 2 self.assertEqual(z.value, 5) z.backward() self.assertEqual(x.grad, 0.5) x = np.random.rand(5, 10, 3) y = np.random.rand(10, 1) p = Parameter(y) z = x / p self.assertTrue((z.value == x / y).all()) z.backward(np.ones((5, 10, 3))) self.assertTrue((p.grad == np.sum(-x / y**2, axis=0).sum(axis=1, keepdims=True)).all())
def test_forward_backward(self): x = np.random.rand(10, 3) y = np.random.rand(3) yp = Parameter(y) z = mean_squared_error(x, yp) self.assertEqual(z.value, 0.5 * np.square(x - y).mean()) z.backward() self.assertTrue(np.allclose(yp.grad, (y - x).sum(axis=0) / 30))
def test_forward_backward(self): self.assertRaises(ValueError, reshape, 1, (2, 3)) x = np.random.rand(2, 6) p = Parameter(x) y = reshape(p, (3, 4)) self.assertTrue((x.reshape(3, 4) == y.value).all()) y.backward(np.ones((3, 4))) self.assertTrue((p.grad == np.ones((2, 6))).all())
def test_forward_backward(self): self.assertRaises(TypeError, flatten, "abc") self.assertRaises(ValueError, flatten, np.ones(1)) x = np.random.rand(5, 4) p = Parameter(x) y = flatten(p) self.assertTrue((y.value == x.flatten()).all()) y.backward(np.ones(20)) self.assertTrue((p.grad == np.ones((5, 4))).all())
def test_forward_backward(self): x = np.random.rand(10, 7) a = Parameter(x) b, c = split(a, (3, ), axis=-1) self.assertTrue((b.value == x[:, :3]).all()) self.assertTrue((c.value == x[:, 3:]).all()) b.backward(np.ones((10, 3))) self.assertIs(a.grad, None) c.backward(np.ones((10, 4))) self.assertTrue((a.grad == np.ones((10, 7))).all())
def test_forward_backward(self): x = np.random.rand(5, 1, 2) xp = Parameter(x) z = xp.mean() self.assertEqual(z.value, x.mean()) z.backward() self.assertTrue((xp.grad == np.ones((5, 1, 2)) / 10).all()) xp.cleargrad() z = xp.mean(axis=0, keepdims=True) self.assertEqual(z.shape, (1, 1, 2)) self.assertTrue((z.value == x.mean(axis=0, keepdims=True)).all()) z.backward(np.ones((1, 1, 2))) self.assertTrue((xp.grad == np.ones((5, 1, 2)) / 5).all())
def test_cleargrad(self): a = Parameter(1) a.backward(delta=2) self.assertEqual(a.grad, 2) a.cleargrad() self.assertIs(a.grad, None)
def test_forward_backward(self): x = Parameter(2.) y = square(x) self.assertEqual(y.value, 4) y.backward() self.assertEqual(x.grad, 4)
def test_forward_backward(self): x = Parameter(2.) y = sqrt(x) self.assertEqual(y.value, np.sqrt(2)) y.backward() self.assertEqual(x.grad, 0.5 / np.sqrt(2))
def test_init(self): a = Parameter(1) self.assertIs(a.grad, None)