def test_abs(self): np.random.seed(1234) x = nn.Parameter(np.random.randn(5, 7)) sign = np.sign(x.value) y = nn.abs(x) self.assertTrue((y.value == np.abs(x.value)).all()) for _ in range(10000): x.cleargrad() y = nn.abs(x) nn.square(y - 0.01).sum().backward() x.value -= x.grad * 0.001 self.assertTrue(np.allclose(x.value, 0.01 * sign))
def test_determinant(self): A = np.array([[2., 1.], [1., 3.]]) detA = np.linalg.det(A) self.assertTrue((detA == nn.linalg.det(A).value).all()) A = nn.Parameter(A) for _ in range(100): A.cleargrad() detA = nn.linalg.det(A) loss = nn.square(detA - 1) loss.backward() A.value -= 0.1 * A.grad self.assertAlmostEqual(detA.value, 1.)
def test_inverse(self): A = np.array([[2., 1.], [1., 3.]]) Ainv = np.linalg.inv(A) self.assertTrue((Ainv == nn.linalg.inv(A).value).all()) B = np.array([[-1., 1.], [1., 0.5]]) A = nn.Parameter(np.array([[-0.4, 0.7], [0.7, 0.7]])) for _ in range(100): A.cleargrad() Ainv = nn.linalg.inv(A) loss = nn.square(Ainv - B).sum() loss.backward() A.value -= 0.1 * A.grad self.assertTrue(np.allclose(A.value, np.linalg.inv(B)))
def test_logdet(self): A = np.array([ [2., 1.], [1., 3.] ]) logdetA = np.linalg.slogdet(A)[1] self.assertTrue((logdetA == nn.linalg.logdet(A).value).all()) A = nn.Parameter(A) for _ in range(100): A.cleargrad() logdetA = nn.linalg.logdet(A) loss = nn.square(logdetA - 1) loss.backward() A.value -= 0.1 * A.grad self.assertAlmostEqual(logdetA.value, 1)
def test_trace(self): arrays = [np.random.normal(size=(2, 2)), np.random.normal(size=(3, 4))] for arr in arrays: arr = nn.Parameter(arr) tr_arr = nn.linalg.trace(arr) self.assertEqual(tr_arr.value, np.trace(arr.value)) a = np.array([[1.5, 0], [-0.1, 1.1]]) a = nn.Parameter(a) for _ in range(100): a.cleargrad() loss = nn.square(nn.linalg.trace(a) - 2) loss.backward() a.value -= 0.1 * a.grad self.assertEqual(nn.linalg.trace(a).value, 2)
def test_cholesky(self): A = np.array([[2., -1], [-1., 5.]]) L = np.linalg.cholesky(A) Ap = nn.Parameter(A) L_test = nn.linalg.cholesky(Ap) self.assertTrue((L == L_test.value).all()) T = np.array([[1., 0.], [-1., 2.]]) for _ in range(1000): Ap.cleargrad() L_ = nn.linalg.cholesky(Ap) loss = nn.square(T - L_).sum() loss.backward() Ap.value -= 0.1 * Ap.grad self.assertTrue(np.allclose(Ap.value, T @ T.T))
def test_solve(self): A = np.array([[2., 1.], [1., 3.]]) B = np.array([1., 2.])[:, None] AinvB = np.linalg.solve(A, B) self.assertTrue((AinvB == nn.linalg.solve(A, B).value).all()) A = nn.Parameter(A) B = nn.Parameter(B) for _ in range(100): A.cleargrad() B.cleargrad() AinvB = nn.linalg.solve(A, B) loss = nn.square(AinvB - 1).sum() loss.backward() A.value -= A.grad B.value -= B.grad self.assertTrue(np.allclose(AinvB.value, 1))
def test_solve(self): A = np.array([ [2., 1.], [1., 3.] ]) B = np.array([1., 2.])[:, None] AinvB = np.linalg.solve(A, B) self.assertTrue((AinvB == nn.linalg.solve(A, B).value).all()) A = nn.Parameter(A) B = nn.Parameter(B) for _ in range(100): A.cleargrad() B.cleargrad() AinvB = nn.linalg.solve(A, B) loss = nn.square(AinvB - 1).sum() loss.backward() A.value -= A.grad B.value -= B.grad self.assertTrue(np.allclose(AinvB.value, 1))
def test_cholesky(self): A = np.array([ [2., -1], [-1., 5.] ]) L = np.linalg.cholesky(A) Ap = nn.Parameter(A) L_test = nn.linalg.cholesky(Ap) self.assertTrue((L == L_test.value).all()) T = np.array([ [1., 0.], [-1., 2.] ]) for _ in range(1000): Ap.cleargrad() L_ = nn.linalg.cholesky(Ap) loss = nn.square(T - L_).sum() loss.backward() Ap.value -= 0.1 * Ap.grad self.assertTrue(np.allclose(Ap.value, T @ T.T))
def test_inverse(self): A = np.array([ [2., 1.], [1., 3.] ]) Ainv = np.linalg.inv(A) self.assertTrue((Ainv == nn.linalg.inv(A).value).all()) B = np.array([ [-1., 1.], [1., 0.5] ]) A = nn.Parameter(np.array([ [-0.4, 0.7], [0.7, 0.7] ])) for _ in range(100): A.cleargrad() Ainv = nn.linalg.inv(A) loss = nn.square(Ainv - B).sum() loss.backward() A.value -= 0.1 * A.grad self.assertTrue(np.allclose(A.value, np.linalg.inv(B)))
def test_sqrt(self): x = nn.Parameter(2.) y = nn.square(x) self.assertEqual(y.value, 4) y.backward() self.assertEqual(x.grad, 4)