class TestProductManifold(ManifoldTestCase): def setUp(self): self.m = m = 100 self.n = n = 50 self.euclidean = Euclidean(m, n) self.sphere = Sphere(n) self.manifold = Product([self.euclidean, self.sphere]) point = self.manifold.random_point() @pymanopt.function.autograd(self.manifold) def cost(*x): return np.sum([np.linalg.norm(a - b)**2 for a, b in zip(x, point)]) self.cost = cost def test_dim(self): np_testing.assert_equal(self.manifold.dim, self.m * self.n + self.n - 1) def test_typical_dist(self): np_testing.assert_equal(self.manifold.typical_dist, np.sqrt((self.m * self.n) + np.pi**2)) def test_dist(self): X = self.manifold.random_point() Y = self.manifold.random_point() np_testing.assert_equal( self.manifold.dist(X, Y), np.sqrt( self.euclidean.dist(X[0], Y[0])**2 + self.sphere.dist(X[1], Y[1])**2), ) def test_tangent_vector_multiplication(self): # Regression test for https://github.com/pymanopt/pymanopt/issues/49. manifold = Product((Euclidean(12), Grassmann(12, 3))) x = manifold.random_point() eta = manifold.random_tangent_vector(x) np.float64(1.0) * eta # def test_inner_product(self): # def test_projection(self): # def test_euclidean_to_riemannian_hessian(self): # def test_retraction(self): def test_first_order_function_approximation(self): self.run_gradient_approximation_test() def test_second_order_function_approximation(self): self.run_hessian_approximation_test() # def test_norm(self): # def test_random_point(self): # def test_random_tangent_vector(self): # def test_transport(self): def test_exp_log_inverse(self): s = self.manifold X = s.random_point() Y = s.random_point() Yexplog = s.exp(X, tangent_vector=s.log(X, Y)) np_testing.assert_almost_equal(s.dist(point_a=Y, point_b=Yexplog), 0) def test_log_exp_inverse(self): s = self.manifold X = s.random_point() U = s.random_tangent_vector(X) Ulogexp = s.log(X, s.exp(X, U)) np_testing.assert_array_almost_equal(U[0], Ulogexp[0]) np_testing.assert_array_almost_equal(U[1], Ulogexp[1]) def test_pair_mean(self): s = self.manifold X = s.random_point() Y = s.random_point() Z = s.pair_mean(X, Y) np_testing.assert_array_almost_equal(s.dist(X, Z), s.dist(Y, Z))
class TestProblemBackendInterface(TestCase): def setUp(self): self.m = m = 20 self.n = n = 10 self.rank = rank = 3 A = np.random.normal(size=(m, n)) self.manifold = Product([FixedRankEmbedded(m, n, rank), Euclidean(n)]) @pymanopt.function.autograd(self.manifold) def cost(u, s, vt, x): return np.linalg.norm(((u * s) @ vt - A) @ x) ** 2 self.cost = cost self.gradient = self.cost.get_gradient_operator() self.hessian = self.cost.get_hessian_operator() self.problem = pymanopt.Problem(self.manifold, self.cost) def test_cost_function(self): (u, s, vt), x = self.manifold.random_point() self.cost(u, s, vt, x) def test_gradient_operator_shapes(self): (u, s, vt), x = self.manifold.random_point() gu, gs, gvt, gx = self.gradient(u, s, vt, x) self.assertEqual(gu.shape, (self.m, self.rank)) self.assertEqual(gs.shape, (self.rank,)) self.assertEqual(gvt.shape, (self.rank, self.n)) self.assertEqual(gx.shape, (self.n,)) def test_hessian_operator_shapes(self): (u, s, vt), x = self.manifold.random_point() (a, b, c), d = self.manifold.random_point() hu, hs, hvt, hx = self.hessian(u, s, vt, x, a, b, c, d) self.assertEqual(hu.shape, (self.m, self.rank)) self.assertEqual(hs.shape, (self.rank,)) self.assertEqual(hvt.shape, (self.rank, self.n)) self.assertEqual(hx.shape, (self.n,)) def test_problem_cost(self): cost = self.problem.cost X = self.manifold.random_point() (u, s, vt), x = X np_testing.assert_allclose(cost(X), self.cost(u, s, vt, x)) def test_problem_gradient_operator(self): X = self.manifold.random_point() (u, s, vt), x = X G = self.problem.euclidean_gradient(X) (gu, gs, gvt), gx = G for ga, gb in zip((gu, gs, gvt, gx), self.gradient(u, s, vt, x)): np_testing.assert_allclose(ga, gb) def test_problem_hessian_operator(self): ehess = self.problem.euclidean_hessian X = self.manifold.random_point() U = self.manifold.random_point() H = ehess(X, U) (u, s, vt), x = X (a, b, c), d = U (hu, hs, hvt), hx = H for ha, hb in zip( (hu, hs, hvt, hx), self.hessian(u, s, vt, x, a, b, c, d) ): np_testing.assert_allclose(ha, hb)
def test_tangent_vector_multiplication(self): # Regression test for https://github.com/pymanopt/pymanopt/issues/49. manifold = Product((Euclidean(12), Grassmann(12, 3))) x = manifold.random_point() eta = manifold.random_tangent_vector(x) np.float64(1.0) * eta