Manifolds¶

All manifolds share same API. Some manifols may have several implementations of retraction operation, every implementation has a corresponding class.

class geoopt.manifolds.Euclidean(ndim=0)[source]

Simple Euclidean manifold, every coordinate is treated as an independent element.

Parameters: ndim (int) – number of trailing dimensions treated as manifold dimensions. All the operations acting on cuch as inner products, etc will respect the ndim.
dist(x, y, *, keepdim=False)[source]

Compute distance between 2 points on the manifold that is the shortest path along geodesics.

Parameters: x (tensor) – point on the manifold y (tensor) – point on the manifold keepdim (bool) – keep the last dim? distance between two points scalar
egrad2rgrad(x, u)[source]

Transform gradient computed using autodiff to the correct Riemannian gradient for the point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – gradient to be projected grad vector in the Riemannian manifold tensor
expmap(x, u)[source]

Perform an exponential map $$\operatorname{Exp}_x(u)$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should reimplement this method in your own modules. Both single-line and multi-line strings are acceptable.

inner(x, u, v=None, *, keepdim=False)[source]

Inner product for tangent vectors at point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor (optional)) – tangent vector at point $$x$$ keepdim (bool) – keep the last dim? inner product (broadcasted) scalar
logmap(x, y)[source]

Perform an logarithmic map $$\operatorname{Log}_{x}(y)$$.

Parameters: x (tensor) – point on the manifold y (tensor) – point on the manifold tangent vector tensor
norm(x, u, *, keepdim=False)[source]

Norm of a tangent vector at point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ keepdim (bool) – keep the last dim? inner product (broadcasted) scalar
proju(x, u)[source]

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
projx(x)[source]

Project point $$x$$ on the manifold.

Parameters: x (tensor) – point to be projected projected point tensor
random_normal(*size, mean=0.0, std=1.0, device=None, dtype=None)[source]

Create a point on the manifold, measure is induced by Normal distribution.

Parameters: size (shape) – the desired shape mean (float|tensor) – mean value for the Normal distribution std (float|tensor) – std value for the Normal distribution device (torch.device) – the desired device dtype (torch.dtype) – the desired dtype random point on the manifold ManifoldTensor
retr(x, u)[source]

Perform a retraction from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
transp(x, y, v)[source]

Perform vector transport $$\mathfrac{T}_{x\to y}(v)$$.

Parameters: x (tensor) – start point on the manifold y (tensor) – target point on the manifold v (tensor) – tangent vector at point $$x$$ transported tensor(s) tensor or tuple of tensors
class geoopt.manifolds.Stiefel(**kwargs)[source]

Manifold induced by the following matrix constraint:

$\begin{split}X^\top X = I\\ X \in \mathrm{R}^{n\times m}\\ n \ge m\end{split}$
Parameters: canonical (bool) – Use canonical inner product instead of euclidean one (defaults to canonical)
projx(x)[source]

Project point $$x$$ on the manifold.

Parameters: x (tensor) – point to be projected projected point tensor
random_naive(*size, dtype=None, device=None)[source]

Naive approach to get random matrix on Stiefel manifold.

A helper function to sample a random point on the Stiefel manifold. The measure is non-uniform for this method, but fast to compute.

Parameters: size (shape) – the desired output shape dtype (torch.dtype) – desired dtype device (torch.device) – desired device random point on Stiefel manifold ManifoldTensor
class geoopt.manifolds.CanonicalStiefel(**kwargs)[source]

Stiefel Manifold with Canonical inner product

Manifold induced by the following matrix constraint:

$\begin{split}X^\top X = I\\ X \in \mathrm{R}^{n\times m}\\ n \ge m\end{split}$
egrad2rgrad(x, u)

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
expmap(x, u)

Perform a retraction from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
expmap_transp(x, u, v)

Calculate an optimized retr_transp for Stiefel Manifold.

inner(x, u, v=None, *, keepdim=False)[source]

Inner product for tangent vectors at point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor (optional)) – tangent vector at point $$x$$ keepdim (bool) – keep the last dim? inner product (broadcasted) scalar
proju(x, u)[source]

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
retr(x, u)[source]

Perform a retraction from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
retr_transp(x, u, v)[source]

Calculate an optimized retr_transp for Stiefel Manifold.

transp_follow_expmap(x, u, v)

Perform vector transport following $$u$$: $$\mathfrac{T}_{x\to\operatorname{retr}(x, u)}(v)$$.

This operation is sometimes is much more simpler and can be optimized.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported tensor tensor
transp_follow_retr(x, u, v)[source]

Perform vector transport following $$u$$: $$\mathfrac{T}_{x\to\operatorname{retr}(x, u)}(v)$$.

This operation is sometimes is much more simpler and can be optimized.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported tensor tensor
class geoopt.manifolds.EuclideanStiefel(**kwargs)[source]

Stiefel Manifold with Euclidean inner product

Manifold induced by the following matrix constraint:

$\begin{split}X^\top X = I\\ X \in \mathrm{R}^{n\times m}\\ n \ge m\end{split}$
egrad2rgrad(x, u)

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
expmap(x, u)[source]

