Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init():
pman = ProductManifold((Sphere(), 10), (Sphere(), (3, 2)))
assert pman.n_elements == (10 + 3 * 2)
assert pman.name == "(Sphere)x(Sphere)"
def test_inner_product():
pman = ProductManifold((Sphere(), 10), (Sphere(), (3, 2)), (Euclidean(), ()))
point = [
Sphere().random_uniform(5, 10),
Sphere().random_uniform(5, 3, 2),
Euclidean().random_normal(5),
]
tensor = pman.pack_point(*point)
tangent = torch.randn_like(tensor)
tangent = pman.proju(tensor, tangent)
inner = pman.inner(tensor, tangent)
assert inner.shape == (5,)
inner_kd = pman.inner(tensor, tangent, keepdim=True)
assert inner_kd.shape == (5, 1)
def product_case():
torch.manual_seed(42)
ex = [torch.randn(10), torch.randn(3) / 10, torch.randn(3, 2), torch.randn(())]
ev = [torch.randn(10), torch.randn(3) / 10, torch.randn(3, 2), torch.randn(())]
manifolds = [
geoopt.Sphere(),
geoopt.PoincareBall(),
geoopt.Stiefel(),
geoopt.Euclidean(),
]
x = [manifolds[i].projx(ex[i]) for i in range(len(manifolds))]
v = [manifolds[i].proju(x[i], ev[i]) for i in range(len(manifolds))]
product_manifold = geoopt.ProductManifold(
*((manifolds[i], ex[i].shape) for i in range(len(ex)))
)
yield UnaryCase(
manifold_shapes[geoopt.ProductManifold],
product_manifold.pack_point(*x),
product_manifold.pack_point(*ex),
product_manifold.pack_point(*v),
product_manifold.pack_point(*ev),
product_manifold,
)
# + 1 case without stiefel
torch.manual_seed(42)
ex = [torch.randn(10), torch.randn(3) / 10, torch.randn(())]
ev = [torch.randn(10), torch.randn(3) / 10, torch.randn(())]
manifolds = [
def test_from_point_checks_shapes():
point = [
Sphere().random_uniform(5, 10),
Sphere().random_uniform(3, 3, 2),
Euclidean().random_normal(5),
]
pman = ProductManifold.from_point(*point)
assert pman.n_elements == (5 * 10 + 3 * 3 * 2 + 5 * 1)
with pytest.raises(ValueError) as e:
_ = ProductManifold.from_point(*point, batch_dims=1)
assert e.match("Not all parts have same batch shape")
ev = [torch.randn(10), torch.randn(3) / 10, torch.randn(3, 2), torch.randn(())]
manifolds = [
geoopt.Sphere(),
geoopt.PoincareBall(),
geoopt.Stiefel(),
geoopt.Euclidean(),
]
x = [manifolds[i].projx(ex[i]) for i in range(len(manifolds))]
v = [manifolds[i].proju(x[i], ev[i]) for i in range(len(manifolds))]
product_manifold = geoopt.ProductManifold(
*((manifolds[i], ex[i].shape) for i in range(len(ex)))
)
yield UnaryCase(
manifold_shapes[geoopt.ProductManifold],
product_manifold.pack_point(*x),
product_manifold.pack_point(*ex),
product_manifold.pack_point(*v),
product_manifold.pack_point(*ev),
product_manifold,
)
# + 1 case without stiefel
torch.manual_seed(42)
ex = [torch.randn(10), torch.randn(3) / 10, torch.randn(())]
ev = [torch.randn(10), torch.randn(3) / 10, torch.randn(())]
manifolds = [
geoopt.Sphere(),
geoopt.PoincareBall(),
# geoopt.Stiefel(),
geoopt.Euclidean(),
]
def test_unpack_tensor_method():
pman = ProductManifold((Sphere(), 10), (Sphere(), (3, 2)), (Euclidean(), ()))
point = pman.random(4, pman.n_elements)
parts = point.unpack_tensor()
assert parts[0].shape == (4, 10)
assert parts[1].shape == (4, 3, 2)
assert parts[2].shape == (4,)
def test_product():
manifold = geoopt.ProductManifold(
(geoopt.Sphere(), 10),
(geoopt.PoincareBall(), 3),
(geoopt.Stiefel(), (20, 2)),
(geoopt.Euclidean(), 43),
)
sample = manifold.random(20, manifold.n_elements)
manifold.assert_check_point_on_manifold(sample)
def test_reshaping():
pman = ProductManifold((Sphere(), 10), (Sphere(), (3, 2)), (Euclidean(), ()))
point = [
Sphere().random_uniform(5, 10),
Sphere().random_uniform(5, 3, 2),
Euclidean().random_normal(5),
]
tensor = pman.pack_point(*point)
assert tensor.shape == (5, 10 + 3 * 2 + 1)
point_new = pman.unpack_tensor(tensor)
for old, new in zip(point, point_new):
np.testing.assert_allclose(old, new)
def test_dtype_checked_properly():
p1 = PoincareBall()
p2 = PoincareBall().double()
with pytest.raises(ValueError) as e:
_ = ProductManifold((p1, (10,)), (p2, (12,)))
assert e.match("Not all manifold share the same dtype")