Skip to main content

GeoTorch

Build Docs Code coverage Codestyle Black License

A library for constrained optimization and manifold optimization for deep learning in PyTorch

Overview

GeoTorch provides a simple way to perform constrained optimization and optimization on manifolds in PyTorch. It is compatible out of the box with any optimizer, layer, and model implemented in PyTorch without any boilerplate in the training code. Just state the constraints when you construct the model and you are ready to go!

import torch
import torch.nn as nn
import geotorch

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        # One line suffices: Instantiate a linear layer with orthonormal columns
        self.linear = nn.Linear(64, 128)
        geotorch.orthogonal(self.linear, "weight")

        # Works with tensors: Instantiate a CNN with kernels of rank 1
        self.cnn = nn.Conv2d(16, 32, 3)
        geotorch.low_rank(self.cnn, "weight", rank=1)

        # Weights are initialized to a random value when you put the constraints, but
        # you may re-initialize them to a different value by assigning to them
        self.linear.weight = torch.eye(128, 64)
        # And that's all you need to do. The rest is regular PyTorch code

    def forward(self, x):
        # self.linear is orthogonal and every 3x3 kernel in self.cnn is of rank 1

# Use the model as you would normally do. Everything just works
model = Model().cuda()

# Use your optimizer of choice. Any optimizer works out of the box with any parametrization
optim = torch.optim.Adam(model.parameters(), lr=lr)

Constraints

The following constraints are implemented and may be used as in the example above:

Each of these constraints have some extra parameters which can be used to tailor the behavior of each constraint to the problem in hand. For more on this, see the documentation.

These constraints are a fronted for the families of spaces listed below.

Supported Spaces

Each constraint in GeoTorch is implemented as a manifold. These give the user more flexibility on the options that they choose for each parametrization. All these support Riemannian Gradient Descent (more on this here), but they also support optimization via any other PyTorch optimizer.

GeoTorch currently supports the following spaces:

  • Rn(n): Rⁿ. Unrestricted optimization

  • Sym(n): Vector space of symmetric matrices

  • Skew(n): Vector space of skew-symmetric matrices

  • Sphere(n): Sphere in Rⁿ. { x ∈ Rⁿ | ||x|| = 1 } ⊂ Rⁿ

  • SO(n): Manifold of n×n orthogonal matrices

  • St(n,k): Manifold of n×k matrices with orthonormal columns

  • AlmostOrthogonal(n,k,λ): Manifold of n×k matrices with singular values in the interval [1-λ, 1+λ]

  • Gr(n,k): Manifold of k-dimensional subspaces in Rⁿ

  • GLp(n): Manifold of invertible n×n matrices with positive determinant

  • SL(n): Manifold of n×n matrices with determinant equal to 1

  • LowRank(n,k,r): Variety of n×k matrices of rank r or less

  • FixedRank(n,k,r): Manifold of n×k matrices of rank r

  • PSD(n): Cone of n×n symmetric positive definite matrices

  • PSSD(n): Cone of n×n symmetric positive semi-definite matrices

  • PSSDLowRank(n,r): Variety of n×n symmetric positive semi-definite matrices of rank r or less

  • PSSDFixedRank(n,r): Manifold of n×n symmetric positive semi-definite matrices of rank r

  • ProductManifold(M₁, ..., Mₖ): Product of manifolds M₁ × ... × Mₖ

Every space of dimension (n, k) can be applied to tensors of shape (*, n, k), so we also get efficient parallel implementations of product spaces such as

  • ObliqueManifold(n,k): Matrix with unit length columns, Sⁿ⁻¹ × ...ᵏ⁾ × Sⁿ⁻¹

Using GeoTorch in your Code

The files in examples/copying_problem.py and examples/sequential_mnist.py serve as tutorials to see how to handle the initialization and usage of GeoTorch in some real code. They also show how to implement Riemannian Gradient Descent and some other tricks. For an introduction to how the library is actually implemented, see the Jupyter Notebook examples/parametrisations.ipynb.

You may try GeoTorch installing it as

pip install geotorch

GeoTorch supports Python >= 3.10 and PyTorch >= 2.6. It is tested on Linux, macOS, and Windows.

Sharing Weights, Parametrizations, and Normalizing Flows

If one wants to use a parametrized tensor in different places in their model, or uses one parametrized layer many times, for example in an RNN, it is recommended to wrap the forward pass as follows to avoid each parametrization to be computed many times:

from torch.nn.utils import parametrize

with parametrize.cached():
    logits = model(input_)

Of course, this with statement may be used simply inside the forward function where the parametrized layer is used several times.

These ideas fall in the context of parametrized optimization, where one wraps a tensor X with a function f, and rather than using X, uses f(X). Particular examples of this idea are pruning, weight normalization, and spectral normalization among others.

As every space in GeoTorch is, at its core, a map from a flat space into a manifold, the tools implemented here also serve as a building block in normalizing flows. Using a factorized space such as LowRank(n,k,r) it is direct to compute the determinant of the transformation it defines, as we have direct access to the singular values of the layer.

Bibliography

Please cite the following work if you found GeoTorch useful. This paper exposes a simplified mathematical explanation of part of the inner-workings of GeoTorch.

@inproceedings{lezcano2019trivializations,
    title = {Trivializations for gradient-based optimization on manifolds},
    author = {Lezcano-Casado, Mario},
    booktitle={Advances in Neural Information Processing Systems, NeurIPS},
    pages = {9154--9164},
    year = {2019},
}

Metadata

Release files for geotorch 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geotorch 0.4.0
File Size Uploaded Provenance Actions
geotorch-0.4.0.tar.gz 27.5 kB With provenance Details for geotorch-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for geotorch 0.4.0
File Interpreter ABI Platform Actions
geotorch-0.4.0-py3-none-any.whl Python 3 none any Details for geotorch-0.4.0-py3-none-any.whl

Total release size: 65.7 kB

Release files / geotorch-0.4.0.tar.gz

Download URL geotorch-0.4.0.tar.gz
Size 27.5 kB
Tags Source
SHA-256 checksum
How to use checksums
104ae1e5da30ff7a2684369606fdd1bdac2715a1af307e27a702ea2433af0da9
BLAKE2b-256 checksum
How to use checksums
a8f76d585d2fb32607411589e442e1674247f0811170f09e5d8d83c051116b31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 6, 2026.

Transparency log

Release files / geotorch-0.4.0-py3-none-any.whl

Download URL geotorch-0.4.0-py3-none-any.whl
Size 38.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fade9cc732f696579f606d10906df07a8559863a40e0ca4329139a38ddd310a
BLAKE2b-256 checksum
How to use checksums
d41a09f3b52c95045d1572f2898d21d87a6f89fa5f8b6fb1a1af59084774576f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 6, 2026.

Transparency log

Security for geotorch 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for geotorch-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page