Perform an exponential map $$\operatorname{Exp}_x(u)$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
inner(x, u, v=None, *, keepdim=False)[source]

Inner product for tangent vectors at point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor (optional)) – tangent vector at point $$x$$ keepdim (bool) – keep the last dim? inner product (broadcasted) scalar
proju(x, u)[source]

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
retr(x, u)[source]

Perform a retraction from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
transp(x, y, v)[source]

Perform vector transport $$\mathfrac{T}_{x\to y}(v)$$.

Parameters: x (tensor) – start point on the manifold y (tensor) – target point on the manifold v (tensor) – tangent vector at point $$x$$ transported tensor(s) tensor or tuple of tensors
class geoopt.manifolds.EuclideanStiefelExact(**kwargs)[source]

Stiefel Manifold with Euclidean inner product

Manifold induced by the following matrix constraint:

$\begin{split}X^\top X = I\\ X \in \mathrm{R}^{n\times m}\\ n \ge m\end{split}$

Notes

The implementation of retraction is an exact exponential map, this retraction will be used in optimization

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should reimplement this method in your own modules. Both single-line and multi-line strings are acceptable.

retr(x, u)

Perform an exponential map $$\operatorname{Exp}_x(u)$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
retr_transp(x, u, v)

Perform an exponential map and vector transport from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported point tensor
transp_follow_retr(x, u, v)

Perform vector transport following $$u$$: $$\mathfrac{T}_{x\to\operatorname{Exp}(x, u)}(v)$$.

Here, $$\operatorname{Exp}$$ is the best possible approximation of the true exponential map. There are cases when the exact variant is hard or impossible implement, therefore a fallback, non-exact, implementation is used.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported tensor tensor
class geoopt.manifolds.Sphere(intersection=None, complement=None)[source]

Sphere manifold induced by the following constraint

$\begin{split}\|x\|=1\\ x \in \mathbb{span}(U)\end{split}$

where $$U$$ can be parametrized with compliment space or intersection.

Parameters: intersection (tensor) – shape (..., dim, K), subspace to intersect with complement (tensor) – shape (..., dim, K), subspace to compliment
dist(x, y, *, keepdim=False)[source]

Compute distance between 2 points on the manifold that is the shortest path along geodesics.

Parameters: x (tensor) – point on the manifold y (tensor) – point on the manifold keepdim (bool) – keep the last dim? distance between two points scalar
egrad2rgrad(x, u)

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
expmap(x, u)[source]

Perform an exponential map $$\operatorname{Exp}_x(u)$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
inner(x, u, v=None, *, keepdim=False)[source]

Inner product for tangent vectors at point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor (optional)) – tangent vector at point $$x$$ keepdim (bool) – keep the last dim? inner product (broadcasted) scalar
logmap(x, y)[source]

Perform an logarithmic map $$\operatorname{Log}_{x}(y)$$.

Parameters: x (tensor) – point on the manifold y (tensor) – point on the manifold tangent vector tensor
proju(x, u)[source]

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
projx(x)[source]

Project point $$x$$ on the manifold.

Parameters: x (tensor) – point to be projected projected point tensor
random_uniform(*size, dtype=None, device=None)[source]

Uniform random measure on Sphere manifold.

Parameters: size (shape) – the desired output shape dtype (torch.dtype) – desired dtype device (torch.device) – desired device random point on Sphere manifold ManifoldTensor

Notes

In case of projector on the manifold, dtype and device are set automatically and shouldn’t be provided. If you provide them, they are checked to match the projector device and dtype

retr(x, u)[source]

Perform a retraction from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
transp(x, y, v)[source]

Perform vector transport $$\mathfrac{T}_{x\to y}(v)$$.

Parameters: x (tensor) – start point on the manifold y (tensor) – target point on the manifold v (tensor) – tangent vector at point $$x$$ transported tensor(s) tensor or tuple of tensors
class geoopt.manifolds.SphereExact(intersection=None, complement=None)[source]

Sphere manifold induced by the following constraint

$\begin{split}\|x\|=1\\ x \in \mathbb{span}(U)\end{split}$

where $$U$$ can be parametrized with compliment space or intersection.

Parameters: intersection (tensor) – shape (..., dim, K), subspace to intersect with complement (tensor) – shape (..., dim, K), subspace to compliment

Notes

The implementation of retraction is an exact exponential map, this retraction will be used in optimization

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should reimplement this method in your own modules. Both single-line and multi-line strings are acceptable.

retr(x, u)

Perform an exponential map $$\operatorname{Exp}_x(u)$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
retr_transp(x, u, v)

Perform an exponential map and vector transport from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported point tensor
transp_follow_retr(x, u, v)

Perform vector transport following $$u$$: $$\mathfrac{T}_{x\to\operatorname{Exp}(x, u)}(v)$$.

Here, $$\operatorname{Exp}$$ is the best possible approximation of the true exponential map. There are cases when the exact variant is hard or impossible implement, therefore a fallback, non-exact, implementation is used.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported tensor tensor
class geoopt.manifolds.PoincareBall(c=1.0)[source]

Poincare ball model, see more in Poincare Ball model.

