def test_forward(self): w = ad.array([[1, 2, 3], [4, 5, 6]]) zeros = ad.zeros_like(w).transpose() actual = zeros.forward() expect = np.zeros((3, 2)) self.assertEqual((3, 2), zeros.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual))
def test_forward(self): val = np.random.random((3, 5)) w = ad.array(val) y = w.transpose().sum() actual = y.forward() expect = np.sum(val) self.assertEqual((), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual)) y = w.transpose().sum(axis=-1) actual = y.forward() expect = np.sum(val, axis=0) self.assertEqual((5,), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual)) y = w.transpose().sum(axis=0) actual = y.forward() expect = np.sum(val, axis=-1) self.assertEqual((3,), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual)) y = w.transpose().sum(axis=(0, -1)) actual = y.forward() expect = np.sum(val) self.assertEqual((), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual))
def test_forward_keepdims(self): val = np.random.random((3, 5)) w = ad.array(val) y = ad.sum(w.transpose(), keepdims=True) actual = y.forward() expect = np.sum(val, keepdims=True) self.assertEqual((1, 1), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual)) y = w.transpose().sum(axis=-1, keepdims=True) actual = y.forward() expect = np.transpose(np.sum(val, axis=0, keepdims=True)) self.assertEqual((5, 1), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual)) y = w.transpose().sum(axis=0, keepdims=True) actual = y.forward() expect = np.transpose(np.sum(val, axis=-1, keepdims=True)) self.assertEqual((1, 3), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual)) y = w.transpose().sum(axis=(0, -1), keepdims=True) actual = y.forward() expect = np.sum(val, keepdims=True) self.assertEqual((1, 1), y.shape) self.assertTrue(np.allclose(expect, actual), (expect, actual))
def test_backward(self): w = ad.array([[1, 2, 3], [4, 5, 6]]) zeros = ad.zeros_like(w).transpose() self.numeric_gradient_check(zeros, {}, [])
def test_constants(self): x = ad.array(np.arange(12)).reshape((3, 4)).transpose().simplify() self.assertEqual('constant(shape=(4, 3))', x.name)