Parameters: c (float|tensor) – ball negative curvature

Notes

It is extremely recommended to work with this manifold in double precision

dist(x, y, *, keepdim=False, dim=-1)[source]

Compute distance between 2 points on the manifold that is the shortest path along geodesics.

Parameters: x (tensor) – point on the manifold y (tensor) – point on the manifold keepdim (bool) – keep the last dim? distance between two points scalar
egrad2rgrad(x, u, *, dim=-1)[source]

Transform gradient computed using autodiff to the correct Riemannian gradient for the point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – gradient to be projected grad vector in the Riemannian manifold tensor
expmap(x, u, *, project=True, dim=-1)[source]

Perform an exponential map $$\operatorname{Exp}_x(u)$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
expmap_transp(x, u, v, dim=-1, project=True)[source]

Perform an exponential map and vector transport from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported point tensor
inner(x, u, v=None, *, keepdim=False, dim=-1)[source]

Inner product for tangent vectors at point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor (optional)) – tangent vector at point $$x$$ keepdim (bool) – keep the last dim? inner product (broadcasted) scalar
logmap(x, y, *, dim=-1)[source]

Perform an logarithmic map $$\operatorname{Log}_{x}(y)$$.

Parameters: x (tensor) – point on the manifold y (tensor) – point on the manifold tangent vector tensor
norm(x, u, *, keepdim=False, dim=-1)[source]

Norm of a tangent vector at point $$x$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ keepdim (bool) – keep the last dim? inner product (broadcasted) scalar
proju(x, u)[source]

Project vector $$u$$ on a tangent space for $$x$$, usually is the same as egrad2rgrad().

Parameters: x (tensor) – point on the manifold u (tensor) – vector to be projected projected vector tensor
projx(x, dim=-1)[source]

Project point $$x$$ on the manifold.

Parameters: x (tensor) – point to be projected projected point tensor
random_normal(*size, mean=0, std=1)[source]

Create a point on the manifold, measure is induced by Normal distribution on the tangent space of zero.

Parameters: size (shape) – the desired shape mean (float|tensor) – mean value for the Normal distribution std (float|tensor) – std value for the Normal distribution random point on the PoincareBall manifold ManifoldTensor

Notes

The device and dtype will match the device and dtype of the Manifold

retr(x, u, *, dim=-1)[source]

Perform a retraction from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
retr_transp(x, u, v, dim=-1)[source]

Perform a retraction + vector transport at once.

Parameters: x (tensor) – point on the manifold v (tensor) – tangent vector at point $$x$$ to be transported u (tensor) – tangent vector at point $$x$$ (required keyword only argument) order (int) – order of retraction approximation, by default uses the simplest. Possible choices depend on a concrete manifold and -1 stays for exponential map transported point and vectors tuple of tensors

Notes

Sometimes this is a far more optimal way to preform retraction + vector transport

transp(x, y, v, dim=-1)[source]

Perform vector transport $$\mathfrac{T}_{x\to y}(v)$$.

Parameters: x (tensor) – start point on the manifold y (tensor) – target point on the manifold v (tensor) – tangent vector at point $$x$$ transported tensor(s) tensor or tuple of tensors
transp_follow_expmap(x, u, v, dim=-1, project=True)[source]

Perform vector transport following $$u$$: $$\mathfrac{T}_{x\to\operatorname{Exp}(x, u)}(v)$$.

Here, $$\operatorname{Exp}$$ is the best possible approximation of the true exponential map. There are cases when the exact variant is hard or impossible implement, therefore a fallback, non-exact, implementation is used.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported tensor tensor
transp_follow_retr(x, u, v, dim=-1)[source]

Perform vector transport following $$u$$: $$\mathfrac{T}_{x\to\operatorname{retr}(x, u)}(v)$$.

This operation is sometimes is much more simpler and can be optimized.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported tensor tensor
class geoopt.manifolds.PoincareBallExact(c=1.0)[source]

Poincare ball model, see more in Poincare Ball model.

Parameters: c (float|tensor) – ball negative curvature

Notes

It is extremely recommended to work with this manifold in double precision

The implementation of retraction is an exact exponential map, this retraction will be used in optimization.

extra_repr()[source]

Set the extra representation of the module

To print customized extra information, you should reimplement this method in your own modules. Both single-line and multi-line strings are acceptable.

retr(x, u, *, project=True, dim=-1)

Perform an exponential map $$\operatorname{Exp}_x(u)$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ transported point tensor
retr_transp(x, u, v, dim=-1, project=True)

Perform an exponential map and vector transport from point $$x$$ with given direction $$u$$.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported point tensor
transp_follow_retr(x, u, v, dim=-1, project=True)

Perform vector transport following $$u$$: $$\mathfrac{T}_{x\to\operatorname{Exp}(x, u)}(v)$$.

Here, $$\operatorname{Exp}$$ is the best possible approximation of the true exponential map. There are cases when the exact variant is hard or impossible implement, therefore a fallback, non-exact, implementation is used.

Parameters: x (tensor) – point on the manifold u (tensor) – tangent vector at point $$x$$ v (tensor) – tangent vector at point $$x$$ to be transported transported tensor tensor